Skip to content

Commit f5f057a

Browse files
Environment variables support for better CI/CD integration
1 parent 22c892e commit f5f057a

File tree

4 files changed

+224
-8
lines changed

4 files changed

+224
-8
lines changed

bin/commands/runs.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ module.exports = function run(args) {
1515
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1616
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
1717

18-
// accept the username from command line if provided
18+
// accept the username from command line or env variable if provided
1919
utils.setUsername(bsConfig, args);
2020

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

2424
// accept the build name from command line if provided
2525
utils.setBuildName(bsConfig, args);
2626

27+
//accept the local from env variable if provided
28+
utils.setLocal(bsConfig);
29+
30+
//accept the local identifier from env variable if provided
31+
utils.setLocalIdentifier(bsConfig);
32+
2733
// Validate browserstack.json values and parallels specified via arguments
2834
return capabilityHelper.validate(bsConfig, args).then(function (validated) {
2935
logger.info(validated);

bin/helpers/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ exports.setParallels = (bsConfig, args) => {
9595
exports.setUsername = (bsConfig, args) => {
9696
if (!this.isUndefined(args.username)) {
9797
bsConfig['auth']['username'] = args.username;
98+
}else if(!this.isUndefined(process.env.BROWSERSTACK_USERNAME)){
99+
bsConfig['auth']['username'] = process.env.BROWSERSTACK_USERNAME;
100+
logger.info("Reading username from the environment variable BROWSERSTACK_USERNAME");
98101
}
99102
}
100103

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

@@ -149,3 +155,19 @@ exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
149155
exports.getLocalFlag = (connectionSettings) => {
150156
return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local
151157
}
158+
159+
exports.setLocal = (bsConfig) => {
160+
if(!this.isUndefined(process.env.BROWSERSTACK_LOCAL) && !this.isUndefined(bsConfig.connection_settings)){
161+
let local = false;
162+
if(String(process.env.BROWSERSTACK_LOCAL).toLowerCase() === "true") local = true;
163+
bsConfig['connection_settings']['local'] = local;
164+
logger.info("Reading local setting from the environment variable BROWSERSTACK_LOCAL");
165+
}
166+
}
167+
168+
exports.setLocalIdentifier = (bsConfig) => {
169+
if (!this.isUndefined(process.env.BROWSERSTACK_LOCAL_IDENTIFIER) && !this.isUndefined(bsConfig.connection_settings)){
170+
bsConfig['connection_settings']['local_identifier'] = process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
171+
logger.info("Reading local identifier from the environment variable BROWSERSTACK_LOCAL_IDENTIFIER");
172+
}
173+
}

test/unit/bin/commands/runs.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ describe("runs", () => {
8989
});
9090
getErrorCodeFromMsgStub = sandbox.stub().returns("random-error-code");
9191
capabilityValidatorStub = sandbox.stub();
92+
setLocalStub = sandbox.stub();
93+
setLocalIdentifierStub = sandbox.stub();
9294
});
9395

9496
afterEach(() => {
@@ -110,7 +112,9 @@ describe("runs", () => {
110112
setUsername: setUsernameStub,
111113
setAccessKey: setAccessKeyStub,
112114
setBuildName: setBuildNameStub,
113-
getConfigPath: getConfigPathStub
115+
getConfigPath: getConfigPathStub,
116+
setLocal: setLocalStub,
117+
setLocalIdentifier: setLocalIdentifierStub
114118
},
115119
"../helpers/capabilityHelper": {
116120
validate: capabilityValidatorStub,
@@ -131,6 +135,8 @@ describe("runs", () => {
131135
sinon.assert.calledOnce(capabilityValidatorStub);
132136
sinon.assert.calledOnce(setUsageReportingFlagStub);
133137
sinon.assert.calledOnce(getErrorCodeFromMsgStub);
138+
sinon.assert.calledOnce(setLocalStub);
139+
sinon.assert.calledOnce(setLocalIdentifierStub);
134140
sinon.assert.calledOnceWithExactly(
135141
sendUsageReportStub,
136142
bsConfig,
@@ -161,6 +167,8 @@ describe("runs", () => {
161167
capabilityValidatorStub = sandbox.stub();
162168
archiverStub = sandbox.stub();
163169
deleteZipStub = sandbox.stub();
170+
setLocalStub = sandbox.stub();
171+
setLocalIdentifierStub = sandbox.stub();
164172
});
165173

166174
afterEach(() => {
@@ -182,7 +190,9 @@ describe("runs", () => {
182190
setAccessKey: setAccessKeyStub,
183191
setBuildName: setBuildNameStub,
184192
setUsageReportingFlag: setUsageReportingFlagStub,
185-
getConfigPath: getConfigPathStub
193+
getConfigPath: getConfigPathStub,
194+
setLocal: setLocalStub,
195+
setLocalIdentifier: setLocalIdentifierStub
186196
},
187197
"../helpers/capabilityHelper": {
188198
validate: capabilityValidatorStub,
@@ -206,7 +216,9 @@ describe("runs", () => {
206216
.catch((error) => {
207217
sinon.assert.calledOnce(getConfigPathStub);
208218
sinon.assert.calledOnce(getConfigPathStub);
209-
sinon.assert.calledOnce(setParallelsStub)
219+
sinon.assert.calledOnce(setParallelsStub);
220+
sinon.assert.calledOnce(setLocalStub);
221+
sinon.assert.calledOnce(setLocalIdentifierStub);
210222
sinon.assert.calledOnce(validateBstackJsonStub);
211223
sinon.assert.calledOnce(capabilityValidatorStub);
212224
sinon.assert.calledOnce(archiverStub);
@@ -243,6 +255,8 @@ describe("runs", () => {
243255
archiverStub = sandbox.stub();
244256
zipUploadStub = sandbox.stub();
245257
deleteZipStub = sandbox.stub();
258+
setLocalStub = sandbox.stub();
259+
setLocalIdentifierStub = sandbox.stub();
246260
});
247261

248262
afterEach(() => {
@@ -264,7 +278,9 @@ describe("runs", () => {
264278
setAccessKey: setAccessKeyStub,
265279
setBuildName: setBuildNameStub,
266280
setUsageReportingFlag: setUsageReportingFlagStub,
267-
getConfigPath: getConfigPathStub
281+
getConfigPath: getConfigPathStub,
282+
setLocal: setLocalStub,
283+
setLocalIdentifier: setLocalIdentifierStub
268284
},
269285
"../helpers/capabilityHelper": {
270286
validate: capabilityValidatorStub,
@@ -293,6 +309,8 @@ describe("runs", () => {
293309
sinon.assert.calledOnce(getConfigPathStub);
294310
sinon.assert.calledOnce(getConfigPathStub);
295311
sinon.assert.calledOnce(setParallelsStub);
312+
sinon.assert.calledOnce(setLocalStub);
313+
sinon.assert.calledOnce(setLocalIdentifierStub);
296314
sinon.assert.calledOnce(validateBstackJsonStub);
297315
sinon.assert.calledOnce(capabilityValidatorStub);
298316
sinon.assert.calledOnce(archiverStub);
@@ -334,6 +352,8 @@ describe("runs", () => {
334352
zipUploadStub = sandbox.stub();
335353
createBuildStub = sandbox.stub();
336354
deleteZipStub = sandbox.stub();
355+
setLocalStub = sandbox.stub();
356+
setLocalIdentifierStub = sandbox.stub();
337357
});
338358

339359
afterEach(() => {
@@ -355,7 +375,9 @@ describe("runs", () => {
355375
setAccessKey: setAccessKeyStub,
356376
setBuildName: setBuildNameStub,
357377
setUsageReportingFlag: setUsageReportingFlagStub,
358-
getConfigPath: getConfigPathStub
378+
getConfigPath: getConfigPathStub,
379+
setLocal: setLocalStub,
380+
setLocalIdentifier: setLocalIdentifierStub
359381
},
360382
"../helpers/capabilityHelper": {
361383
validate: capabilityValidatorStub,
@@ -392,6 +414,8 @@ describe("runs", () => {
392414
sinon.assert.calledOnce(validateBstackJsonStub);
393415
sinon.assert.calledOnce(capabilityValidatorStub);
394416
sinon.assert.calledOnce(setParallelsStub);
417+
sinon.assert.calledOnce(setLocalStub);
418+
sinon.assert.calledOnce(setLocalIdentifierStub);
395419
sinon.assert.calledOnce(archiverStub);
396420
sinon.assert.calledOnce(setUsageReportingFlagStub);
397421
sinon.assert.calledOnce(zipUploadStub);
@@ -437,6 +461,8 @@ describe("runs", () => {
437461
createBuildStub = sandbox.stub();
438462
deleteZipStub = sandbox.stub();
439463
isUndefinedStub = sandbox.stub();
464+
setLocalStub = sandbox.stub();
465+
setLocalIdentifierStub = sandbox.stub();
440466
});
441467

442468
afterEach(() => {
@@ -460,7 +486,9 @@ describe("runs", () => {
460486
setUsageReportingFlag: setUsageReportingFlagStub,
461487
setParallels: setParallelsStub,
462488
getConfigPath: getConfigPathStub,
463-
isUndefined: isUndefinedStub
489+
isUndefined: isUndefinedStub,
490+
setLocal: setLocalStub,
491+
setLocalIdentifier: setLocalIdentifierStub
464492
},
465493
"../helpers/capabilityHelper": {
466494
validate: capabilityValidatorStub,
@@ -500,6 +528,8 @@ describe("runs", () => {
500528
sinon.assert.calledOnce(validateBstackJsonStub);
501529
sinon.assert.calledOnce(capabilityValidatorStub);
502530
sinon.assert.calledOnce(setParallelsStub);
531+
sinon.assert.calledOnce(setLocalStub);
532+
sinon.assert.calledOnce(setLocalIdentifierStub);
503533
sinon.assert.calledOnce(archiverStub);
504534
sinon.assert.calledOnce(setUsageReportingFlagStub);
505535
sinon.assert.calledOnce(zipUploadStub);

test/unit/bin/helpers/utils.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,162 @@ describe("utils", () => {
273273
expect(utils.getLocalFlag({"local": true})).to.be.true;
274274
});
275275
});
276+
277+
describe("setLocal", () => {
278+
beforeEach(function () {
279+
delete process.env.BROWSERSTACK_LOCAL;
280+
});
281+
282+
afterEach(function () {
283+
delete process.env.BROWSERSTACK_LOCAL;
284+
});
285+
286+
it("should not change local in bsConfig if process.env.BROWSERSTACK_LOCAL is undefined", () => {
287+
let bsConfig = {
288+
connection_settings: {
289+
local: true
290+
}
291+
}
292+
utils.setLocal(bsConfig);
293+
expect(bsConfig.connection_settings.local).to.be.eq(true);
294+
});
295+
296+
it("should change local to false in bsConfig if process.env.BROWSERSTACK_LOCAL is set to false", () => {
297+
let bsConfig = {
298+
connection_settings: {
299+
local: true
300+
}
301+
}
302+
process.env.BROWSERSTACK_LOCAL = false;
303+
utils.setLocal(bsConfig);
304+
expect(bsConfig.connection_settings.local).to.be.eq(false);
305+
});
306+
307+
it("should change local to true in bsConfig if process.env.BROWSERSTACK_LOCAL is set to true", () => {
308+
let bsConfig = {
309+
connection_settings: {
310+
local: false
311+
}
312+
}
313+
process.env.BROWSERSTACK_LOCAL = true;
314+
utils.setLocal(bsConfig);
315+
expect(bsConfig.connection_settings.local).to.be.eq(true);
316+
});
317+
});
318+
319+
describe("setLocalIdentifier", () => {
320+
beforeEach(function () {
321+
delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
322+
});
323+
324+
afterEach(function () {
325+
delete process.env.BROWSERSTACK_LOCAL_IDENTIFIER;
326+
});
327+
it("should not change local identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is undefined", () => {
328+
let bsConfig = {
329+
connection_settings: {
330+
local_identifier: "local_identifier"
331+
}
332+
}
333+
utils.setLocalIdentifier(bsConfig);
334+
expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier");
335+
});
336+
337+
it("should change local identifier to local_identifier in bsConfig if process.env.BROWSERSTACK_LOCAL_IDENTIFIER is set to local_identifier", () => {
338+
let bsConfig = {
339+
connection_settings: {
340+
local_identifier: "test"
341+
}
342+
}
343+
process.env.BROWSERSTACK_LOCAL_IDENTIFIER = "local_identifier";
344+
utils.setLocalIdentifier(bsConfig);
345+
expect(bsConfig.connection_settings.local_identifier).to.be.eq("local_identifier");
346+
});
347+
348+
});
349+
350+
describe("setUsername", () => {
351+
352+
beforeEach(function () {
353+
delete process.env.BROWSERSTACK_USERNAME;
354+
});
355+
356+
afterEach(function () {
357+
delete process.env.BROWSERSTACK_USERNAME;
358+
});
359+
360+
it("should set username if args.username is present", () =>{
361+
let bsConfig = {
362+
auth: {
363+
username: "test"
364+
}
365+
}
366+
utils.setUsername(bsConfig,{username: "username"});
367+
expect(bsConfig.auth.username).to.be.eq("username");
368+
});
369+
370+
it("should set username if process.env.BROWSERSTACK_USERNAME is present and args.username is not present", () =>{
371+
let bsConfig = {
372+
auth: {
373+
username: "test"
374+
}
375+
}
376+
process.env.BROWSERSTACK_USERNAME = "username"
377+
utils.setUsername(bsConfig,{});
378+
expect(bsConfig.auth.username).to.be.eq("username");
379+
});
380+
381+
it("should set username to default if process.env.BROWSERSTACK_USERNAME and args.username is not present", () =>{
382+
let bsConfig = {
383+
auth: {
384+
username: "test"
385+
}
386+
}
387+
utils.setUsername(bsConfig,{});
388+
expect(bsConfig.auth.username).to.be.eq("test");
389+
});
390+
391+
});
392+
393+
describe("setAccessKey", () => {
394+
beforeEach(function () {
395+
delete process.env.BROWSERSTACK_ACCESS_KEY;
396+
});
397+
398+
afterEach(function () {
399+
delete process.env.BROWSERSTACK_ACCESS_KEY;
400+
});
401+
402+
it("should set access_key if args.key is present", () =>{
403+
let bsConfig = {
404+
auth: {
405+
access_key: "test"
406+
}
407+
}
408+
utils.setAccessKey(bsConfig,{key: "access_key"});
409+
expect(bsConfig.auth.access_key).to.be.eq("access_key");
410+
});
411+
412+
it("should set access_key if process.env.BROWSERSTACK_ACCESS_KEY is present and args.access_key is not present", () =>{
413+
let bsConfig = {
414+
auth: {
415+
access_key: "test"
416+
}
417+
}
418+
process.env.BROWSERSTACK_ACCESS_KEY = "access_key"
419+
utils.setAccessKey(bsConfig,{});
420+
expect(bsConfig.auth.access_key).to.be.eq("access_key");
421+
});
422+
423+
it("should set access_key to default if process.env.BROWSERSTACK_ACCESS_KEY and args.access_key is not present", () =>{
424+
let bsConfig = {
425+
auth: {
426+
access_key: "test"
427+
}
428+
}
429+
utils.setAccessKey(bsConfig,{});
430+
expect(bsConfig.auth.access_key).to.be.eq("test");
431+
});
432+
433+
});
276434
});

0 commit comments

Comments
 (0)