Skip to content
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

Support for env variables using different ways #147

Merged
merged 12 commits into from
Aug 27, 2021
3 changes: 3 additions & 0 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ module.exports = function run(args) {
// accept the env list from command line and set it
utils.setTestEnvs(bsConfig, args);

// accept the system env list from bsconf and set it
utils.setSystemEnvs(bsConfig);

//accept the local from env variable if provided
utils.setLocal(bsConfig, args);

Expand Down
26 changes: 6 additions & 20 deletions bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,18 @@ const caps = (bsConfig, zip) => {
obj.projectNotifyURL = null;

if (bsConfig.run_settings) {
obj.project = bsConfig.run_settings.project || bsConfig.run_settings.project_name;
obj.customBuildName = bsConfig.run_settings.build_name || bsConfig.run_settings.customBuildName;
obj.project = bsConfig.run_settings.project || bsConfig.run_settings.project_name || obj.project;
obj.customBuildName = bsConfig.run_settings.build_name || bsConfig.run_settings.customBuildName || obj.customBuildName;
obj.callbackURL = bsConfig.run_settings.callback_url;
obj.projectNotifyURL = bsConfig.run_settings.project_notify_URL;
obj.parallels = bsConfig.run_settings.parallels;

if (!Utils.isUndefined(bsConfig.run_settings.cypress_config_filename)) {
obj.cypress_config_filename = bsConfig.run_settings.cypress_config_filename;
}

if (!Utils.isUndefined(bsConfig.run_settings.specs)){
obj.specs = bsConfig.run_settings.specs;
}

if (!Utils.isUndefined(bsConfig.run_settings.env)){
obj.env = bsConfig.run_settings.env;
}
if (!Utils.isUndefined(bsConfig.run_settings.cypress_version)){
obj.cypress_version = bsConfig.run_settings.cypress_version;
}

if (!Utils.isUndefined(bsConfig.run_settings.headless) && String(bsConfig.run_settings.headless) === "false"){
obj.headless = bsConfig.run_settings.headless;
} else {
if (!(!Utils.isUndefined(bsConfig.run_settings.headless) && String(bsConfig.run_settings.headless) === "false")) {
logger.info(`Running your tests in headless mode. Use --headed arg to run in headful mode.`);
}

// send run_settings as is for other capabilities
obj.run_settings = JSON.stringify(bsConfig.run_settings);
}

if(obj.parallels === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
Expand Down
2 changes: 1 addition & 1 deletion bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const messageTypes = {
NULL: null
}

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'];
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'];

const filesToIgnoreWhileUploading = [
'**/node_modules/**',
Expand Down
54 changes: 51 additions & 3 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,61 @@ exports.setUserSpecs = (bsConfig, args) => {
}
}

// env option must be set only from command line args as a string
exports.setTestEnvs = (bsConfig, args) => {
let envKeys = {};

if(bsConfig.run_settings.env && Object.keys(bsConfig.run_settings.env).length !== 0) {
nagpalkaran95 marked this conversation as resolved.
Show resolved Hide resolved
envKeys = bsConfig.run_settings.env;
}

// set env vars passed from command line args as a string
if (!this.isUndefined(args.env)) {
bsConfig.run_settings.env = this.fixCommaSeparatedString(args.env);
} else {
let argsEnvVars = this.fixCommaSeparatedString(args.env).split(',');
argsEnvVars.forEach((envVar) => {
let env = envVar.split("=");
envKeys[env[0]] = env[1];
});
}

if (Object.keys(envKeys).length === 0) {
bsConfig.run_settings.env = null;
} else {
bsConfig.run_settings.env = Object.keys(envKeys).map(key => (`${key}=${envKeys[key]}`)).join(',');
}
}

exports.setSystemEnvs = (bsConfig) => {
let envKeys = {};

// set env vars which are defined in system_env_vars key
if(!this.isUndefined(bsConfig.run_settings.system_env_vars) && Array.isArray(bsConfig.run_settings.system_env_vars) && bsConfig.run_settings.system_env_vars.length) {
let systemEnvVars = bsConfig.run_settings.system_env_vars;
systemEnvVars.forEach((envVar) => {
envKeys[envVar] = process.env[envVar];
});
}

// set env vars which start with CYPRESS_ and cypress_
let pattern = /^cypress_/i;
let matchingKeys = this.getKeysMatchingPattern(process.env, pattern);
if (matchingKeys && matchingKeys.length) {
matchingKeys.forEach((envVar) => {
nagpalkaran95 marked this conversation as resolved.
Show resolved Hide resolved
envKeys[envVar] = process.env[envVar];
});
}

if (Object.keys(envKeys).length === 0) {
bsConfig.run_settings.system_env_vars = null;
} else {
bsConfig.run_settings.system_env_vars = Object.keys(envKeys).map(key => (`${key}=${envKeys[key]}`));
}
}

exports.getKeysMatchingPattern = (obj, pattern) => {
let matchingKeys = Object.keys(obj).filter(function(key) {
return pattern.test(key);
});
return matchingKeys;
}

exports.fixCommaSeparatedString = (string) => {
Expand Down
100 changes: 89 additions & 11 deletions test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ describe('utils', () => {
let args = testObjects.initSampleArgs;

it('should call sendUsageReport', () => {
sendUsageReportStub = sandbox
let sendUsageReportStub = sandbox
.stub(utils, 'sendUsageReport')
.callsFake(function () {
return 'end';
Expand Down Expand Up @@ -508,11 +508,10 @@ describe('utils', () => {
});

describe('setTestEnvs', () => {
it('sets env only from args', () => {
it('set env only from args', () => {
let argsEnv = 'env3=value3, env4=value4';
let bsConfig = {
run_settings: {
env: 'env1=value1, env2=value2',
},
};
let args = {
Expand All @@ -523,34 +522,113 @@ describe('utils', () => {
expect(bsConfig.run_settings.env).to.be.eq('env3=value3,env4=value4');
});

it('sets env from args without spaces in it', () => {
let argsEnv = 'env3=value3 , env4=value4';
it('set env only from browserstack.json env param', () => {
let bsConfig = {
run_settings: {
env: {
env1: 'value1',
env2: 'value2',
}
},
};
let args = {
env: null
};

utils.setTestEnvs(bsConfig, args);
expect(bsConfig.run_settings.env).to.be.eq('env1=value1,env2=value2');
});

it('merges env from args and browserstack.json env param', () => {
let argsEnv = 'env3=value3, env4=value4';
let bsConfig = {
run_settings: {
env: 'env1=value1 , env2=value2',
env: {
env1: 'value1',
env2: 'value2',
}
},
};
let args = {
env: argsEnv,
};

utils.setTestEnvs(bsConfig, args);
expect(bsConfig.run_settings.env).to.be.eq('env3=value3,env4=value4');
expect(bsConfig.run_settings.env).to.be.eq('env1=value1,env2=value2,env3=value3,env4=value4');
});

it('does not set env if not specified in args', () => {
it('merges env from args and browserstack.json env param but give preceedence to args', () => {
let argsEnv = 'env1=value0, env4=value4';
let bsConfig = {
run_settings: {
env: {
env1: 'value1',
env2: 'value2',
}
},
};
let args = {
env: argsEnv,
};

utils.setTestEnvs(bsConfig, args);
expect(bsConfig.run_settings.env).to.be.eq('env1=value0,env2=value2,env4=value4');
});

it('handle spaces passed while specifying env', () => {
let argsEnv = 'env3=value3 , env4=value4';
let bsConfig = {
run_settings: {
env: 'env1=value1 , env2=value2',
env: {
env1: 'value1',
env2: 'value2',
}
},
};
let args = {
env: null,
env: argsEnv,
};

utils.setTestEnvs(bsConfig, args);
expect(bsConfig.run_settings.env).to.be.eq(null);
expect(bsConfig.run_settings.env).to.be.eq('env1=value1,env2=value2,env3=value3,env4=value4');
});
});

describe('setSystemEnvs', () => {
it('set vars passed in system_env_vars', () => {
process.env.ENV1 = 'env1';
process.env.ENV2 = 'env2';
let bsConfig = {
run_settings: {
env: {
env1: 'value1',
env2: 'value2',
},
system_env_vars: ['ENV1', 'ENV2']
},
};

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars).to.be.an('array').that.includes('ENV1=env1');
expect(bsConfig.run_settings.system_env_vars).to.be.an('array').that.includes('ENV2=env2');
delete process.env.ENV1;
delete process.env.ENV2;
});

it('set vars defined on machine as CYPRESS_ or cypress_', () => {
process.env.CYPRESS_TEST_1 = 'env1';
process.env.cypress_test_2 = 'env2';
let bsConfig = {
run_settings: {
env: null
},
};

utils.setSystemEnvs(bsConfig);
expect(bsConfig.run_settings.system_env_vars).to.be.an('array').that.includes('CYPRESS_TEST_1=env1');
expect(bsConfig.run_settings.system_env_vars).to.be.an('array').that.includes('cypress_test_2=env2');
delete process.env.CYPRESS_TEST_1;
delete process.env.cypress_test_2;
});
});

Expand Down