Skip to content

Environment variables support for better CI/CD integration #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,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);
Expand All @@ -48,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);
Expand Down
44 changes: 33 additions & 11 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <path to browserstack.json>.");
reject("Couldn't find the browserstack.json file at \"" + bsConfigPath + "\". Please use --config-file <path to browserstack.json>.");
}
});
}
Expand Down Expand Up @@ -51,7 +51,7 @@ exports.getErrorCodeFromMsg = (errMsg) => {
errorCode = "invalid_directory_structure";
break;
}
if(errMsg.includes("Please use --config-file <path to browserstack.json>.")){
if (errMsg.includes("Please use --config-file <path to browserstack.json>.")) {
errorCode = "bstack_json_path_invalid";
}
return errorCode;
Expand Down Expand Up @@ -96,12 +96,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");
}
}

Expand All @@ -116,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)
}

Expand All @@ -138,25 +144,25 @@ 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`);
}
});
}

exports.deleteResults = () => {
fs.unlink("log/build_results.txt", function (err){
fs.unlink("log/build_results.txt", function (err) {
});
}

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);
Expand All @@ -165,3 +171,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)) {
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)) {
bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER");
}
}
34 changes: 32 additions & 2 deletions test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -92,6 +92,8 @@ describe("runs", () => {
});
getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code");
capabilityValidatorStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
});

Expand All @@ -115,6 +117,8 @@ describe("runs", () => {
setAccessKey: setAccessKeyStub,
setBuildName: setBuildNameStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
deleteResults: deleteResultsStub
},
"../helpers/capabilityHelper": {
Expand All @@ -136,6 +140,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.calledOnce(deleteResultsStub);
sinon.assert.calledOnceWithExactly(
sendUsageReportStub,
Expand Down Expand Up @@ -167,6 +173,8 @@ describe("runs", () => {
capabilityValidatorStub = sandbox.stub();
archiverStub = sandbox.stub();
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
});

Expand All @@ -190,6 +198,8 @@ describe("runs", () => {
setBuildName: setBuildNameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
deleteResults: deleteResultsStub
},
"../helpers/capabilityHelper": {
Expand All @@ -214,7 +224,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);
Expand Down Expand Up @@ -252,6 +264,8 @@ describe("runs", () => {
archiverStub = sandbox.stub();
zipUploadStub = sandbox.stub();
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
});

Expand All @@ -275,6 +289,8 @@ describe("runs", () => {
setBuildName: setBuildNameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
deleteResults: deleteResultsStub
},
"../helpers/capabilityHelper": {
Expand Down Expand Up @@ -304,6 +320,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);
Expand Down Expand Up @@ -345,6 +363,8 @@ describe("runs", () => {
zipUploadStub = sandbox.stub();
createBuildStub = sandbox.stub();
deleteZipStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
});

Expand All @@ -368,6 +388,8 @@ describe("runs", () => {
setBuildName: setBuildNameStub,
setUsageReportingFlag: setUsageReportingFlagStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
deleteResults: deleteResultsStub
},
"../helpers/capabilityHelper": {
Expand Down Expand Up @@ -405,6 +427,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);
Expand Down Expand Up @@ -453,6 +477,8 @@ describe("runs", () => {
exportResultsStub = sandbox.stub();
deleteResultsStub = sandbox.stub();
isUndefinedStub = sandbox.stub();
setLocalStub = sandbox.stub();
setLocalIdentifierStub = sandbox.stub();
});

afterEach(() => {
Expand All @@ -476,6 +502,8 @@ describe("runs", () => {
setUsageReportingFlag: setUsageReportingFlagStub,
setParallels: setParallelsStub,
getConfigPath: getConfigPathStub,
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
exportResults: exportResultsStub,
deleteResults: deleteResultsStub,
isUndefined: isUndefinedStub
Expand Down Expand Up @@ -518,6 +546,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);
Expand Down
Loading