diff --git a/CHANGELOG.md b/CHANGELOG.md index 12a0e04..c54a5f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +### Added +- Output launch UUID to file and ENV variable ## [5.1.2] - 2024-02-20 ### Fixed diff --git a/README.md b/README.md index 435c883..9dafc34 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ When creating a client instance, you need to specify the following options: | isLaunchMergeRequired | Optional | false | Allows client to merge launches into one at the end of the run via saving their UUIDs to the temp files at filesystem . At the end of the run launches can be merged using `mergeLaunches` method. Temp file format: `rplaunch-${launch_uuid}.tmp`. | | restClientConfig | Optional | Not set | `axios` like http client [config](https://github.com/axios/axios#request-config). May contain `agent` property for configure [http(s)](https://nodejs.org/api/https.html#https_https_request_url_options_callback) client, and other client options eg. `timeout`. For debugging and displaying logs you can set `debug: true`. | | launchUuidPrint | Optional | false | Whether to print the current launch UUID. | -| launchUuidPrintOutput | Optional | 'STDOUT' | Launch UUID printing output. Possible values: 'STDOUT', 'STDERR'. Works only if `launchUuidPrint` set to `true`. | +| launchUuidPrintOutput | Optional | 'STDOUT' | Launch UUID printing output. Possible values: 'STDOUT', 'STDERR', 'FILE', 'ENVIRONMENT'. Works only if `launchUuidPrint` set to `true`. File format: `rp-launch-uuid-${launch_uuid}.tmp`. Env variable: `RP_LAUNCH_UUID`. | | token | Deprecated | Not set | Use `apiKey` instead. | ## Asynchronous reporting diff --git a/lib/constants/outputs.js b/lib/constants/outputs.js index 85f7857..b5818e3 100644 --- a/lib/constants/outputs.js +++ b/lib/constants/outputs.js @@ -1,8 +1,13 @@ +const helpers = require('../helpers'); + const OUTPUT_TYPES = { // eslint-disable-next-line no-console - STDOUT: console.log, + STDOUT: (launchUuid) => console.log(`Report Portal Launch UUID: ${launchUuid}`), // eslint-disable-next-line no-console - STDERR: console.error, + STDERR: (launchUuid) => console.error(`Report Portal Launch UUID: ${launchUuid}`), + // eslint-disable-next-line no-return-assign + ENVIRONMENT: (launchUuid) => (process.env.RP_LAUNCH_UUID = launchUuid), + FILE: helpers.saveLaunchUuidToFile, }; module.exports = { OUTPUT_TYPES }; diff --git a/lib/helpers.js b/lib/helpers.js index f18579e..33ee556 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -89,4 +89,13 @@ module.exports = { return `${codeRef}[${parameters}]`; }, + + saveLaunchUuidToFile(launchUuid) { + const filename = `rp-launch-uuid-${launchUuid}.tmp`; + fs.open(filename, 'w', (err) => { + if (err) { + throw err; + } + }); + }, }; diff --git a/lib/report-portal-client.js b/lib/report-portal-client.js index 659294b..d29aa16 100644 --- a/lib/report-portal-client.js +++ b/lib/report-portal-client.js @@ -207,7 +207,7 @@ class RPClient { this.map[tempId].realId = response.id; this.launchUuid = response.id; if (this.config.launchUuidPrint) { - this.config.launchUuidPrintOutput(`Report Portal Launch UUID: ${this.launchUuid}`); + this.config.launchUuidPrintOutput(this.launchUuid); } if (this.isLaunchMergeRequired) { diff --git a/spec/helpers.spec.js b/spec/helpers.spec.js index 52c4d58..f1c7a0b 100644 --- a/spec/helpers.spec.js +++ b/spec/helpers.spec.js @@ -143,4 +143,18 @@ describe('Helpers', () => { expect(testCaseId).toEqual('codeRef[value,valueTwo,valueThree]'); }); }); + + describe('saveLaunchUuidToFile', () => { + it('should call fs.open method with right parameters', () => { + spyOn(fs, 'open'); + + helpers.saveLaunchUuidToFile('fileOne'); + + expect(fs.open).toHaveBeenCalledWith( + 'rp-launch-uuid-fileOne.tmp', + 'w', + jasmine.any(Function), + ); + }); + }); }); diff --git a/spec/report-portal-client.spec.js b/spec/report-portal-client.spec.js index 787d1a1..021470d 100644 --- a/spec/report-portal-client.spec.js +++ b/spec/report-portal-client.spec.js @@ -334,9 +334,7 @@ describe('ReportPortal javascript client', () => { startTime: time, }) .promise.then(function () { - expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith( - 'Report Portal Launch UUID: testidlaunch', - ); + expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith('testidlaunch'); }); }); @@ -357,9 +355,7 @@ describe('ReportPortal javascript client', () => { startTime: time, }) .promise.then(function () { - expect(OUTPUT_TYPES.STDERR).toHaveBeenCalledWith( - 'Report Portal Launch UUID: testidlaunch', - ); + expect(OUTPUT_TYPES.STDERR).toHaveBeenCalledWith('testidlaunch'); }); }); @@ -380,9 +376,49 @@ describe('ReportPortal javascript client', () => { startTime: time, }) .promise.then(function () { - expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith( - 'Report Portal Launch UUID: testidlaunch', - ); + expect(OUTPUT_TYPES.STDOUT).toHaveBeenCalledWith('testidlaunch'); + }); + }); + + it('should log Launch UUID into ENVIRONMENT if enabled', () => { + spyOn(OUTPUT_TYPES, 'ENVIRONMENT'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + launchUuidPrint: true, + launchUuidPrintOutput: 'environment', + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.ENVIRONMENT).toHaveBeenCalledWith('testidlaunch'); + }); + }); + + it('should log Launch UUID into FILE if enabled', () => { + spyOn(OUTPUT_TYPES, 'FILE'); + const client = new RPClient({ + apiKey: 'startLaunchTest', + endpoint: 'https://rp.us/api/v1', + project: 'tst', + launchUuidPrint: true, + launchUuidPrintOutput: 'file', + }); + const myPromise = Promise.resolve({ id: 'testidlaunch' }); + const time = 12345734; + spyOn(client.restClient, 'create').and.returnValue(myPromise); + return client + .startLaunch({ + startTime: time, + }) + .promise.then(function () { + expect(OUTPUT_TYPES.FILE).toHaveBeenCalledWith('testidlaunch'); }); });