Skip to content

Commit 2e19a75

Browse files
committed
Merge branch 'master' into specs-option
2 parents 7dbd8c0 + 3e4181e commit 2e19a75

File tree

4 files changed

+269
-32
lines changed

4 files changed

+269
-32
lines changed

bin/commands/runs.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = function run(args) {
1717
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1818
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
1919

20-
// accept the username from command line if provided
20+
// accept the username from command line or env variable if provided
2121
utils.setUsername(bsConfig, args);
2222

23-
// accept the access key from command line if provided
23+
// accept the access key from command line or env variable if provided
2424
utils.setAccessKey(bsConfig, args);
2525

2626
// accept the build name from command line if provided
@@ -32,6 +32,12 @@ module.exports = function run(args) {
3232
// accept the env list from command line and set it
3333
utils.setTestEnvs(bsConfig, args);
3434

35+
//accept the local from env variable if provided
36+
utils.setLocal(bsConfig);
37+
38+
//accept the local identifier from env variable if provided
39+
utils.setLocalIdentifier(bsConfig);
40+
3541
// Validate browserstack.json values and parallels specified via arguments
3642
return capabilityHelper.validate(bsConfig, args).then(function (validated) {
3743
logger.info(validated);
@@ -54,8 +60,8 @@ module.exports = function run(args) {
5460
logger.warn(Constants.userMessages.NO_PARALLELS);
5561
}
5662

57-
if(!args.disableNpmWarning && bsConfig.run_settings.npm_dependencies && Object.keys(bsConfig.run_settings.npm_dependencies).length <= 0) logger.warn(Constants.userMessages.NO_NPM_DEPENDENCIES);
58-
63+
if (!args.disableNpmWarning && bsConfig.run_settings.npm_dependencies && Object.keys(bsConfig.run_settings.npm_dependencies).length <= 0) logger.warn(Constants.userMessages.NO_NPM_DEPENDENCIES);
64+
5965
logger.info(message);
6066
logger.info(dashboardLink);
6167
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null);

bin/helpers/utils.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ const os = require("os");
33
const path = require("path");
44
const fs = require("fs");
55

6-
const usageReporting = require('./usageReporting'),
6+
const usageReporting = require('./usageReporting'),
77
logger = require('./logger').winstonLogger,
88
Constants = require('./constants');
99

1010
exports.validateBstackJson = (bsConfigPath) => {
11-
return new Promise(function(resolve, reject){
11+
return new Promise(function (resolve, reject) {
1212
try {
1313
logger.info(`Reading config from ${bsConfigPath}`);
1414
let bsConfig = require(bsConfigPath);
1515
resolve(bsConfig);
1616
}
1717
catch (e) {
18-
reject("Couldn't find the browserstack.json file at \""+ bsConfigPath +"\". Please use --config-file <path to browserstack.json>.");
18+
reject("Couldn't find the browserstack.json file at \"" + bsConfigPath + "\". Please use --config-file <path to browserstack.json>.");
1919
}
2020
});
2121
}
@@ -51,7 +51,7 @@ exports.getErrorCodeFromMsg = (errMsg) => {
5151
errorCode = "invalid_directory_structure";
5252
break;
5353
}
54-
if(errMsg.includes("Please use --config-file <path to browserstack.json>.")){
54+
if (errMsg.includes("Please use --config-file <path to browserstack.json>.")) {
5555
errorCode = "bstack_json_path_invalid";
5656
}
5757
return errorCode;
@@ -96,12 +96,18 @@ exports.setParallels = (bsConfig, args) => {
9696
exports.setUsername = (bsConfig, args) => {
9797
if (!this.isUndefined(args.username)) {
9898
bsConfig['auth']['username'] = args.username;
99+
} else if (!this.isUndefined(process.env.BROWSERSTACK_USERNAME)) {
100+
bsConfig['auth']['username'] = process.env.BROWSERSTACK_USERNAME;
101+
logger.info("Reading username from the environment variable BROWSERSTACK_USERNAME");
99102
}
100103
}
101104

102105
exports.setAccessKey = (bsConfig, args) => {
103106
if (!this.isUndefined(args.key)) {
104107
bsConfig['auth']['access_key'] = args.key;
108+
} else if (!this.isUndefined(process.env.BROWSERSTACK_ACCESS_KEY)) {
109+
bsConfig['auth']['access_key'] = process.env.BROWSERSTACK_ACCESS_KEY;
110+
logger.info("Reading access key from the environment variable BROWSERSTACK_ACCESS_KEY");
105111
}
106112
}
107113

@@ -146,14 +152,14 @@ exports.isUndefined = value => (value === undefined || value === null);
146152
exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0);
147153

148154
exports.isParallelValid = (value) => {
149-
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1 ) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE;
155+
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE;
150156
}
151157

152158
exports.getUserAgent = () => {
153159
return `BStack-Cypress-CLI/1.3.0 (${os.arch()}/${os.platform()}/${os.release()})`;
154160
}
155161

156-
exports.isAbsolute = (configPath) => {
162+
exports.isAbsolute = (configPath) => {
157163
return path.isAbsolute(configPath)
158164
}
159165

@@ -168,25 +174,25 @@ exports.configCreated = (args) => {
168174
}
169175

170176
exports.exportResults = (buildId, buildUrl) => {
171-
let data = "BUILD_ID=" + buildId + "\nBUILD_URL="+buildUrl;
172-
fs.writeFileSync("log/build_results.txt", data , function(err){
173-
if(err) {
177+
let data = "BUILD_ID=" + buildId + "\nBUILD_URL=" + buildUrl;
178+
fs.writeFileSync("log/build_results.txt", data, function (err) {
179+
if (err) {
174180
logger.warn(`Couldn't write BUILD_ID with value: ${buildId} to browserstack/build_results.txt`);
175181
logger.warn(`Couldn't write BUILD_URL with value: ${buildUrl} to browserstack/build_results.txt`);
176182
}
177183
});
178184
}
179185

180186
exports.deleteResults = () => {
181-
fs.unlink("log/build_results.txt", function (err){
187+
fs.unlink("log/build_results.txt", function (err) {
182188
});
183189
}
184190

185191
exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
186192
// Getting absolute path
187193
cypressDir = path.resolve(cypressDir);
188194
cypressProjDir = path.resolve(cypressProjDir);
189-
if(cypressProjDir === cypressDir) return true;
195+
if (cypressProjDir === cypressDir) return true;
190196
let parentTokens = cypressDir.split('/').filter(i => i.length);
191197
let childTokens = cypressProjDir.split('/').filter(i => i.length);
192198
return parentTokens.every((t, i) => childTokens[i] === t);
@@ -195,3 +201,19 @@ exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
195201
exports.getLocalFlag = (connectionSettings) => {
196202
return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local
197203
}
204+
205+
exports.setLocal = (bsConfig) => {
206+
if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL)) {
207+
let local = false;
208+
if (String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true;
209+
bsConfig['connection_settings']['local'] = local;
210+
logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL");
211+
}
212+
}
213+
214+
exports.setLocalIdentifier = (bsConfig) => {
215+
if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER)) {
216+
bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
217+
logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER");
218+
}
219+
}

test/unit/bin/commands/runs.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe("runs", () => {
5151
},
5252
});
5353

54-
validateBstackJsonStub.returns(Promise.reject({message: "random-error"}));
54+
validateBstackJsonStub.returns(Promise.reject({ message: "random-error" }));
5555

5656
return runs(args)
5757
.then(function (_bsConfig) {
@@ -94,6 +94,8 @@ describe("runs", () => {
9494
});
9595
getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code");
9696
capabilityValidatorStub = sandbox.stub();
97+
setLocalStub = sandbox.stub();
98+
setLocalIdentifierStub = sandbox.stub();
9799
deleteResultsStub = sandbox.stub();
98100
});
99101

@@ -119,6 +121,8 @@ describe("runs", () => {
119121
setUserSpecs: setUserSpecsStub,
120122
setTestEnvs: setTestEnvsStub,
121123
getConfigPath: getConfigPathStub,
124+
setLocal: setLocalStub,
125+
setLocalIdentifier: setLocalIdentifierStub,
122126
deleteResults: deleteResultsStub
123127
},
124128
"../helpers/capabilityHelper": {
@@ -140,6 +144,8 @@ describe("runs", () => {
140144
sinon.assert.calledOnce(capabilityValidatorStub);
141145
sinon.assert.calledOnce(setUsageReportingFlagStub);
142146
sinon.assert.calledOnce(getErrorCodeFromMsgStub);
147+
sinon.assert.calledOnce(setLocalStub);
148+
sinon.assert.calledOnce(setLocalIdentifierStub);
143149
sinon.assert.calledOnce(deleteResultsStub);
144150
sinon.assert.calledOnceWithExactly(
145151
sendUsageReportStub,
@@ -173,6 +179,8 @@ describe("runs", () => {
173179
capabilityValidatorStub = sandbox.stub();
174180
archiverStub = sandbox.stub();
175181
deleteZipStub = sandbox.stub();
182+
setLocalStub = sandbox.stub();
183+
setLocalIdentifierStub = sandbox.stub();
176184
deleteResultsStub = sandbox.stub();
177185
});
178186

@@ -198,6 +206,8 @@ describe("runs", () => {
198206
setTestEnvs: setTestEnvsStub,
199207
setUsageReportingFlag: setUsageReportingFlagStub,
200208
getConfigPath: getConfigPathStub,
209+
setLocal: setLocalStub,
210+
setLocalIdentifier: setLocalIdentifierStub,
201211
deleteResults: deleteResultsStub
202212
},
203213
"../helpers/capabilityHelper": {
@@ -222,7 +232,9 @@ describe("runs", () => {
222232
.catch((error) => {
223233
sinon.assert.calledOnce(getConfigPathStub);
224234
sinon.assert.calledOnce(getConfigPathStub);
225-
sinon.assert.calledOnce(setParallelsStub)
235+
sinon.assert.calledOnce(setParallelsStub);
236+
sinon.assert.calledOnce(setLocalStub);
237+
sinon.assert.calledOnce(setLocalIdentifierStub);
226238
sinon.assert.calledOnce(validateBstackJsonStub);
227239
sinon.assert.calledOnce(capabilityValidatorStub);
228240
sinon.assert.calledOnce(archiverStub);
@@ -262,6 +274,8 @@ describe("runs", () => {
262274
archiverStub = sandbox.stub();
263275
zipUploadStub = sandbox.stub();
264276
deleteZipStub = sandbox.stub();
277+
setLocalStub = sandbox.stub();
278+
setLocalIdentifierStub = sandbox.stub();
265279
deleteResultsStub = sandbox.stub();
266280
});
267281

@@ -287,6 +301,8 @@ describe("runs", () => {
287301
setTestEnvs: setTestEnvsStub,
288302
setUsageReportingFlag: setUsageReportingFlagStub,
289303
getConfigPath: getConfigPathStub,
304+
setLocal: setLocalStub,
305+
setLocalIdentifier: setLocalIdentifierStub,
290306
deleteResults: deleteResultsStub
291307
},
292308
"../helpers/capabilityHelper": {
@@ -316,6 +332,8 @@ describe("runs", () => {
316332
sinon.assert.calledOnce(getConfigPathStub);
317333
sinon.assert.calledOnce(getConfigPathStub);
318334
sinon.assert.calledOnce(setParallelsStub);
335+
sinon.assert.calledOnce(setLocalStub);
336+
sinon.assert.calledOnce(setLocalIdentifierStub);
319337
sinon.assert.calledOnce(validateBstackJsonStub);
320338
sinon.assert.calledOnce(capabilityValidatorStub);
321339
sinon.assert.calledOnce(archiverStub);
@@ -359,6 +377,8 @@ describe("runs", () => {
359377
zipUploadStub = sandbox.stub();
360378
createBuildStub = sandbox.stub();
361379
deleteZipStub = sandbox.stub();
380+
setLocalStub = sandbox.stub();
381+
setLocalIdentifierStub = sandbox.stub();
362382
deleteResultsStub = sandbox.stub();
363383
});
364384

@@ -384,6 +404,8 @@ describe("runs", () => {
384404
setTestEnvs: setTestEnvsStub,
385405
setUsageReportingFlag: setUsageReportingFlagStub,
386406
getConfigPath: getConfigPathStub,
407+
setLocal: setLocalStub,
408+
setLocalIdentifier: setLocalIdentifierStub,
387409
deleteResults: deleteResultsStub
388410
},
389411
"../helpers/capabilityHelper": {
@@ -421,6 +443,8 @@ describe("runs", () => {
421443
sinon.assert.calledOnce(validateBstackJsonStub);
422444
sinon.assert.calledOnce(capabilityValidatorStub);
423445
sinon.assert.calledOnce(setParallelsStub);
446+
sinon.assert.calledOnce(setLocalStub);
447+
sinon.assert.calledOnce(setLocalIdentifierStub);
424448
sinon.assert.calledOnce(archiverStub);
425449
sinon.assert.calledOnce(setUsageReportingFlagStub);
426450
sinon.assert.calledOnce(zipUploadStub);
@@ -471,6 +495,8 @@ describe("runs", () => {
471495
exportResultsStub = sandbox.stub();
472496
deleteResultsStub = sandbox.stub();
473497
isUndefinedStub = sandbox.stub();
498+
setLocalStub = sandbox.stub();
499+
setLocalIdentifierStub = sandbox.stub();
474500
});
475501

476502
afterEach(() => {
@@ -496,6 +522,8 @@ describe("runs", () => {
496522
setUsageReportingFlag: setUsageReportingFlagStub,
497523
setParallels: setParallelsStub,
498524
getConfigPath: getConfigPathStub,
525+
setLocal: setLocalStub,
526+
setLocalIdentifier: setLocalIdentifierStub,
499527
exportResults: exportResultsStub,
500528
deleteResults: deleteResultsStub,
501529
isUndefined: isUndefinedStub
@@ -538,6 +566,8 @@ describe("runs", () => {
538566
sinon.assert.calledOnce(validateBstackJsonStub);
539567
sinon.assert.calledOnce(capabilityValidatorStub);
540568
sinon.assert.calledOnce(setParallelsStub);
569+
sinon.assert.calledOnce(setLocalStub);
570+
sinon.assert.calledOnce(setLocalIdentifierStub);
541571
sinon.assert.calledOnce(archiverStub);
542572
sinon.assert.calledOnce(setUsageReportingFlagStub);
543573
sinon.assert.calledOnce(zipUploadStub);

0 commit comments

Comments
 (0)