Skip to content

Commit d6a6a01

Browse files
Merge pull request #147 from browserstack/ACE_2714_env
Support for env variables using different ways
2 parents 0040ad8 + a2e8a4f commit d6a6a01

File tree

7 files changed

+173
-43
lines changed

7 files changed

+173
-43
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ module.exports = function run(args) {
4747
// accept the env list from command line and set it
4848
utils.setTestEnvs(bsConfig, args);
4949

50+
// accept the system env list from bsconf and set it
51+
utils.setSystemEnvs(bsConfig);
52+
5053
//accept the local from env variable if provided
5154
utils.setLocal(bsConfig, args);
5255

bin/helpers/capabilityHelper.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,18 @@ const caps = (bsConfig, zip) => {
9393
obj.projectNotifyURL = null;
9494

9595
if (bsConfig.run_settings) {
96-
obj.project = bsConfig.run_settings.project || bsConfig.run_settings.project_name;
97-
obj.customBuildName = bsConfig.run_settings.build_name || bsConfig.run_settings.customBuildName;
96+
obj.project = bsConfig.run_settings.project || bsConfig.run_settings.project_name || obj.project;
97+
obj.customBuildName = bsConfig.run_settings.build_name || bsConfig.run_settings.customBuildName || obj.customBuildName;
9898
obj.callbackURL = bsConfig.run_settings.callback_url;
9999
obj.projectNotifyURL = bsConfig.run_settings.project_notify_URL;
100100
obj.parallels = bsConfig.run_settings.parallels;
101101

102-
if (!Utils.isUndefined(bsConfig.run_settings.cypress_config_filename)) {
103-
obj.cypress_config_filename = bsConfig.run_settings.cypress_config_filename;
104-
}
105-
106-
if (!Utils.isUndefined(bsConfig.run_settings.specs)){
107-
obj.specs = bsConfig.run_settings.specs;
108-
}
109-
110-
if (!Utils.isUndefined(bsConfig.run_settings.env)){
111-
obj.env = bsConfig.run_settings.env;
112-
}
113-
if (!Utils.isUndefined(bsConfig.run_settings.cypress_version)){
114-
obj.cypress_version = bsConfig.run_settings.cypress_version;
115-
}
116-
117-
if (!Utils.isUndefined(bsConfig.run_settings.headless) && String(bsConfig.run_settings.headless) === "false"){
118-
obj.headless = bsConfig.run_settings.headless;
119-
} else {
102+
if (!(!Utils.isUndefined(bsConfig.run_settings.headless) && String(bsConfig.run_settings.headless) === "false")) {
120103
logger.info(`Running your tests in headless mode. Use --headed arg to run in headful mode.`);
121104
}
105+
106+
// send run_settings as is for other capabilities
107+
obj.run_settings = JSON.stringify(bsConfig.run_settings);
122108
}
123109

124110
if(obj.parallels === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined

bin/helpers/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ const messageTypes = {
132132
NULL: null
133133
}
134134

135-
const allowedFileTypes = ['js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf', 'jpg', 'jpeg', 'png', 'zip', 'npmrc', 'xml', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'jsx', 'coffee', 'cjsx', 'csv', 'tsv', 'yml', 'yaml'];
135+
const allowedFileTypes = ['js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf', 'jpg', 'jpeg', 'png', 'zip', 'npmrc', 'xml', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'jsx', 'coffee', 'cjsx', 'csv', 'tsv', 'yml', 'yaml', 'env'];
136136

137137
const filesToIgnoreWhileUploading = [
138138
'**/node_modules/**',

bin/helpers/utils.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,13 +268,61 @@ exports.setUserSpecs = (bsConfig, args) => {
268268
}
269269
}
270270

271-
// env option must be set only from command line args as a string
272271
exports.setTestEnvs = (bsConfig, args) => {
272+
let envKeys = {};
273+
274+
if(bsConfig.run_settings.env && Object.keys(bsConfig.run_settings.env).length !== 0) {
275+
envKeys = bsConfig.run_settings.env;
276+
}
277+
278+
// set env vars passed from command line args as a string
273279
if (!this.isUndefined(args.env)) {
274-
bsConfig.run_settings.env = this.fixCommaSeparatedString(args.env);
275-
} else {
280+
let argsEnvVars = this.fixCommaSeparatedString(args.env).split(',');
281+
argsEnvVars.forEach((envVar) => {
282+
let env = envVar.split("=");
283+
envKeys[env[0]] = env.slice(1,).join('=');
284+
});
285+
}
286+
287+
if (Object.keys(envKeys).length === 0) {
276288
bsConfig.run_settings.env = null;
289+
} else {
290+
bsConfig.run_settings.env = Object.keys(envKeys).map(key => (`${key}=${envKeys[key]}`)).join(',');
291+
}
292+
}
293+
294+
exports.setSystemEnvs = (bsConfig) => {
295+
let envKeys = {};
296+
297+
// set env vars which are defined in system_env_vars key
298+
if(!this.isUndefined(bsConfig.run_settings.system_env_vars) && Array.isArray(bsConfig.run_settings.system_env_vars) && bsConfig.run_settings.system_env_vars.length) {
299+
let systemEnvVars = bsConfig.run_settings.system_env_vars;
300+
systemEnvVars.forEach((envVar) => {
301+
envKeys[envVar] = process.env[envVar];
302+
});
303+
}
304+
305+
// set env vars which start with CYPRESS_ and cypress_
306+
let pattern = /^cypress_/i;
307+
let matchingKeys = this.getKeysMatchingPattern(process.env, pattern);
308+
if (matchingKeys && matchingKeys.length) {
309+
matchingKeys.forEach((envVar) => {
310+
envKeys[envVar] = process.env[envVar];
311+
});
277312
}
313+
314+
if (Object.keys(envKeys).length === 0) {
315+
bsConfig.run_settings.system_env_vars = null;
316+
} else {
317+
bsConfig.run_settings.system_env_vars = Object.keys(envKeys).map(key => (`${key}=${envKeys[key]}`));
318+
}
319+
}
320+
321+
exports.getKeysMatchingPattern = (obj, pattern) => {
322+
let matchingKeys = Object.keys(obj).filter(function(key) {
323+
return pattern.test(key);
324+
});
325+
return matchingKeys;
278326
}
279327

280328
exports.fixCommaSeparatedString = (string) => {

test/unit/bin/commands/runs.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ describe("runs", () => {
9191
setCypressConfigFilenameStub = sandbox.stub();
9292
setUserSpecsStub = sandbox.stub();
9393
setTestEnvsStub = sandbox.stub();
94+
setSystemEnvsStub = sandbox.stub();
9495
getConfigPathStub = sandbox.stub();
9596
setupLocalTestingStub = sandbox.stub();
9697
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
@@ -141,7 +142,8 @@ describe("runs", () => {
141142
setupLocalTesting: setupLocalTestingStub,
142143
isJSONInvalid: isJSONInvalidStub,
143144
setLocalMode: setLocalModeStub,
144-
setLocalConfigFile: setLocalConfigFileStub
145+
setLocalConfigFile: setLocalConfigFileStub,
146+
setSystemEnvs: setSystemEnvsStub
145147
},
146148
'../helpers/capabilityHelper': {
147149
validate: capabilityValidatorStub
@@ -168,6 +170,7 @@ describe("runs", () => {
168170
sinon.assert.calledOnce(setCypressConfigFilenameStub);
169171
sinon.assert.calledOnce(setUserSpecsStub);
170172
sinon.assert.calledOnce(setTestEnvsStub);
173+
sinon.assert.calledOnce(setSystemEnvsStub);
171174
sinon.assert.calledOnce(setLocalStub);
172175
sinon.assert.calledOnce(setLocalModeStub);
173176
sinon.assert.calledOnce(setLocalConfigFileStub);
@@ -203,6 +206,7 @@ describe("runs", () => {
203206
setCypressConfigFilenameStub = sandbox.stub();
204207
setUserSpecsStub = sandbox.stub();
205208
setTestEnvsStub = sandbox.stub();
209+
setSystemEnvsStub = sandbox.stub();
206210
validateBstackJsonStub = sandbox.stub();
207211
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
208212
checkUploadedStub = sandbox.stub();
@@ -257,7 +261,8 @@ describe("runs", () => {
257261
deleteResults: deleteResultsStub,
258262
setDefaults: setDefaultsStub,
259263
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
260-
setLocalConfigFile: setLocalConfigFileStub
264+
setLocalConfigFile: setLocalConfigFileStub,
265+
setSystemEnvs: setSystemEnvsStub
261266
},
262267
'../helpers/capabilityHelper': {
263268
validate: capabilityValidatorStub,
@@ -306,6 +311,7 @@ describe("runs", () => {
306311
sinon.assert.calledOnce(deleteZipStub);
307312
sinon.assert.calledOnce(deleteResultsStub);
308313
sinon.assert.calledOnce(setDefaultsStub);
314+
sinon.assert.calledOnce(setSystemEnvsStub);
309315
sinon.assert.calledOnceWithExactly(
310316
sendUsageReportStub,
311317
bsConfig,
@@ -332,6 +338,7 @@ describe("runs", () => {
332338
setCypressConfigFilenameStub = sandbox.stub();
333339
setUserSpecsStub = sandbox.stub();
334340
setTestEnvsStub = sandbox.stub();
341+
setSystemEnvsStub = sandbox.stub();
335342
getConfigPathStub = sandbox.stub();
336343
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
337344
checkUploadedStub = sandbox.stub();
@@ -376,6 +383,7 @@ describe("runs", () => {
376383
setCypressConfigFilename: setCypressConfigFilenameStub,
377384
setUserSpecs: setUserSpecsStub,
378385
setTestEnvs: setTestEnvsStub,
386+
setSystemEnvs: setSystemEnvsStub,
379387
setUsageReportingFlag: setUsageReportingFlagStub,
380388
getConfigPath: getConfigPathStub,
381389
setLocal: setLocalStub,
@@ -436,6 +444,7 @@ describe("runs", () => {
436444
sinon.assert.calledOnce(zipUploadStub);
437445
sinon.assert.calledOnce(deleteResultsStub);
438446
sinon.assert.calledOnce(setDefaultsStub);
447+
sinon.assert.calledOnce(setSystemEnvsStub);
439448
sinon.assert.calledOnceWithExactly(
440449
sendUsageReportStub,
441450
bsConfig,
@@ -465,6 +474,7 @@ describe("runs", () => {
465474
setCypressConfigFilenameStub = sandbox.stub();
466475
setUserSpecsStub = sandbox.stub();
467476
setTestEnvsStub = sandbox.stub();
477+
setSystemEnvsStub = sandbox.stub();
468478
getConfigPathStub = sandbox.stub();
469479
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
470480
checkUploadedStub = sandbox.stub();
@@ -511,6 +521,7 @@ describe("runs", () => {
511521
setCypressConfigFilename: setCypressConfigFilenameStub,
512522
setUserSpecs: setUserSpecsStub,
513523
setTestEnvs: setTestEnvsStub,
524+
setSystemEnvs: setSystemEnvsStub,
514525
setUsageReportingFlag: setUsageReportingFlagStub,
515526
getConfigPath: getConfigPathStub,
516527
setLocal: setLocalStub,
@@ -583,6 +594,7 @@ describe("runs", () => {
583594
sinon.assert.calledOnce(sendUsageReportStub);
584595
sinon.assert.calledOnce(deleteResultsStub);
585596
sinon.assert.calledOnce(setDefaultsStub);
597+
sinon.assert.calledOnce(setSystemEnvsStub);
586598

587599
sinon.assert.calledOnceWithExactly(
588600
sendUsageReportStub,
@@ -611,6 +623,7 @@ describe("runs", () => {
611623
setCypressConfigFilenameStub = sandbox.stub();
612624
setUserSpecsStub = sandbox.stub();
613625
setTestEnvsStub = sandbox.stub();
626+
setSystemEnvsStub = sandbox.stub();
614627
getConfigPathStub = sandbox.stub();
615628
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
616629
checkUploadedStub = sandbox.stub();
@@ -663,6 +676,7 @@ describe("runs", () => {
663676
setCypressConfigFilename: setCypressConfigFilenameStub,
664677
setUserSpecs: setUserSpecsStub,
665678
setTestEnvs: setTestEnvsStub,
679+
setSystemEnvs: setSystemEnvsStub,
666680
setUsageReportingFlag: setUsageReportingFlagStub,
667681
setParallels: setParallelsStub,
668682
warnSpecLimit: warnSpecLimitStub,
@@ -745,6 +759,7 @@ describe("runs", () => {
745759
sinon.assert.calledOnce(exportResultsStub);
746760
sinon.assert.calledOnce(deleteResultsStub);
747761
sinon.assert.calledOnce(setDefaultsStub);
762+
sinon.assert.calledOnce(setSystemEnvsStub);
748763
sinon.assert.match(
749764
sendUsageReportStub.getCall(0).args,
750765
[

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe("capabilityHelper.js", () => {
6969
return capabilityHelper
7070
.caps(bsConfig, { zip_url: zip_url })
7171
.then(function (data) {
72-
chai.assert.equal(JSON.parse(data).cypress_version, cypress_version);
72+
chai.assert.equal(JSON.parse(JSON.parse(data).run_settings).cypress_version, cypress_version);
7373
})
7474
.catch((error) => {
7575
chai.assert.fail("Promise error");
@@ -309,7 +309,7 @@ describe("capabilityHelper.js", () => {
309309
return capabilityHelper
310310
.caps(bsConfig, { zip_url: zip_url })
311311
.then(function (data) {
312-
let parsed_data = JSON.parse(data);
312+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
313313
chai.assert.equal(parsed_data.specs, specsList);
314314
chai.assert.equal(parsed_data.env, undefined);
315315
})
@@ -341,7 +341,7 @@ describe("capabilityHelper.js", () => {
341341
return capabilityHelper
342342
.caps(bsConfig, { zip_url: zip_url })
343343
.then(function (data) {
344-
let parsed_data = JSON.parse(data);
344+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
345345
chai.assert.equal(parsed_data.env, envList);
346346
chai.assert.equal(parsed_data.specs, undefined);
347347
})
@@ -375,7 +375,7 @@ describe("capabilityHelper.js", () => {
375375
return capabilityHelper
376376
.caps(bsConfig, { zip_url: zip_url })
377377
.then(function (data) {
378-
let parsed_data = JSON.parse(data);
378+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
379379
chai.assert.equal(parsed_data.specs, specsList);
380380
chai.assert.equal(parsed_data.env, envList);
381381
})
@@ -405,7 +405,7 @@ describe("capabilityHelper.js", () => {
405405
return capabilityHelper
406406
.caps(bsConfig, { zip_url: zip_url })
407407
.then(function (data) {
408-
let parsed_data = JSON.parse(data);
408+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
409409
chai.assert.equal(parsed_data.specs, undefined);
410410
chai.assert.equal(parsed_data.env, undefined);
411411
})
@@ -439,7 +439,7 @@ describe("capabilityHelper.js", () => {
439439
return capabilityHelper
440440
.caps(bsConfig, { zip_url: zip_url })
441441
.then(function (data) {
442-
let parsed_data = JSON.parse(data);
442+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
443443
chai.assert.equal(parsed_data.headless, headless);
444444
chai.assert.equal(parsed_data.env, undefined);
445445
})
@@ -471,7 +471,7 @@ describe("capabilityHelper.js", () => {
471471
return capabilityHelper
472472
.caps(bsConfig, { zip_url: zip_url })
473473
.then(function (data) {
474-
let parsed_data = JSON.parse(data);
474+
let parsed_data = JSON.parse(JSON.parse(data).run_settings);
475475
chai.assert.equal(parsed_data.headless, headless);
476476
chai.assert.equal(parsed_data.env, undefined);
477477
})

0 commit comments

Comments
 (0)