From 966dff560aefff2e38ba3e54f182b2f24798d4d9 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Wed, 26 Aug 2020 17:57:11 +0530 Subject: [PATCH 1/3] Adding BUILD_ID & BUILD_URL to build_results.txt --- bin/commands/runs.js | 1 + bin/helpers/utils.js | 11 +++++++++++ test/unit/bin/commands/runs.js | 4 +++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index e592d0bc..0b0029f9 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -41,6 +41,7 @@ module.exports = function run(args) { return build.createBuild(bsConfig, zip).then(function (data) { let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`; let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${config.dashboardUrl}${data.build_id}`; + utils.exportResults(data.build_id, `${config.dashboardUrl}${data.build_id}`); logger.info(message); logger.info(dashboardLink); utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null); diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index dcb5650c..9a76ed02 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -1,6 +1,7 @@ 'use strict'; const os = require("os"); const path = require("path"); +const fs = require("fs"); const usageReporting = require('./usageReporting'), logger = require('./logger').winstonLogger, @@ -120,3 +121,13 @@ exports.configCreated = (args) => { logger.info(message); this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null); } + +exports.exportResults = (buildId, buildUrl) => { + let data = "BUILD_ID=" + buildId + "\nBUILD_URL="+buildUrl; + fs.writeFileSync("log/build_results.txt", data , function(err){ + if(err) { + logger.warn(`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`); + logger.warn(`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`); + } + }); +} diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index be3e679e..d5857cc8 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -436,6 +436,7 @@ describe("runs", () => { zipUploadStub = sandbox.stub(); createBuildStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + exportResultsStub = sandbox.stub(); }); afterEach(() => { @@ -458,7 +459,8 @@ describe("runs", () => { setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, setParallels: setParallelsStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + exportResults: exportResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, From 4a4061e536c1c84282068d5d7c75128e719a8b70 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Wed, 26 Aug 2020 18:47:45 +0530 Subject: [PATCH 2/3] Adding unit test case for exportResults --- test/unit/bin/commands/runs.js | 1 + test/unit/bin/helpers/utils.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index d5857cc8..c615ec93 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -504,6 +504,7 @@ describe("runs", () => { sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(zipUploadStub); sinon.assert.calledOnce(createBuildStub); + sinon.assert.calledOnce(exportResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index 47559a66..c29314ef 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -4,7 +4,8 @@ const path = require('path'); const chai = require("chai"), expect = chai.expect, sinon = require('sinon'), - chaiAsPromised = require("chai-as-promised"); + chaiAsPromised = require("chai-as-promised"), + fs = require('fs'); const utils = require('../../../../bin/helpers/utils'), constant = require('../../../../bin/helpers/constants'), @@ -236,4 +237,24 @@ describe("utils", () => { sinon.assert.calledOnce(sendUsageReportStub); }); }); + + describe("exportResults", () => { + + it("should export results to log/build_results.txt", () => { + sinon.stub(fs, 'writeFileSync').returns(true); + utils.exportResults("build_id", "build_url"); + fs.writeFileSync.restore(); + }); + + it("should log warning if write to log/build_results.txt fails", () => { + let writeFileSyncStub = sinon.stub(fs, 'writeFileSync'); + let loggerWarnStub = sinon.stub(logger, "warn"); + writeFileSyncStub.yields(new Error("Write Failed")); + utils.exportResults("build_id", "build_url"); + sinon.assert.calledOnce(writeFileSyncStub); + sinon.assert.calledTwice(loggerWarnStub); + fs.writeFileSync.restore(); + }); + + }); }); From e133c5350c2954d336c73d6dc09ac482f93a693b Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Mon, 31 Aug 2020 20:33:19 +0530 Subject: [PATCH 3/3] Deleting build_results.txt on start of run command. --- bin/commands/runs.js | 2 ++ bin/helpers/utils.js | 5 +++++ test/unit/bin/commands/runs.js | 32 ++++++++++++++++++++++++-------- test/unit/bin/helpers/utils.js | 10 ++++++++++ 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 0b0029f9..6795a26f 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -11,6 +11,8 @@ const archiver = require("../helpers/archiver"), module.exports = function run(args) { let bsConfigPath = utils.getConfigPath(args.cf); + //Delete build_results.txt from log folder if already present. + utils.deleteResults(); return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) { utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting); diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 9a76ed02..0f36b2aa 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -131,3 +131,8 @@ exports.exportResults = (buildId, buildUrl) => { } }); } + +exports.deleteResults = () => { + fs.unlink("log/build_results.txt", function (err){ + }); +} diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index c615ec93..6f390ec7 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -27,6 +27,7 @@ describe("runs", () => { return "end"; }); getErrorCodeFromErrStub = sandbox.stub().returns("random-error-code"); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -45,7 +46,8 @@ describe("runs", () => { getErrorCodeFromErr: getErrorCodeFromErrStub, sendUsageReport: sendUsageReportStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + deleteResults: deleteResultsStub }, }); @@ -61,6 +63,7 @@ describe("runs", () => { sinon.assert.calledOnce(validateBstackJsonStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(getErrorCodeFromErrStub); + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, null, @@ -89,6 +92,7 @@ describe("runs", () => { }); getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code"); capabilityValidatorStub = sandbox.stub(); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -110,7 +114,8 @@ describe("runs", () => { setUsername: setUsernameStub, setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + deleteResults: deleteResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -131,6 +136,7 @@ describe("runs", () => { sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(getErrorCodeFromMsgStub); + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -161,6 +167,7 @@ describe("runs", () => { capabilityValidatorStub = sandbox.stub(); archiverStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -182,7 +189,8 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + deleteResults: deleteResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -212,6 +220,7 @@ describe("runs", () => { sinon.assert.calledOnce(archiverStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(deleteZipStub); + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -243,6 +252,7 @@ describe("runs", () => { archiverStub = sandbox.stub(); zipUploadStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -264,7 +274,8 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + deleteResults: deleteResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -298,7 +309,7 @@ describe("runs", () => { sinon.assert.calledOnce(archiverStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(zipUploadStub); - + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -334,6 +345,7 @@ describe("runs", () => { zipUploadStub = sandbox.stub(); createBuildStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -355,7 +367,8 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + deleteResults: deleteResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -398,6 +411,7 @@ describe("runs", () => { sinon.assert.calledOnce(createBuildStub); sinon.assert.calledOnce(sendUsageReportStub); + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, @@ -437,6 +451,7 @@ describe("runs", () => { createBuildStub = sandbox.stub(); deleteZipStub = sandbox.stub(); exportResultsStub = sandbox.stub(); + deleteResultsStub = sandbox.stub(); }); afterEach(() => { @@ -460,7 +475,8 @@ describe("runs", () => { setUsageReportingFlag: setUsageReportingFlagStub, setParallels: setParallelsStub, getConfigPath: getConfigPathStub, - exportResults: exportResultsStub + exportResults: exportResultsStub, + deleteResults: deleteResultsStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -505,7 +521,7 @@ describe("runs", () => { sinon.assert.calledOnce(zipUploadStub); sinon.assert.calledOnce(createBuildStub); sinon.assert.calledOnce(exportResultsStub); - + sinon.assert.calledOnce(deleteResultsStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index c29314ef..6bc08237 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -257,4 +257,14 @@ describe("utils", () => { }); }); + + describe("deleteResults", () => { + + it("should delete log/build_results.txt", () => { + sinon.stub(fs, 'unlink').returns(true); + utils.deleteResults(); + fs.unlink.restore(); + }); + + }); });