From e9be27201cd25951760cc3be904ed3327207d2ab Mon Sep 17 00:00:00 2001 From: Craig Atkinson Date: Fri, 25 Sep 2020 17:22:09 -0500 Subject: [PATCH] (feature) Publishing CI flag from Node script (#181) --- publishers/node-script/package.json | 4 +- .../publish-functional-test/package.json | 2 +- .../src/__tests__/util/projektor_client.js | 4 + .../without-token/publish-metadata.spec.js | 78 +++++++++++++++++++ .../publish-functional-test/yarn.lock | 10 +-- .../src/__tests__/publish-metdata.spec.js | 48 ++++++++++++ publishers/node-script/src/index.js | 5 +- publishers/node-script/src/publish.js | 10 ++- publishers/node-script/yarn.lock | 8 +- 9 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 publishers/node-script/publish-functional-test/src/__tests__/without-token/publish-metadata.spec.js create mode 100644 publishers/node-script/src/__tests__/publish-metdata.spec.js diff --git a/publishers/node-script/package.json b/publishers/node-script/package.json index d2a18a313..6296ab899 100644 --- a/publishers/node-script/package.json +++ b/publishers/node-script/package.json @@ -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", @@ -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" }, diff --git a/publishers/node-script/publish-functional-test/package.json b/publishers/node-script/publish-functional-test/package.json index c812750d1..5ed9c43dc 100644 --- a/publishers/node-script/publish-functional-test/package.json +++ b/publishers/node-script/publish-functional-test/package.json @@ -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" }, diff --git a/publishers/node-script/publish-functional-test/src/__tests__/util/projektor_client.js b/publishers/node-script/publish-functional-test/src/__tests__/util/projektor_client.js index 99ba1894c..a617417c9 100644 --- a/publishers/node-script/publish-functional-test/src/__tests__/util/projektor_client.js +++ b/publishers/node-script/publish-functional-test/src/__tests__/util/projektor_client.js @@ -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`); @@ -26,5 +29,6 @@ module.exports = { fetchAttachments, fetchAttachment, fetchCoverage, + fetchResultsMetadata, fetchGitMetadata, }; diff --git a/publishers/node-script/publish-functional-test/src/__tests__/without-token/publish-metadata.spec.js b/publishers/node-script/publish-functional-test/src/__tests__/without-token/publish-metadata.spec.js new file mode 100644 index 000000000..b0d335da6 --- /dev/null +++ b/publishers/node-script/publish-functional-test/src/__tests__/without-token/publish-metadata.spec.js @@ -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(); + } + ); + }); +}); diff --git a/publishers/node-script/publish-functional-test/yarn.lock b/publishers/node-script/publish-functional-test/yarn.lock index 62754cb0d..0c22709dc 100644 --- a/publishers/node-script/publish-functional-test/yarn.lock +++ b/publishers/node-script/publish-functional-test/yarn.lock @@ -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" @@ -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" diff --git a/publishers/node-script/src/__tests__/publish-metdata.spec.js b/publishers/node-script/src/__tests__/publish-metdata.spec.js new file mode 100644 index 000000000..69f63793e --- /dev/null +++ b/publishers/node-script/src/__tests__/publish-metdata.spec.js @@ -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); + }); +}); diff --git a/publishers/node-script/src/index.js b/publishers/node-script/src/index.js index 16302f147..4baf6855e 100644 --- a/publishers/node-script/src/index.js +++ b/publishers/node-script/src/index.js @@ -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(); @@ -71,7 +71,8 @@ async function run(args, publishToken, defaultConfigFilePath) { coverageFileGlobs, gitRepoName, gitBranchName, - projectName + projectName, + isCI ); if (!resultsBlob) { diff --git a/publishers/node-script/src/publish.js b/publishers/node-script/src/publish.js index c6ccb88de..439d69e01 100644 --- a/publishers/node-script/src/publish.js +++ b/publishers/node-script/src/publish.js @@ -49,7 +49,8 @@ const sendResults = async ( resultsBlob, gitRepoName, gitBranchName, - projectName + projectName, + isCI ) => { const headers = {}; @@ -75,6 +76,7 @@ const sendResults = async ( isMainBranch: gitBranchName === "main" || gitBranchName === "master", projectName, }, + ci: isCI, }, }; @@ -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}` @@ -228,7 +231,8 @@ const collectAndSendResults = async ( resultsBlob, gitRepoName, gitBranchName, - projectName + projectName, + isCI ); const publicId = resultsResponseData.id; diff --git a/publishers/node-script/yarn.lock b/publishers/node-script/yarn.lock index e9e6bcdf0..3ad6ac1f5 100644 --- a/publishers/node-script/yarn.lock +++ b/publishers/node-script/yarn.lock @@ -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"