From f5f057ac26ebe20390b5fbd0b575f7f16ad0f6f0 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Wed, 2 Sep 2020 20:14:38 +0530 Subject: [PATCH 1/3] Environment variables support for better CI/CD integration --- bin/commands/runs.js | 10 ++- bin/helpers/utils.js | 22 +++++ test/unit/bin/commands/runs.js | 42 +++++++-- test/unit/bin/helpers/utils.js | 158 +++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 8 deletions(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 785d5c0b..a635e893 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -15,15 +15,21 @@ module.exports = function run(args) { return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) { utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting); - // accept the username from command line if provided + // accept the username from command line or env variable if provided utils.setUsername(bsConfig, args); - // accept the access key from command line if provided + // accept the access key from command line or env variable if provided utils.setAccessKey(bsConfig, args); // accept the build name from command line if provided utils.setBuildName(bsConfig, args); + //accept the local from env variable if provided + utils.setLocal(bsConfig); + + //accept the local identifier from env variable if provided + utils.setLocalIdentifier(bsConfig); + // Validate browserstack.json values and parallels specified via arguments return capabilityHelper.validate(bsConfig, args).then(function (validated) { logger.info(validated); diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 3e878d8a..2e239727 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -95,12 +95,18 @@ exports.setParallels = (bsConfig, args) => { exports.setUsername = (bsConfig, args) => { if (!this.isUndefined(args.username)) { bsConfig['auth']['username'] = args.username; + }else if(!this.isUndefined(process.env.BROWSERSTACK_USERNAME)){ + bsConfig['auth']['username'] = process.env.BROWSERSTACK_USERNAME; + logger.info("Reading username from the environment variable BROWSERSTACK_USERNAME"); } } exports.setAccessKey = (bsConfig, args) => { if (!this.isUndefined(args.key)) { bsConfig['auth']['access_key'] = args.key; + }else if (!this.isUndefined(process.env.BROWSERSTACK_ACCESS_KEY)) { + bsConfig['auth']['access_key'] = process.env.BROWSERSTACK_ACCESS_KEY; + logger.info("Reading access key from the environment variable BROWSERSTACK_ACCESS_KEY"); } } @@ -149,3 +155,19 @@ exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => { exports.getLocalFlag = (connectionSettings) => { return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local } + +exports.setLocal = (bsConfig) => { + if(!this.isUndefined(process.env.BROWSERSTACK_LOCAL) && !this.isUndefined(bsConfig.connection_settings)){ + let local = false; + if(String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true; + bsConfig['connection_settings']['local'] = local; + logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL"); + } +} + +exports.setLocalIdentifier = (bsConfig) => { + if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER) && !this.isUndefined(bsConfig.connection_settings)){ + bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER; + logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER"); + } +} diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index e4d92a9f..210bc637 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -89,6 +89,8 @@ describe("runs", () => { }); getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code"); capabilityValidatorStub = sandbox.stub(); + setLocalStub = sandbox.stub(); + setLocalIdentifierStub = sandbox.stub(); }); afterEach(() => { @@ -110,7 +112,9 @@ describe("runs", () => { setUsername: setUsernameStub, setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + setLocal: setLocalStub, + setLocalIdentifier: setLocalIdentifierStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -131,6 +135,8 @@ describe("runs", () => { sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(getErrorCodeFromMsgStub); + sinon.assert.calledOnce(setLocalStub); + sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnceWithExactly( sendUsageReportStub, bsConfig, @@ -161,6 +167,8 @@ describe("runs", () => { capabilityValidatorStub = sandbox.stub(); archiverStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + setLocalStub = sandbox.stub(); + setLocalIdentifierStub = sandbox.stub(); }); afterEach(() => { @@ -182,7 +190,9 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + setLocal: setLocalStub, + setLocalIdentifier: setLocalIdentifierStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -206,7 +216,9 @@ describe("runs", () => { .catch((error) => { sinon.assert.calledOnce(getConfigPathStub); sinon.assert.calledOnce(getConfigPathStub); - sinon.assert.calledOnce(setParallelsStub) + sinon.assert.calledOnce(setParallelsStub); + sinon.assert.calledOnce(setLocalStub); + sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnce(validateBstackJsonStub); sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(archiverStub); @@ -243,6 +255,8 @@ describe("runs", () => { archiverStub = sandbox.stub(); zipUploadStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + setLocalStub = sandbox.stub(); + setLocalIdentifierStub = sandbox.stub(); }); afterEach(() => { @@ -264,7 +278,9 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + setLocal: setLocalStub, + setLocalIdentifier: setLocalIdentifierStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -293,6 +309,8 @@ describe("runs", () => { sinon.assert.calledOnce(getConfigPathStub); sinon.assert.calledOnce(getConfigPathStub); sinon.assert.calledOnce(setParallelsStub); + sinon.assert.calledOnce(setLocalStub); + sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnce(validateBstackJsonStub); sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(archiverStub); @@ -334,6 +352,8 @@ describe("runs", () => { zipUploadStub = sandbox.stub(); createBuildStub = sandbox.stub(); deleteZipStub = sandbox.stub(); + setLocalStub = sandbox.stub(); + setLocalIdentifierStub = sandbox.stub(); }); afterEach(() => { @@ -355,7 +375,9 @@ describe("runs", () => { setAccessKey: setAccessKeyStub, setBuildName: setBuildNameStub, setUsageReportingFlag: setUsageReportingFlagStub, - getConfigPath: getConfigPathStub + getConfigPath: getConfigPathStub, + setLocal: setLocalStub, + setLocalIdentifier: setLocalIdentifierStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -392,6 +414,8 @@ describe("runs", () => { sinon.assert.calledOnce(validateBstackJsonStub); sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(setParallelsStub); + sinon.assert.calledOnce(setLocalStub); + sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnce(archiverStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(zipUploadStub); @@ -437,6 +461,8 @@ describe("runs", () => { createBuildStub = sandbox.stub(); deleteZipStub = sandbox.stub(); isUndefinedStub = sandbox.stub(); + setLocalStub = sandbox.stub(); + setLocalIdentifierStub = sandbox.stub(); }); afterEach(() => { @@ -460,7 +486,9 @@ describe("runs", () => { setUsageReportingFlag: setUsageReportingFlagStub, setParallels: setParallelsStub, getConfigPath: getConfigPathStub, - isUndefined: isUndefinedStub + isUndefined: isUndefinedStub, + setLocal: setLocalStub, + setLocalIdentifier: setLocalIdentifierStub }, "../helpers/capabilityHelper": { validate: capabilityValidatorStub, @@ -500,6 +528,8 @@ describe("runs", () => { sinon.assert.calledOnce(validateBstackJsonStub); sinon.assert.calledOnce(capabilityValidatorStub); sinon.assert.calledOnce(setParallelsStub); + sinon.assert.calledOnce(setLocalStub); + sinon.assert.calledOnce(setLocalIdentifierStub); sinon.assert.calledOnce(archiverStub); sinon.assert.calledOnce(setUsageReportingFlagStub); sinon.assert.calledOnce(zipUploadStub); diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index dd66325f..9ed20def 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -273,4 +273,162 @@ describe("utils", () => { expect(utils.getLocalFlag({"local": true})).to.be.true; }); }); + + describe("setLocal", () => { + beforeEach(function () { + delete process.env.BROWSERSTACK_LOCAL; + }); + + afterEach(function () { + delete process.env.BROWSERSTACK_LOCAL; + }); + + it("should not change local in bsConfig if process.env.BROWSERSTACK_LOCAL is undefined", () => { + let bsConfig = { + connection_settings: { + local: true + } + } + utils.setLocal(bsConfig); + expect(bsConfig.connection_settings.local).to.be.eq(true); + }); + + it("should change local to false in bsConfig if process.env.BROWSERSTACK_LOCAL is set to false", () => { + let bsConfig = { + connection_settings: { + local: true + } + } + process.env.BROWSERSTACK_LOCAL = false; + utils.setLocal(bsConfig); + expect(bsConfig.connection_settings.local).to.be.eq(false); + }); + + it("should change local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true", () => { + let bsConfig = { + connection_settings: { + local: false + } + } + process.env.BROWSERSTACK_LOCAL = true; + utils.setLocal(bsConfig); + expect(bsConfig.connection_settings.local).to.be.eq(true); + }); + }); + + describe("setLocalIdentifier", () => { + beforeEach(function () { + delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER; + }); + + afterEach(function () { + delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER; + }); + it("should not change local identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined", () => { + let bsConfig = { + connection_settings: { + local_identifier: "local_identifier" + } + } + utils.setLocalIdentifier(bsConfig); + expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier"); + }); + + it("should change local identifier to local_identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is set to local_identifier", () => { + let bsConfig = { + connection_settings: { + local_identifier: "test" + } + } + process.env.BROWSERSTACK_LOCAL_IDENTIFIER = "local_identifier"; + utils.setLocalIdentifier(bsConfig); + expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier"); + }); + + }); + + describe("setUsername", () => { + + beforeEach(function () { + delete process.env.BROWSERSTACK_USERNAME; + }); + + afterEach(function () { + delete process.env.BROWSERSTACK_USERNAME; + }); + + it("should set username if args.username is present", () =>{ + let bsConfig = { + auth: { + username: "test" + } + } + utils.setUsername(bsConfig,{username: "username"}); + expect(bsConfig.auth.username).to.be.eq("username"); + }); + + it("should set username if process.env.BROWSERSTACK_USERNAME is present and args.username is not present", () =>{ + let bsConfig = { + auth: { + username: "test" + } + } + process.env.BROWSERSTACK_USERNAME = "username" + utils.setUsername(bsConfig,{}); + expect(bsConfig.auth.username).to.be.eq("username"); + }); + + it("should set username to default if process.env.BROWSERSTACK_USERNAME and args.username is not present", () =>{ + let bsConfig = { + auth: { + username: "test" + } + } + utils.setUsername(bsConfig,{}); + expect(bsConfig.auth.username).to.be.eq("test"); + }); + + }); + + describe("setAccessKey", () => { + beforeEach(function () { + delete process.env.BROWSERSTACK_ACCESS_KEY; + }); + + afterEach(function () { + delete process.env.BROWSERSTACK_ACCESS_KEY; + }); + + it("should set access_key if args.key is present", () =>{ + let bsConfig = { + auth: { + access_key: "test" + } + } + utils.setAccessKey(bsConfig,{key: "access_key"}); + expect(bsConfig.auth.access_key).to.be.eq("access_key"); + }); + + it("should set access_key if process.env.BROWSERSTACK_ACCESS_KEY is present and args.access_key is not present", () =>{ + let bsConfig = { + auth: { + access_key: "test" + } + } + process.env.BROWSERSTACK_ACCESS_KEY = "access_key" + utils.setAccessKey(bsConfig,{}); + expect(bsConfig.auth.access_key).to.be.eq("access_key"); + }); + + it("should set access_key to default if process.env.BROWSERSTACK_ACCESS_KEY and args.access_key is not present", () =>{ + let bsConfig = { + auth: { + access_key: "test" + } + } + utils.setAccessKey(bsConfig,{}); + expect(bsConfig.auth.access_key).to.be.eq("test"); + }); + + }); }); From 3f62dbbee0da5e1e2a7a2b174103b49c34f51e5d Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Wed, 2 Sep 2020 21:22:37 +0530 Subject: [PATCH 2/3] Setting local & local_identifier if not present in conf json but set as env variable. --- bin/helpers/utils.js | 4 ++-- test/unit/bin/helpers/utils.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/bin/helpers/utils.js b/bin/helpers/utils.js index 65463d6e..9305e7da 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -173,7 +173,7 @@ exports.getLocalFlag = (connectionSettings) => { } exports.setLocal = (bsConfig) => { - if(!this.isUndefined(process.env.BROWSERSTACK_LOCAL) && !this.isUndefined(bsConfig.connection_settings)){ + if(!this.isUndefined(process.env.BROWSERSTACK_LOCAL)){ let local = false; if(String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true; bsConfig['connection_settings']['local'] = local; @@ -182,7 +182,7 @@ exports.setLocal = (bsConfig) => { } exports.setLocalIdentifier = (bsConfig) => { - if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER) && !this.isUndefined(bsConfig.connection_settings)){ + if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)){ bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER; logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER"); } diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index 5ccdf75d..86fdc32a 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -344,6 +344,17 @@ describe("utils", () => { utils.setLocal(bsConfig); expect(bsConfig.connection_settings.local).to.be.eq(true); }); + + it("should set local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true & local is not set in bsConfig", () => { + let bsConfig = { + connection_settings: { + } + } + process.env.BROWSERSTACK_LOCAL = true; + utils.setLocal(bsConfig); + expect(bsConfig.connection_settings.local).to.be.eq(true); + }); + }); describe("setLocalIdentifier", () => { @@ -375,6 +386,16 @@ describe("utils", () => { expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier"); }); + it("should set local identifier in connection_settings in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is present & not set in bsConfig", () => { + let bsConfig = { + connection_settings: { + } + } + process.env.BROWSERSTACK_LOCAL_IDENTIFIER = "local_identifier"; + utils.setLocalIdentifier(bsConfig); + expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier"); + }); + }); describe("setUsername", () => { From 799922ef2289da359b64fea94d6e93791bbd4f9f Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Thu, 3 Sep 2020 11:47:47 +0530 Subject: [PATCH 3/3] Formatting changes --- bin/commands/runs.js | 4 +-- bin/helpers/utils.js | 32 +++++++++---------- test/unit/bin/commands/runs.js | 2 +- test/unit/bin/helpers/utils.js | 58 +++++++++++++++++----------------- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/bin/commands/runs.js b/bin/commands/runs.js index 6f1f590f..661e129f 100644 --- a/bin/commands/runs.js +++ b/bin/commands/runs.js @@ -54,8 +54,8 @@ module.exports = function run(args) { logger.warn(Constants.userMessages.NO_PARALLELS); } - if(!args.disableNpmWarning && bsConfig.run_settings.npm_dependencies && Object.keys(bsConfig.run_settings.npm_dependencies).length <= 0) logger.warn(Constants.userMessages.NO_NPM_DEPENDENCIES); - + if (!args.disableNpmWarning && bsConfig.run_settings.npm_dependencies && Object.keys(bsConfig.run_settings.npm_dependencies).length <= 0) logger.warn(Constants.userMessages.NO_NPM_DEPENDENCIES); + 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 9305e7da..6a1d4680 100644 --- a/bin/helpers/utils.js +++ b/bin/helpers/utils.js @@ -3,19 +3,19 @@ const os = require("os"); const path = require("path"); const fs = require("fs"); -const usageReporting = require('./usageReporting'), +const usageReporting = require('./usageReporting'), logger = require('./logger').winstonLogger, Constants = require('./constants'); exports.validateBstackJson = (bsConfigPath) => { - return new Promise(function(resolve, reject){ + return new Promise(function (resolve, reject) { try { logger.info(`Reading config from ${bsConfigPath}`); let bsConfig = require(bsConfigPath); resolve(bsConfig); } catch (e) { - reject("Couldn't find the browserstack.json file at \""+ bsConfigPath +"\". Please use --config-file ."); + reject("Couldn't find the browserstack.json file at \"" + bsConfigPath + "\". Please use --config-file ."); } }); } @@ -51,7 +51,7 @@ exports.getErrorCodeFromMsg = (errMsg) => { errorCode = "invalid_directory_structure"; break; } - if(errMsg.includes("Please use --config-file .")){ + if (errMsg.includes("Please use --config-file .")) { errorCode = "bstack_json_path_invalid"; } return errorCode; @@ -96,7 +96,7 @@ exports.setParallels = (bsConfig, args) => { exports.setUsername = (bsConfig, args) => { if (!this.isUndefined(args.username)) { bsConfig['auth']['username'] = args.username; - }else if(!this.isUndefined(process.env.BROWSERSTACK_USERNAME)){ + } else if (!this.isUndefined(process.env.BROWSERSTACK_USERNAME)) { bsConfig['auth']['username'] = process.env.BROWSERSTACK_USERNAME; logger.info("Reading username from the environment variable BROWSERSTACK_USERNAME"); } @@ -105,7 +105,7 @@ exports.setUsername = (bsConfig, args) => { exports.setAccessKey = (bsConfig, args) => { if (!this.isUndefined(args.key)) { bsConfig['auth']['access_key'] = args.key; - }else if (!this.isUndefined(process.env.BROWSERSTACK_ACCESS_KEY)) { + } else if (!this.isUndefined(process.env.BROWSERSTACK_ACCESS_KEY)) { bsConfig['auth']['access_key'] = process.env.BROWSERSTACK_ACCESS_KEY; logger.info("Reading access key from the environment variable BROWSERSTACK_ACCESS_KEY"); } @@ -122,14 +122,14 @@ exports.isUndefined = value => (value === undefined || value === null); exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0); exports.isParallelValid = (value) => { - return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1 ) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE; + return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE; } exports.getUserAgent = () => { return `BStack-Cypress-CLI/1.3.0 (${os.arch()}/${os.platform()}/${os.release()})`; } -exports.isAbsolute = (configPath) => { +exports.isAbsolute = (configPath) => { return path.isAbsolute(configPath) } @@ -144,9 +144,9 @@ exports.configCreated = (args) => { } exports.exportResults = (buildId, buildUrl) => { - let data = "BUILD_ID=" + buildId + "\nBUILD_URL="+buildUrl; - fs.writeFileSync("log/build_results.txt", data , function(err){ - if(err) { + 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`); } @@ -154,7 +154,7 @@ exports.exportResults = (buildId, buildUrl) => { } exports.deleteResults = () => { - fs.unlink("log/build_results.txt", function (err){ + fs.unlink("log/build_results.txt", function (err) { }); } @@ -162,7 +162,7 @@ exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => { // Getting absolute path cypressDir = path.resolve(cypressDir); cypressProjDir = path.resolve(cypressProjDir); - if(cypressProjDir === cypressDir) return true; + if (cypressProjDir === cypressDir) return true; let parentTokens = cypressDir.split('/').filter(i => i.length); let childTokens = cypressProjDir.split('/').filter(i => i.length); return parentTokens.every((t, i) => childTokens[i] === t); @@ -173,16 +173,16 @@ exports.getLocalFlag = (connectionSettings) => { } exports.setLocal = (bsConfig) => { - if(!this.isUndefined(process.env.BROWSERSTACK_LOCAL)){ + if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL)) { let local = false; - if(String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true; + if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true; bsConfig['connection_settings']['local'] = local; logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL"); } } exports.setLocalIdentifier = (bsConfig) => { - if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)){ + if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)) { bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER; logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER"); } diff --git a/test/unit/bin/commands/runs.js b/test/unit/bin/commands/runs.js index 165a3a3f..40db4205 100644 --- a/test/unit/bin/commands/runs.js +++ b/test/unit/bin/commands/runs.js @@ -51,7 +51,7 @@ describe("runs", () => { }, }); - validateBstackJsonStub.returns(Promise.reject({message: "random-error"})); + validateBstackJsonStub.returns(Promise.reject({ message: "random-error" })); return runs(args) .then(function (_bsConfig) { diff --git a/test/unit/bin/helpers/utils.js b/test/unit/bin/helpers/utils.js index 86fdc32a..3f7331ca 100644 --- a/test/unit/bin/helpers/utils.js +++ b/test/unit/bin/helpers/utils.js @@ -105,7 +105,7 @@ describe("utils", () => { "parallels": 10, } }; - utils.setParallels(bsConfig, {parallels: 100}); + utils.setParallels(bsConfig, { parallels: 100 }); expect(bsConfig['run_settings']['parallels']).to.be.eq(100); }); @@ -115,7 +115,7 @@ describe("utils", () => { "parallels": 10, } }; - utils.setParallels(bsConfig, {parallels: undefined}); + utils.setParallels(bsConfig, { parallels: undefined }); expect(bsConfig['run_settings']['parallels']).to.be.eq(10); }); }); @@ -126,8 +126,8 @@ describe("utils", () => { }); it(`should return value depending on validation messages`, () => { - expect(utils.getErrorCodeFromErr({"code": "SyntaxError"})).to.eq("bstack_json_parse_error"); - expect(utils.getErrorCodeFromErr({"code": "EACCES"})).to.eq("bstack_json_no_permission"); + expect(utils.getErrorCodeFromErr({ "code": "SyntaxError" })).to.eq("bstack_json_parse_error"); + expect(utils.getErrorCodeFromErr({ "code": "EACCES" })).to.eq("bstack_json_no_permission"); }); }); @@ -139,15 +139,15 @@ describe("utils", () => { describe("validateBstackJson", () => { it("should reject with SyntaxError for empty file", () => { - let bsConfigPath = path.join(process.cwd(),'test', 'test_files', 'dummy_bstack.json'); + let bsConfigPath = path.join(process.cwd(), 'test', 'test_files', 'dummy_bstack.json'); expect(utils.validateBstackJson(bsConfigPath)).to.be.rejectedWith(SyntaxError); }); it("should resolve with data for valid json", () => { - let bsConfigPath = path.join(process.cwd(),'test', 'test_files', 'dummy_bstack_2.json'); + let bsConfigPath = path.join(process.cwd(), 'test', 'test_files', 'dummy_bstack_2.json'); expect(utils.validateBstackJson(bsConfigPath)).to.be.eventually.eql({}); }); it("should reject with SyntaxError for invalid json file", () => { - let bsConfigPath = path.join(process.cwd(),'test', 'test_files', 'dummy_bstack_3.json'); + let bsConfigPath = path.join(process.cwd(), 'test', 'test_files', 'dummy_bstack_3.json'); expect(utils.validateBstackJson(bsConfigPath)).to.be.rejectedWith(SyntaxError); }); }); @@ -271,18 +271,18 @@ describe("utils", () => { fs.unlink.restore(); }); }); - + describe("isCypressProjDirValid", () => { - it("should return true when cypressDir and cypressProjDir is same", () =>{ - expect(utils.isCypressProjDirValid("/absolute/path","/absolute/path")).to.be.true; + it("should return true when cypressDir and cypressProjDir is same", () => { + expect(utils.isCypressProjDirValid("/absolute/path", "/absolute/path")).to.be.true; }); - it("should return true when cypressProjDir is child directory of cypressDir", () =>{ - expect(utils.isCypressProjDirValid("/absolute/path","/absolute/path/childpath")).to.be.true; + it("should return true when cypressProjDir is child directory of cypressDir", () => { + expect(utils.isCypressProjDirValid("/absolute/path", "/absolute/path/childpath")).to.be.true; }); - it("should return false when cypressProjDir is not child directory of cypressDir", () =>{ - expect(utils.isCypressProjDirValid("/absolute/path","/absolute")).to.be.false; + it("should return false when cypressProjDir is not child directory of cypressDir", () => { + expect(utils.isCypressProjDirValid("/absolute/path", "/absolute")).to.be.false; }); }); @@ -296,14 +296,14 @@ describe("utils", () => { }); it("should return false if connectionSettings.local is false", () => { - expect(utils.getLocalFlag({"local": false})).to.be.false; + expect(utils.getLocalFlag({ "local": false })).to.be.false; }); it("should return true if connectionSettings.local is true", () => { - expect(utils.getLocalFlag({"local": true})).to.be.true; + expect(utils.getLocalFlag({ "local": true })).to.be.true; }); }); - + describe("setLocal", () => { beforeEach(function () { delete process.env.BROWSERSTACK_LOCAL; @@ -408,34 +408,34 @@ describe("utils", () => { delete process.env.BROWSERSTACK_USERNAME; }); - it("should set username if args.username is present", () =>{ + it("should set username if args.username is present", () => { let bsConfig = { auth: { username: "test" } } - utils.setUsername(bsConfig,{username: "username"}); + utils.setUsername(bsConfig, { username: "username" }); expect(bsConfig.auth.username).to.be.eq("username"); }); - it("should set username if process.env.BROWSERSTACK_USERNAME is present and args.username is not present", () =>{ + it("should set username if process.env.BROWSERSTACK_USERNAME is present and args.username is not present", () => { let bsConfig = { auth: { username: "test" } } process.env.BROWSERSTACK_USERNAME = "username" - utils.setUsername(bsConfig,{}); + utils.setUsername(bsConfig, {}); expect(bsConfig.auth.username).to.be.eq("username"); }); - it("should set username to default if process.env.BROWSERSTACK_USERNAME and args.username is not present", () =>{ + it("should set username to default if process.env.BROWSERSTACK_USERNAME and args.username is not present", () => { let bsConfig = { auth: { username: "test" } } - utils.setUsername(bsConfig,{}); + utils.setUsername(bsConfig, {}); expect(bsConfig.auth.username).to.be.eq("test"); }); @@ -450,34 +450,34 @@ describe("utils", () => { delete process.env.BROWSERSTACK_ACCESS_KEY; }); - it("should set access_key if args.key is present", () =>{ + it("should set access_key if args.key is present", () => { let bsConfig = { auth: { access_key: "test" } } - utils.setAccessKey(bsConfig,{key: "access_key"}); + utils.setAccessKey(bsConfig, { key: "access_key" }); expect(bsConfig.auth.access_key).to.be.eq("access_key"); }); - it("should set access_key if process.env.BROWSERSTACK_ACCESS_KEY is present and args.access_key is not present", () =>{ + it("should set access_key if process.env.BROWSERSTACK_ACCESS_KEY is present and args.access_key is not present", () => { let bsConfig = { auth: { access_key: "test" } } process.env.BROWSERSTACK_ACCESS_KEY = "access_key" - utils.setAccessKey(bsConfig,{}); + utils.setAccessKey(bsConfig, {}); expect(bsConfig.auth.access_key).to.be.eq("access_key"); }); - it("should set access_key to default if process.env.BROWSERSTACK_ACCESS_KEY and args.access_key is not present", () =>{ + it("should set access_key to default if process.env.BROWSERSTACK_ACCESS_KEY and args.access_key is not present", () => { let bsConfig = { auth: { access_key: "test" } } - utils.setAccessKey(bsConfig,{}); + utils.setAccessKey(bsConfig, {}); expect(bsConfig.auth.access_key).to.be.eq("test"); });