Skip to content

Commit 9c56b1b

Browse files
Karan NagpalKaran Nagpal
authored andcommitted
Add support for CYPRESS_*, cypress_* env vars and system_env_vars
1 parent 8689aa8 commit 9c56b1b

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

bin/helpers/utils.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,51 @@ exports.setUserSpecs = (bsConfig, args) => {
266266
}
267267
}
268268

269-
// env option must be set only from command line args as a string
270269
exports.setTestEnvs = (bsConfig, args) => {
270+
// env option must be set only from command line args as a string
271271
if (!this.isUndefined(args.env)) {
272272
bsConfig.run_settings.env = this.fixCommaSeparatedString(args.env);
273273
} else {
274274
bsConfig.run_settings.env = null;
275275
}
276+
277+
// set env vars which start with CYPRESS_ and cypress_
278+
let pattern = /^cypress_/i;
279+
let matchingKeys = this.getKeysMatchingPattern(process.env, pattern);
280+
if (matchingKeys) {
281+
let envKeys = [];
282+
matchingKeys.forEach((envVar) => {
283+
envKeys.push(`${envVar}=${process.env.envVar}`);
284+
});
285+
286+
if (bsConfig.run_settings.env !== null) {
287+
bsConfig.run_settings.env = `${bsConfig.run_settings.env},${envKeys.join(',')}`;
288+
} else {
289+
bsConfig.run_settings.env = matchingKeys.join(',');
290+
}
291+
}
292+
293+
// set env vars which are defined in system_env_vars key
294+
if(!this.undefined(bsConfig.run_settings.system_env_vars)) {
295+
let system_env_vars = bsConfig.run_settings.system_env_vars;
296+
let envKeys = [];
297+
system_env_vars.forEach((envVar) => {
298+
envKeys.push(`${envVar}=${process.env.envVar}`);
299+
});
300+
301+
if (bsConfig.run_settings.env !== null) {
302+
bsConfig.run_settings.env = `${bsConfig.run_settings.env},${system_env_vars.join(',')}`;
303+
} else {
304+
bsConfig.run_settings.env = system_env_vars.join(',');
305+
}
306+
}
307+
}
308+
309+
exports.getKeysMatchingPattern = (obj, pattern) => {
310+
let matchingKeys = Object.keys(obj).filter(function(key) {
311+
return pattern.test(key);
312+
});
313+
return matchingKeys;
276314
}
277315

278316
exports.fixCommaSeparatedString = (string) => {

bin/templates/configTemplate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ module.exports = function () {
6161
},
6262
"package_config_options": {
6363
},
64+
"system_env_vars": [],
6465
"headless": true
6566
},
6667
"connection_settings": {

0 commit comments

Comments
 (0)