Skip to content

Commit 809207a

Browse files
authored
Merge pull request #336 from browserstack/AFD-843-support-multiple-node-vers
Adds code changes to parse and validate node_version param
2 parents 413dd86 + d5a500f commit 809207a

File tree

9 files changed

+260
-7
lines changed

9 files changed

+260
-7
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ module.exports = function run(args, rawArgs) {
9797
// set record feature caps
9898
utils.setRecordCaps(bsConfig, args);
9999

100+
// set node version
101+
utils.setNodeVersion(bsConfig, args);
102+
100103
//set browsers
101104
await utils.setBrowsers(bsConfig, args);
102105

bin/helpers/capabilityHelper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ const validate = (bsConfig, args) => {
254254
}
255255
}
256256

257+
if (!Utils.isUndefined(bsConfig.run_settings.nodeVersion) && typeof(bsConfig.run_settings.nodeVersion) === 'string' && !bsConfig.run_settings.nodeVersion.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/))
258+
logger.warn(Constants.validationMessages.NODE_VERSION_PARSING_ERROR);
259+
257260
resolve(cypressJson);
258261
});
259262
}

bin/helpers/constants.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ const validationMessages = {
104104
SPEC_TIMEOUT_LIMIT_ERROR: "The maximum allowed value of 'spec_timeout' is 120. Read more on https://browserstack.com/docs/automate/cypress/spec-timeout ",
105105
SPEC_TIMEOUT_NOT_PASSED_ERROR: "'spec_timeout' key not specified. Going ahead with 30 mins as the default spec timeout. Read more about how to specify the option in https://browserstack.com/docs/automate/cypress/spec-timeout ",
106106
PROJECT_ID_MISSING: "You have specified '--record' flag but you've not provided the 'projectId' in cypress.json or in browserstack.json. Your record functionality on cypress.io dashboard might not work as it needs both the key and the projectId",
107-
RECORD_KEY_MISSING: "You have specified '--record' flag but you've not provided the '--record-key' and we could not find any value in 'CYPRESS_RECORD_KEY' environment variable. Your record functionality on cypress.io dashboard might not work as it needs the key and projectId"
107+
RECORD_KEY_MISSING: "You have specified '--record' flag but you've not provided the '--record-key' and we could not find any value in 'CYPRESS_RECORD_KEY' environment variable. Your record functionality on cypress.io dashboard might not work as it needs the key and projectId",
108+
NODE_VERSION_PARSING_ERROR: "We weren't able to successfully parse the specified nodeVersion. We will be using the default nodeVersion to run your tests."
108109
};
109110

110111
const cliMessages = {
@@ -151,7 +152,8 @@ const cliMessages = {
151152
SPEC_TIMEOUT: "Specify a value for a hard timeout for each spec execution in the 1-120 mins range. Read https://browserstack.com/docs/automate/cypress/spec-timeout for more details.",
152153
RECORD: "Pass the --record flag to record your Cypress runs on Cypress.io dashboard. Note: You also need to specify '--record-key' and '--projectId' arguments either in CLI or in browserstack.json.",
153154
RECORD_KEY: "You can specify the 'key' that is needed to record your runs on Cypress.io dashboard using the '--record-key' argument. Alternatively, you can also pass it on browserstack.json",
154-
PROJECT_ID: "You can pass the 'projectId' of your Cypress.io project where you want to record your runs if specifying the '--record' key. You can also specify this in your cypress.json or in your browserstack.json."
155+
PROJECT_ID: "You can pass the 'projectId' of your Cypress.io project where you want to record your runs if specifying the '--record' key. You can also specify this in your cypress.json or in your browserstack.json.",
156+
NODE_VERSION: "Pass the node version that you want BrowserStack to use to run your Cypress tests on."
155157
},
156158
COMMON: {
157159
DISABLE_USAGE_REPORTING: "Disable usage reporting",

bin/helpers/usageReporting.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,5 @@ function send(args) {
291291
module.exports = {
292292
send,
293293
cli_version_and_path,
294+
get_version
294295
};

bin/helpers/utils.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,36 @@ exports.setRecordCaps = (bsConfig, args) => {
371371
bsConfig.run_settings["projectId"] = this.setProjectId(bsConfig, args);
372372
}
373373

374+
exports.verifyNodeVersionOption = () => {
375+
let nvOptionsSet = (this.searchForOption('-nv') || this.searchForOption('--nv'));
376+
let nodeVersionHyphenLocationOptionsSet = (this.searchForOption('-node-version') || this.searchForOption('--node-version'));
377+
let nodeVersionOptionsSet = (this.searchForOption('-nodeVersion') || this.searchForOption('--nodeVersion'));
378+
return (nvOptionsSet || nodeVersionHyphenLocationOptionsSet || nodeVersionOptionsSet);
379+
}
380+
381+
exports.setNodeVersion = (bsConfig, args) => {
382+
let userProvidedNodeVersion = this.verifyNodeVersionOption();
383+
bsConfig.run_settings.userProvidedNodeVersion = (userProvidedNodeVersion || (!this.isUndefined(bsConfig.run_settings.nodeVersion)) || (!this.isUndefined(bsConfig.run_settings.nodeversion)));
384+
385+
if (bsConfig.run_settings.userProvidedNodeVersion) {
386+
if(!this.isUndefined(args.nodeVersion)) {
387+
bsConfig.run_settings.nodeVersion = args.nodeVersion;
388+
} else if (!this.isUndefined(bsConfig.run_settings.nodeversion)) {
389+
bsConfig.run_settings.nodeVersion = bsConfig.run_settings.nodeversion;
390+
}
391+
}
392+
393+
if (this.isUndefined(bsConfig.run_settings.nodeVersion)) {
394+
bsConfig.run_settings.nodeVersion = usageReporting.get_version('node') || '';
395+
}
396+
397+
if (bsConfig.run_settings.nodeVersion && typeof(bsConfig.run_settings.nodeVersion) === 'string' && bsConfig.run_settings.nodeVersion.charAt(0).toLowerCase() === 'v') {
398+
bsConfig.run_settings.nodeVersion = bsConfig.run_settings.nodeVersion.substr(1);
399+
}
400+
401+
logger.debug(`Setting nodeVersion = ${bsConfig.run_settings.nodeVersion}`);
402+
}
403+
374404
// specs can be passed from bstack configuration file
375405
// specs can be passed via command line args as a string
376406
// command line args takes precedence over config

bin/runner.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ var argv = yargs
265265
default: undefined,
266266
describe: Constants.cliMessages.RUN.PROJECT_ID,
267267
type: "string"
268+
},
269+
'nv': {
270+
alias: ['node-version', 'nodeVersion'],
271+
default: undefined,
272+
describe: Constants.cliMessages.RUN.NODE_VERSION,
273+
type: "string"
268274
}
269275
})
270276
.help('help')

test/unit/bin/commands/runs.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ describe("runs", () => {
121121
setGeolocationStub = sandbox.stub();
122122
setSpecTimeoutStub = sandbox.stub().returns(undefined);
123123
setRecordCapsStub = sandbox.stub().returns(undefined);
124+
setNodeVersionStub = sandbox.stub();
124125
});
125126

126127
afterEach(() => {
@@ -164,7 +165,8 @@ describe("runs", () => {
164165
setGeolocation: setGeolocationStub,
165166
setSpecTimeout: setSpecTimeoutStub,
166167
setRecordCaps: setRecordCapsStub,
167-
setDebugMode: setDebugModeStub
168+
setDebugMode: setDebugModeStub,
169+
setNodeVersion: setNodeVersionStub
168170
},
169171
'../helpers/capabilityHelper': {
170172
validate: capabilityValidatorStub
@@ -212,6 +214,7 @@ describe("runs", () => {
212214
sinon.assert.calledOnce(setSpecTimeoutStub);
213215
sinon.assert.calledOnce(getInitialDetailsStub);
214216
sinon.assert.calledOnce(setRecordCapsStub);
217+
sinon.assert.calledOnce(setNodeVersionStub);
215218
sinon.assert.calledOnceWithExactly(
216219
sendUsageReportStub,
217220
bsConfig,
@@ -272,6 +275,7 @@ describe("runs", () => {
272275
getInitialDetailsStub = sandbox.stub();
273276
setSpecTimeoutStub = sandbox.stub().returns(undefined);
274277
setRecordCapsStub = sandbox.stub().returns(undefined);
278+
setNodeVersionStub = sandbox.stub();
275279
});
276280

277281
afterEach(() => {
@@ -318,7 +322,8 @@ describe("runs", () => {
318322
getVideoConfig: getVideoConfigStub,
319323
setSpecTimeout: setSpecTimeoutStub,
320324
setRecordCaps: setRecordCapsStub,
321-
setDebugMode: setDebugModeStub
325+
setDebugMode: setDebugModeStub,
326+
setNodeVersion: setNodeVersionStub
322327
},
323328
'../helpers/capabilityHelper': {
324329
validate: capabilityValidatorStub,
@@ -385,6 +390,7 @@ describe("runs", () => {
385390
sinon.assert.calledOnce(setSpecTimeoutStub);
386391
sinon.assert.calledOnce(getInitialDetailsStub);
387392
sinon.assert.calledOnce(setRecordCapsStub);
393+
sinon.assert.calledOnce(setNodeVersionStub);
388394
sinon.assert.calledOnceWithExactly(
389395
sendUsageReportStub,
390396
bsConfig,
@@ -447,6 +453,7 @@ describe("runs", () => {
447453
setSpecTimeoutStub = sandbox.stub().returns(undefined);
448454
getInitialDetailsStub = sandbox.stub();
449455
setRecordCapsStub = sandbox.stub().returns(undefined);
456+
setNodeVersionStub = sandbox.stub();
450457
});
451458

452459
afterEach(() => {
@@ -494,7 +501,8 @@ describe("runs", () => {
494501
getVideoConfig: getVideoConfigStub,
495502
setSpecTimeout: setSpecTimeoutStub,
496503
setRecordCaps: setRecordCapsStub,
497-
setDebugMode: setDebugModeStub
504+
setDebugMode: setDebugModeStub,
505+
setNodeVersion: setNodeVersionStub
498506
},
499507
'../helpers/capabilityHelper': {
500508
validate: capabilityValidatorStub,
@@ -563,6 +571,7 @@ describe("runs", () => {
563571
sinon.assert.calledOnce(setSpecTimeoutStub);
564572
sinon.assert.calledOnce(getInitialDetailsStub);
565573
sinon.assert.calledOnce(setRecordCapsStub);
574+
sinon.assert.calledOnce(setNodeVersionStub);
566575
sinon.assert.calledOnceWithExactly(
567576
sendUsageReportStub,
568577
bsConfig,
@@ -630,6 +639,7 @@ describe("runs", () => {
630639
setSpecTimeoutStub = sandbox.stub().returns(undefined);
631640
getInitialDetailsStub = sandbox.stub();
632641
setRecordCapsStub = sandbox.stub().returns(undefined);
642+
setNodeVersionStub = sandbox.stub();
633643
});
634644

635645
afterEach(() => {
@@ -678,7 +688,8 @@ describe("runs", () => {
678688
getVideoConfig: getVideoConfigStub,
679689
setSpecTimeout: setSpecTimeoutStub,
680690
setRecordCaps: setRecordCapsStub,
681-
setDebugMode: setDebugModeStub
691+
setDebugMode: setDebugModeStub,
692+
setNodeVersion: setNodeVersionStub,
682693
},
683694
'../helpers/capabilityHelper': {
684695
validate: capabilityValidatorStub,
@@ -758,6 +769,8 @@ describe("runs", () => {
758769
sinon.assert.calledOnce(setSpecTimeoutStub);
759770
sinon.assert.calledOnce(getInitialDetailsStub);
760771
sinon.assert.calledOnce(setRecordCapsStub);
772+
sinon.assert.calledOnce(setNodeVersionStub);
773+
761774
sinon.assert.calledOnceWithExactly(
762775
sendUsageReportStub,
763776
bsConfig,
@@ -838,6 +851,7 @@ describe("runs", () => {
838851
setSpecTimeoutStub = sandbox.stub().returns(undefined);
839852
getInitialDetailsStub = sandbox.stub();
840853
setRecordCapsStub = sandbox.stub().returns(undefined);
854+
setNodeVersionStub = sandbox.stub();
841855
});
842856

843857
afterEach(() => {
@@ -894,7 +908,8 @@ describe("runs", () => {
894908
getVideoConfig: getVideoConfigStub,
895909
setSpecTimeout: setSpecTimeoutStub,
896910
setRecordCaps: setRecordCapsStub,
897-
setDebugMode: setDebugModeStub
911+
setDebugMode: setDebugModeStub,
912+
setNodeVersion: setNodeVersionStub,
898913
},
899914
'../helpers/capabilityHelper': {
900915
validate: capabilityValidatorStub,
@@ -990,6 +1005,7 @@ describe("runs", () => {
9901005
sinon.assert.calledOnce(setGeolocationStub);
9911006
sinon.assert.calledOnce(getInitialDetailsStub);
9921007
sinon.assert.calledOnce(setRecordCapsStub);
1008+
sinon.assert.calledOnce(setNodeVersionStub);
9931009
sinon.assert.match(
9941010
sendUsageReportStub.getCall(0).args,
9951011
[

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,42 @@ describe("capabilityHelper.js", () => {
11701170
});
11711171
});
11721172

1173+
describe("validate nodeVersion", () => {
1174+
beforeEach(() => {
1175+
bsConfig = {
1176+
auth: {},
1177+
browsers: [
1178+
{
1179+
browser: "chrome",
1180+
os: "Windows 10",
1181+
versions: ["78", "77"],
1182+
},
1183+
],
1184+
run_settings: {
1185+
cypress_proj_dir: "random path",
1186+
cypressConfigFilePath: "random path",
1187+
cypressProjectDir: "random path",
1188+
cypress_config_filename: "false"
1189+
},
1190+
connection_settings: {}
1191+
};
1192+
loggerWarningSpy = sinon.stub(logger, 'warn');
1193+
});
1194+
1195+
afterEach(function() {
1196+
loggerWarningSpy.restore();
1197+
});
1198+
1199+
it("should log a warning if nodeVersion is not in x.x.x format where x is a number", () => {
1200+
1201+
return capabilityHelper
1202+
.validate(bsConfig, {})
1203+
.then(function (data) {
1204+
sinon.assert.called(loggerWarningSpy);
1205+
});
1206+
});
1207+
});
1208+
11731209
describe("validate home directory", () => {
11741210
beforeEach(() => {
11751211
bsConfig = {

0 commit comments

Comments
 (0)