Skip to content

Commit

Permalink
(feature) Publishing CI flag from Node script (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
craigatk authored Sep 25, 2020
1 parent 909f62c commit e9be272
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 17 deletions.
4 changes: 2 additions & 2 deletions publishers/node-script/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "projektor-publish",
"version": "2.7.0",
"version": "2.8.0",
"homepage": "https://projektor.dev/docs/node-script/",
"repository": {
"type": "git",
Expand All @@ -26,7 +26,7 @@
"env-cmd": "10.1.0",
"jest": "25.1.0",
"jest-junit": "10.0.0",
"prettier": "2.0.1",
"prettier": "2.1.2",
"projektor-publish": "2.7.0",
"wait-for-expect": "3.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"env-cmd": "10.1.0",
"jest": "25.1.0",
"jest-junit": "10.0.0",
"prettier": "2.0.1",
"prettier": "2.1.2",
"projektor-publish": "file:../",
"wait-for-expect": "3.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const fetchAttachment = (attachmentName, testRunId, serverPort) => {
const fetchCoverage = (testRunId, serverPort) =>
axios.get(`http://localhost:${serverPort}/run/${testRunId}/coverage`);

const fetchResultsMetadata = (testRunId, serverPort) =>
axios.get(`http://localhost:${serverPort}/run/${testRunId}/metadata`);

const fetchGitMetadata = (testRunId, serverPort) =>
axios.get(`http://localhost:${serverPort}/run/${testRunId}/metadata/git`);

Expand All @@ -26,5 +29,6 @@ module.exports = {
fetchAttachments,
fetchAttachment,
fetchCoverage,
fetchResultsMetadata,
fetchGitMetadata,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { exec } = require("child_process");
const waitForExpect = require("wait-for-expect");
const {
fetchTestRunSummary,
fetchResultsMetadata,
} = require("../util/projektor_client");
const { extractTestRunId } = require("../util/parse_output");
const { verifyOutput } = require("../verify/cli_output_verify");

describe("publish results metadata functional spec", () => {
const serverPort = "8082";

it("should publish CI=true when running in CI", async (done) => {
exec(
`env-cmd -f .ci-env yarn projektor-publish --serverUrl=http://localhost:${serverPort} results/*.xml`,
async (error, stdout, stderr) => {
verifyOutput(error, stdout, stderr, serverPort);
expect(error).toBeNull();

const testRunId = extractTestRunId(stdout);
console.log("Test ID", testRunId);

await waitForExpect(async () => {
const testRunSummaryResponse = await fetchTestRunSummary(
testRunId,
serverPort
);
expect(testRunSummaryResponse.status).toEqual(200);
});

const metadataResponse = await fetchResultsMetadata(
testRunId,
serverPort
);
expect(metadataResponse.status).toEqual(200);

console.log("Results metadata response", metadataResponse.data);

expect(metadataResponse.data.ci).toEqual(true);

done();
}
);
});

it("should publish CI=false when running in CI", async (done) => {
exec(
`env-cmd -f .no-ci-env yarn projektor-publish --serverUrl=http://localhost:${serverPort} results/*.xml`,
async (error, stdout, stderr) => {
verifyOutput(error, stdout, stderr, serverPort);
expect(error).toBeNull();

const testRunId = extractTestRunId(stdout);
console.log("Test ID", testRunId);

await waitForExpect(async () => {
const testRunSummaryResponse = await fetchTestRunSummary(
testRunId,
serverPort
);
expect(testRunSummaryResponse.status).toEqual(200);
});

const metadataResponse = await fetchResultsMetadata(
testRunId,
serverPort
);
expect(metadataResponse.status).toEqual(200);

console.log("Results metadata response", metadataResponse.data);

expect(metadataResponse.data.ci).toEqual(false);

done();
}
);
});
});
10 changes: 5 additions & 5 deletions publishers/node-script/publish-functional-test/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2780,10 +2780,10 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=

prettier@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.1.tgz#3f00ac71263be34684b2b2c8d7e7f63737592dac"
integrity sha512-piXGBcY1zoFOG0MvHpNE5reAGseLmaCRifQ/fmfF49BcYkInEs/naD/unxGNAeOKFA5+JxVrPyMvMlpzcd20UA==
prettier@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==

pretty-format@^24.9.0:
version "24.9.0"
Expand All @@ -2806,7 +2806,7 @@ pretty-format@^25.1.0:
react-is "^16.12.0"

"projektor-publish@file:..":
version "2.3.1"
version "2.8.0"
dependencies:
axios "0.19.2"
glob "7.1.4"
Expand Down
48 changes: 48 additions & 0 deletions publishers/node-script/src/__tests__/publish-metdata.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
const { collectAndSendResults } = require("../publish");

describe("Node script publishing with results metadata", () => {
let mockAxios;

beforeEach(() => {
mockAxios = new MockAdapter(axios);
});

afterEach(() => {
mockAxios.restore();
});

it("should publish results along with metadata", async () => {
const fileGlob = "src/__tests__/resultsDir1/*.xml";
const serverUrl = "http://localhost:8080";
mockAxios
.onPost("http://localhost:8080/groupedResults")
.reply(200, { id: "ABC123", uri: "/tests/ABC123" });

const isCI = true;

await collectAndSendResults(
serverUrl,
null,
[fileGlob],
null,
null,
"projektor/projektor",
"main",
"my-proj",
isCI
);

expect(mockAxios.history.post.length).toBe(1);

const resultsPostRequest = mockAxios.history.post.find((postRequest) =>
postRequest.url.includes("groupedResults")
);

const parsedRequestBody = JSON.parse(resultsPostRequest.data);
expect(parsedRequestBody.groupedTestSuites.length).toBe(1);

expect(parsedRequestBody.metadata.ci).toBe(true);
});
});
5 changes: 3 additions & 2 deletions publishers/node-script/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ async function run(args, publishToken, defaultConfigFilePath) {
}

if (resultsFileGlobs) {
const isCI = process.env.CI && process.env.CI !== "false";
const isCI = Boolean(process.env.CI) && process.env.CI !== "false";
const gitRepoName =
process.env.VELA_REPO_FULL_NAME || process.env.GITHUB_REPOSITORY;
const gitBranchName = findGitBranchName();
Expand All @@ -71,7 +71,8 @@ async function run(args, publishToken, defaultConfigFilePath) {
coverageFileGlobs,
gitRepoName,
gitBranchName,
projectName
projectName,
isCI
);

if (!resultsBlob) {
Expand Down
10 changes: 7 additions & 3 deletions publishers/node-script/src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const sendResults = async (
resultsBlob,
gitRepoName,
gitBranchName,
projectName
projectName,
isCI
) => {
const headers = {};

Expand All @@ -75,6 +76,7 @@ const sendResults = async (
isMainBranch: gitBranchName === "main" || gitBranchName === "master",
projectName,
},
ci: isCI,
},
};

Expand Down Expand Up @@ -212,7 +214,8 @@ const collectAndSendResults = async (
coverageFileGlobs,
gitRepoName,
gitBranchName,
projectName
projectName,
isCI
) => {
console.log(
`Gathering results from ${resultsFileGlobs} to send to Projektor server ${serverUrl}`
Expand All @@ -228,7 +231,8 @@ const collectAndSendResults = async (
resultsBlob,
gitRepoName,
gitBranchName,
projectName
projectName,
isCI
);

const publicId = resultsResponseData.id;
Expand Down
8 changes: 4 additions & 4 deletions publishers/node-script/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2625,10 +2625,10 @@ prelude-ls@~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"

prettier@2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.0.1.tgz#3f00ac71263be34684b2b2c8d7e7f63737592dac"
integrity sha512-piXGBcY1zoFOG0MvHpNE5reAGseLmaCRifQ/fmfF49BcYkInEs/naD/unxGNAeOKFA5+JxVrPyMvMlpzcd20UA==
prettier@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==

pretty-format@^24.9.0:
version "24.9.0"
Expand Down

0 comments on commit e9be272

Please sign in to comment.