Skip to content

Commit d94e992

Browse files
Merge pull request #159 from browserstack/config_options
Improved support of config options in cli
2 parents 0be6b45 + 642b92f commit d94e992

File tree

8 files changed

+630
-340
lines changed

8 files changed

+630
-340
lines changed

bin/commands/runs.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function run(args) {
2323
markBlockEnd('deleteOldResults');
2424

2525
markBlockStart('validateBstackJson');
26-
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
26+
return utils.validateBstackJson(bsConfigPath).then(async function (bsConfig) {
2727
markBlockEnd('validateBstackJson');
2828
markBlockStart('setConfig');
2929
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);
@@ -69,6 +69,11 @@ module.exports = function run(args) {
6969
// set the no-wrap
7070
utils.setNoWrap(bsConfig, args);
7171

72+
//set browsers
73+
await utils.setBrowsers(bsConfig, args);
74+
75+
//set config (--config)
76+
utils.setConfig(bsConfig, args);
7277
// set other cypress configs e.g. reporter and reporter-options
7378
utils.setOtherConfigs(bsConfig, args);
7479
markBlockEnd('setConfig');

bin/helpers/capabilityHelper.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const logger = require("./logger").winstonLogger,
22
Constants = require("./constants"),
33
Utils = require("./utils"),
4-
fs = require('fs'),
5-
path = require('path');
4+
fs = require('fs');
65

76
const caps = (bsConfig, zip) => {
87
return new Promise(function (resolve, reject) {

bin/helpers/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ const validationMessages = {
7272
INVALID_CLI_LOCAL_IDENTIFIER: "When using --local-identifier, a value needs to be supplied. \n--local-identifier <String>.\nFor more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
7373
INVALID_LOCAL_MODE: "When using --local-mode, a value needs to be supplied. \n--local-mode (\"always-on\" | \"on-demand\").\nFor more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
7474
INVALID_LOCAL_CONFIG_FILE: "Using --local-config-file requires an input of the form /path/to/config-file.yml.\nFor more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
75-
INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference"
75+
INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
76+
INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference"
7677
};
7778

7879
const cliMessages = {
@@ -110,6 +111,8 @@ const cliMessages = {
110111
LOCAL_IDENTIFIER: "Accepted values: String - assign an identifier to your Local process instance",
111112
LOCAL_CONFIG_FILE: "Accepted values: String - path to local config-file to your Local process instance. Learn more at https://www.browserstack.com/local-testing/binary-params",
112113
SYNC_NO_WRAP: "Wrap the spec names in --sync mode in case of smaller terminal window size pass --no-wrap",
114+
BROWSER_DESCRIPTION: "Specify the browsers you need to run your tests on.",
115+
CONFIG_DESCRIPTION: "Set configuration values. Separate multiple values with a comma. The values set here override any values set in your configuration file.",
113116
REPORTER: "Specify the custom reporter to use",
114117
REPORTER_OPTIONS: "Specify reporter options for custom reporter",
115118
},

bin/helpers/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,36 @@ exports.deleteBaseUrlFromError = (err) => {
754754
return err.replace(/To test ([\s\S]*)on BrowserStack/g, 'To test on BrowserStack');
755755
}
756756

757+
exports.setBrowsers = async (bsConfig, args) => {
758+
return new Promise((resolve, reject) => {
759+
if(!this.isUndefined(args.browser)){
760+
try{
761+
bsConfig["browsers"] = []
762+
let browsersList = args.browser.split(',')
763+
browsersList.forEach((browser)=>{
764+
let browserHash = {}
765+
let osBrowserDetails = browser.split(':')
766+
browserHash['os'] = osBrowserDetails[1].trim()
767+
let browserDetails = osBrowserDetails[0].split('@')
768+
browserHash['browser'] = browserDetails[0].trim()
769+
browserHash['versions'] = []
770+
browserHash['versions'].push(this.isUndefined(browserDetails[1]) ? "latest" : browserDetails[1].trim())
771+
bsConfig["browsers"].push(browserHash)
772+
});
773+
} catch(err){
774+
reject(Constants.validationMessages.INVALID_BROWSER_ARGS)
775+
}
776+
}
777+
resolve()
778+
});
779+
}
780+
781+
exports.setConfig = (bsConfig, args) => {
782+
if (!this.isUndefined(args.config)) {
783+
bsConfig["run_settings"]["config"] = args.config
784+
}
785+
}
786+
757787
// blindly send other passed configs with run_settings and handle at backend
758788
exports.setOtherConfigs = (bsConfig, args) => {
759789
if (!this.isUndefined(args.reporter)) {

bin/runner.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ var argv = yargs
130130
demand: Constants.cliMessages.RUN.CYPRESS_CONFIG_DEMAND
131131
},
132132
'p': {
133-
alias: 'parallels',
133+
alias: ['parallels', 'parallel'],
134134
describe: Constants.cliMessages.RUN.PARALLEL_DESC,
135135
type: "number",
136136
default: undefined
137137
},
138138
'b': {
139-
alias: 'build-name',
139+
alias: ['build-name', 'ci-build-id'],
140140
describe: Constants.cliMessages.RUN.BUILD_NAME,
141141
type: "string",
142142
default: undefined
@@ -199,6 +199,17 @@ var argv = yargs
199199
describe: Constants.cliMessages.RUN.SYNC_NO_WRAP,
200200
type: "boolean"
201201
},
202+
'browser': {
203+
describe: Constants.cliMessages.RUN.BROWSER_DESCRIPTION,
204+
type: "string",
205+
default: undefined
206+
},
207+
'c': {
208+
alias: 'config',
209+
describe: Constants.cliMessages.RUN.CONFIG_DESCRIPTION,
210+
type: "string",
211+
default: undefined
212+
},
202213
'r': {
203214
alias: 'reporter',
204215
default: undefined,

test/unit/bin/commands/runs.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const chai = require("chai"),
55
const Constants = require("../../../../bin/helpers/constants"),
66
logger = require("../../../../bin/helpers/logger").winstonLogger,
77
testObjects = require("../../support/fixtures/testObjects");
8-
8+
99
const proxyquire = require("proxyquire").noCallThru();
1010

1111
chai.use(chaiAsPromised);
@@ -107,6 +107,8 @@ describe("runs", () => {
107107
setDefaultsStub = sandbox.stub();
108108
setLocalModeStub = sandbox.stub();
109109
setLocalConfigFileStub = sandbox.stub();
110+
setBrowsersStub = sandbox.stub();
111+
setConfigStub = sandbox.stub();
110112
});
111113

112114
afterEach(() => {
@@ -143,7 +145,9 @@ describe("runs", () => {
143145
isJSONInvalid: isJSONInvalidStub,
144146
setLocalMode: setLocalModeStub,
145147
setLocalConfigFile: setLocalConfigFileStub,
146-
setSystemEnvs: setSystemEnvsStub
148+
setSystemEnvs: setSystemEnvsStub,
149+
setBrowsers: setBrowsersStub,
150+
setConfig: setConfigStub
147151
},
148152
'../helpers/capabilityHelper': {
149153
validate: capabilityValidatorStub
@@ -176,6 +180,7 @@ describe("runs", () => {
176180
sinon.assert.calledOnce(setLocalConfigFileStub);
177181
sinon.assert.calledOnce(setHeadedStub);
178182
sinon.assert.calledOnce(setNoWrapStub);
183+
sinon.assert.calledOnce(setConfigStub);
179184
sinon.assert.calledOnce(setOtherConfigsStub);
180185
sinon.assert.calledOnce(capabilityValidatorStub);
181186
sinon.assert.calledOnce(getErrorCodeFromMsgStub);
@@ -228,6 +233,8 @@ describe("runs", () => {
228233
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
229234
setDefaultsStub = sandbox.stub();
230235
setLocalConfigFileStub = sandbox.stub();
236+
setBrowsersStub = sandbox.stub();
237+
setConfigStub = sandbox.stub();
231238
});
232239

233240
afterEach(() => {
@@ -265,7 +272,9 @@ describe("runs", () => {
265272
setDefaults: setDefaultsStub,
266273
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
267274
setLocalConfigFile: setLocalConfigFileStub,
268-
setSystemEnvs: setSystemEnvsStub
275+
setSystemEnvs: setSystemEnvsStub,
276+
setBrowsers: setBrowsersStub,
277+
setConfig: setConfigStub
269278
},
270279
'../helpers/capabilityHelper': {
271280
validate: capabilityValidatorStub,
@@ -364,6 +373,8 @@ describe("runs", () => {
364373
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
365374
setDefaultsStub = sandbox.stub();
366375
setLocalConfigFileStub = sandbox.stub();
376+
setConfigStub = sandbox.stub();
377+
setBrowsersStub = sandbox.stub();
367378
});
368379

369380
afterEach(() => {
@@ -401,7 +412,9 @@ describe("runs", () => {
401412
deleteResults: deleteResultsStub,
402413
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
403414
setDefaults: setDefaultsStub,
404-
setLocalConfigFile: setLocalConfigFileStub
415+
setLocalConfigFile: setLocalConfigFileStub,
416+
setBrowsers: setBrowsersStub,
417+
setConfig: setConfigStub
405418
},
406419
'../helpers/capabilityHelper': {
407420
validate: capabilityValidatorStub,
@@ -505,6 +518,8 @@ describe("runs", () => {
505518
setDefaultsStub = sandbox.stub();
506519
stopLocalBinaryStub = sandbox.stub();
507520
setLocalConfigFileStub = sandbox.stub();
521+
setConfigStub = sandbox.stub();
522+
setBrowsersStub = sandbox.stub();
508523
});
509524

510525
afterEach(() => {
@@ -543,7 +558,9 @@ describe("runs", () => {
543558
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
544559
setDefaults: setDefaultsStub,
545560
stopLocalBinary: stopLocalBinaryStub,
546-
setLocalConfigFile: setLocalConfigFileStub
561+
setLocalConfigFile: setLocalConfigFileStub,
562+
setBrowsers: setBrowsersStub,
563+
setConfig: setConfigStub
547564
},
548565
'../helpers/capabilityHelper': {
549566
validate: capabilityValidatorStub,
@@ -663,6 +680,8 @@ describe("runs", () => {
663680
initTimeComponentsStub = sandbox.stub();
664681
markBlockStartStub = sandbox.stub();
665682
markBlockEndStub = sandbox.stub();
683+
setConfigStub = sandbox.stub();
684+
setBrowsersStub = sandbox.stub();
666685
stopLocalBinaryStub = sandbox.stub();
667686
nonEmptyArrayStub = sandbox.stub();
668687
});
@@ -707,6 +726,8 @@ describe("runs", () => {
707726
isUndefined: isUndefinedStub,
708727
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
709728
setLocalConfigFile: setLocalConfigFileStub,
729+
setBrowsers: setBrowsersStub,
730+
setConfig: setConfigStub,
710731
stopLocalBinary: stopLocalBinaryStub,
711732
nonEmptyArray: nonEmptyArrayStub,
712733
},

test/unit/bin/helpers/fileHelpers.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ describe("fileHelpers", () => {
4545
expect(dataMock).to.eql(1);
4646
});
4747

48+
it("callback fn is executed after file write", () => {
49+
let dataMock = 0;
50+
51+
let callbackStub = sandbox.stub().callsFake(() => {
52+
dataMock = 1;
53+
});
54+
55+
const fileExtraStub = sinon.stub(fs,'writeFile');
56+
fileExtraStub.yields(true);
57+
fileHelpers.write({path: "./random_path", file: "random"}, "writing successful", {}, callbackStub);
58+
59+
sinon.assert.calledOnce(callbackStub);
60+
sinon.assert.calledOnce(fileExtraStub);
61+
expect(dataMock).to.eql(1);
62+
});
63+
4864
it("callback fn is executed after fileExists returns error", () => {
4965
let dataMock = undefined;
5066

0 commit comments

Comments
 (0)