Skip to content

Commit 942094f

Browse files
committed
Merge branch 'master' into custom-reporter
2 parents 23f3f14 + 92ced56 commit 942094f

File tree

8 files changed

+230
-44
lines changed

8 files changed

+230
-44
lines changed

bin/commands/runs.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ module.exports = function run(args) {
4646
utils.setLocalIdentifier(bsConfig);
4747

4848
// Validate browserstack.json values and parallels specified via arguments
49-
return capabilityHelper.validate(bsConfig, args).then(function (validated) {
49+
return capabilityHelper.validate(bsConfig, args).then(function (cypressJson) {
50+
51+
//get the number of spec files
52+
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressJson);
5053

5154
// accept the number of parallels
52-
utils.setParallels(bsConfig, args);
55+
utils.setParallels(bsConfig, args, specFiles.length);
5356

5457
// Archive the spec files
5558
return archiver.archive(bsConfig.run_settings, config.fileName, args.exclude).then(function (data) {

bin/helpers/capabilityHelper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ const validate = (bsConfig, args) => {
126126
// validate if config file provided exists or not when cypress_config_file provided
127127
// validate the cypressProjectDir key otherwise.
128128
let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath;
129+
let cypressJson = {};
129130

130131
if (!fs.existsSync(cypressConfigFilePath) && bsConfig.run_settings.cypress_config_filename !== 'false') reject(Constants.validationMessages.INVALID_CYPRESS_CONFIG_FILE);
131132

@@ -143,8 +144,7 @@ const validate = (bsConfig, args) => {
143144
} catch(error){
144145
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
145146
}
146-
147-
resolve(Constants.validationMessages.VALIDATED);
147+
resolve(cypressJson);
148148
});
149149
}
150150

bin/helpers/constants.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,18 @@ const allowedFileTypes = ['js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf
117117

118118
const filesToIgnoreWhileUploading = ['**/node_modules/**', 'node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'tests.zip', 'cypress.json']
119119

120+
const specFileTypes = ['js', 'ts', 'feature']
121+
122+
const DEFAULT_CYPRESS_SPEC_PATH = "cypress/integration"
123+
120124
module.exports = Object.freeze({
121125
syncCLI,
122126
userMessages,
123127
cliMessages,
124128
validationMessages,
125129
messageTypes,
126130
allowedFileTypes,
127-
filesToIgnoreWhileUploading
131+
filesToIgnoreWhileUploading,
132+
specFileTypes,
133+
DEFAULT_CYPRESS_SPEC_PATH
128134
});

bin/helpers/utils.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const os = require("os");
33
const path = require("path");
44
const fs = require("fs");
5+
const glob = require('glob');
56

67
const usageReporting = require("./usageReporting"),
78
logger = require("./logger").winstonLogger,
@@ -112,10 +113,22 @@ exports.setUsageReportingFlag = (bsConfig, disableUsageReporting) => {
112113
}
113114
};
114115

115-
exports.setParallels = (bsConfig, args) => {
116+
exports.setParallels = (bsConfig, args, numOfSpecs) => {
116117
if (!this.isUndefined(args.parallels)) {
117118
bsConfig["run_settings"]["parallels"] = args.parallels;
118119
}
120+
let browserCombinations = this.getBrowserCombinations(bsConfig);
121+
let maxParallels = browserCombinations.length * numOfSpecs;
122+
if (numOfSpecs <= 0) {
123+
bsConfig['run_settings']['parallels'] = browserCombinations.length;
124+
return;
125+
}
126+
if (bsConfig['run_settings']['parallels'] > maxParallels && bsConfig['run_settings']['parallels'] != -1 ) {
127+
logger.warn(
128+
`Using ${maxParallels} machines instead of ${bsConfig['run_settings']['parallels']} that you configured as there are ${numOfSpecs} specs to be run on ${browserCombinations.length} browser combinations.`
129+
);
130+
bsConfig['run_settings']['parallels'] = maxParallels;
131+
}
119132
};
120133

121134
exports.setDefaults = (bsConfig, args) => {
@@ -320,6 +333,27 @@ exports.setLocalIdentifier = (bsConfig) => {
320333
}
321334
};
322335

336+
exports.getNumberOfSpecFiles = (bsConfig, args, cypressJson) => {
337+
let testFolderPath = cypressJson.integrationFolder || Constants.DEFAULT_CYPRESS_SPEC_PATH;
338+
let globSearchPatttern = bsConfig.run_settings.specs || `${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`;
339+
let ignoreFiles = args.exclude || bsConfig.run_settings.exclude;
340+
let files = glob.sync(globSearchPatttern, {cwd: bsConfig.run_settings.cypressProjectDir, matchBase: true, ignore: ignoreFiles});
341+
return files;
342+
};
343+
344+
exports.getBrowserCombinations = (bsConfig) => {
345+
let osBrowserArray = [];
346+
let osBrowser = "";
347+
if (bsConfig.browsers) {
348+
bsConfig.browsers.forEach((element) => {
349+
osBrowser = element.os + '-' + element.browser;
350+
element.versions.forEach((version) => {
351+
osBrowserArray.push(osBrowser + version);
352+
});
353+
});
354+
}
355+
return osBrowserArray;
356+
};
323357
exports.capitalizeFirstLetter = (stringToCapitalize) => {
324358
return stringToCapitalize && (stringToCapitalize[0].toUpperCase() + stringToCapitalize.slice(1));
325359
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"requestretry": "^4.1.0",
2121
"table": "^5.4.6",
2222
"winston": "^2.3.1",
23-
"yargs": "^14.2.2"
23+
"yargs": "^14.2.2",
24+
"glob": "^7.1.6"
2425
},
2526
"repository": {
2627
"type": "git",

test/unit/bin/commands/runs.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ describe("runs", () => {
188188
setLocalStub = sandbox.stub();
189189
setLocalIdentifierStub = sandbox.stub();
190190
deleteResultsStub = sandbox.stub();
191+
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
191192
setDefaultsStub = sandbox.stub();
192193
});
193194

@@ -217,7 +218,8 @@ describe("runs", () => {
217218
setLocal: setLocalStub,
218219
setLocalIdentifier: setLocalIdentifierStub,
219220
deleteResults: deleteResultsStub,
220-
setDefaults: setDefaultsStub
221+
setDefaults: setDefaultsStub,
222+
getNumberOfSpecFiles: getNumberOfSpecFilesStub
221223
},
222224
'../helpers/capabilityHelper': {
223225
validate: capabilityValidatorStub,
@@ -241,6 +243,7 @@ describe("runs", () => {
241243
.catch((error) => {
242244
sinon.assert.calledOnce(getConfigPathStub);
243245
sinon.assert.calledOnce(getConfigPathStub);
246+
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
244247
sinon.assert.calledOnce(setParallelsStub);
245248
sinon.assert.calledOnce(setLocalStub);
246249
sinon.assert.calledOnce(setLocalIdentifierStub);
@@ -288,6 +291,7 @@ describe("runs", () => {
288291
setLocalStub = sandbox.stub();
289292
setLocalIdentifierStub = sandbox.stub();
290293
deleteResultsStub = sandbox.stub();
294+
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
291295
setDefaultsStub = sandbox.stub();
292296
});
293297

@@ -317,6 +321,7 @@ describe("runs", () => {
317321
setLocal: setLocalStub,
318322
setLocalIdentifier: setLocalIdentifierStub,
319323
deleteResults: deleteResultsStub,
324+
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
320325
setDefaults: setDefaultsStub
321326
},
322327
'../helpers/capabilityHelper': {
@@ -345,6 +350,7 @@ describe("runs", () => {
345350
.catch((error) => {
346351
sinon.assert.calledOnce(getConfigPathStub);
347352
sinon.assert.calledOnce(getConfigPathStub);
353+
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
348354
sinon.assert.calledOnce(setParallelsStub);
349355
sinon.assert.calledOnce(setLocalStub);
350356
sinon.assert.calledOnce(setLocalIdentifierStub);
@@ -396,6 +402,7 @@ describe("runs", () => {
396402
setLocalStub = sandbox.stub();
397403
setLocalIdentifierStub = sandbox.stub();
398404
deleteResultsStub = sandbox.stub();
405+
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
399406
setDefaultsStub = sandbox.stub();
400407
});
401408

@@ -425,6 +432,7 @@ describe("runs", () => {
425432
setLocal: setLocalStub,
426433
setLocalIdentifier: setLocalIdentifierStub,
427434
deleteResults: deleteResultsStub,
435+
getNumberOfSpecFiles: getNumberOfSpecFilesStub,
428436
setDefaults: setDefaultsStub
429437
},
430438
'../helpers/capabilityHelper': {
@@ -461,6 +469,7 @@ describe("runs", () => {
461469
sinon.assert.calledOnce(getConfigPathStub);
462470
sinon.assert.calledOnce(validateBstackJsonStub);
463471
sinon.assert.calledOnce(capabilityValidatorStub);
472+
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
464473
sinon.assert.calledOnce(setParallelsStub);
465474
sinon.assert.calledOnce(setLocalStub);
466475
sinon.assert.calledOnce(setLocalIdentifierStub);
@@ -516,6 +525,7 @@ describe("runs", () => {
516525
isUndefinedStub = sandbox.stub();
517526
setLocalStub = sandbox.stub();
518527
setLocalIdentifierStub = sandbox.stub();
528+
getNumberOfSpecFilesStub = sandbox.stub().returns([]);
519529
});
520530

521531
afterEach(() => {
@@ -529,8 +539,8 @@ describe("runs", () => {
529539
let message = `Success! ${Constants.userMessages.BUILD_CREATED} with build id: random_build_id`;
530540
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${dashboardUrl}`;
531541

532-
const runs = proxyquire("../../../../bin/commands/runs", {
533-
"../helpers/utils": {
542+
const runs = proxyquire('../../../../bin/commands/runs', {
543+
'../helpers/utils': {
534544
validateBstackJson: validateBstackJsonStub,
535545
sendUsageReport: sendUsageReportStub,
536546
setUsername: setUsernameStub,
@@ -547,24 +557,25 @@ describe("runs", () => {
547557
exportResults: exportResultsStub,
548558
deleteResults: deleteResultsStub,
549559
setDefaults: setDefaultsStub,
550-
isUndefined: isUndefinedStub
560+
isUndefined: isUndefinedStub,
561+
getNumberOfSpecFiles: getNumberOfSpecFilesStub
551562
},
552-
"../helpers/capabilityHelper": {
563+
'../helpers/capabilityHelper': {
553564
validate: capabilityValidatorStub,
554565
},
555-
"../helpers/archiver": {
566+
'../helpers/archiver': {
556567
archive: archiverStub,
557568
},
558-
"../helpers/fileHelpers": {
569+
'../helpers/fileHelpers': {
559570
deleteZip: deleteZipStub,
560571
},
561-
"../helpers/zipUpload": {
572+
'../helpers/zipUpload': {
562573
zipUpload: zipUploadStub,
563574
},
564-
"../helpers/build": {
575+
'../helpers/build': {
565576
createBuild: createBuildStub,
566577
},
567-
"../helpers/config": {
578+
'../helpers/config': {
568579
dashboardUrl: dashboardUrl,
569580
},
570581
});
@@ -586,6 +597,7 @@ describe("runs", () => {
586597
sinon.assert.calledOnce(getConfigPathStub);
587598
sinon.assert.calledOnce(validateBstackJsonStub);
588599
sinon.assert.calledOnce(capabilityValidatorStub);
600+
sinon.assert.calledOnce(getNumberOfSpecFilesStub);
589601
sinon.assert.calledOnce(setParallelsStub);
590602
sinon.assert.calledOnce(setLocalStub);
591603
sinon.assert.calledOnce(setLocalIdentifierStub);

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -561,10 +561,7 @@ describe("capabilityHelper.js", () => {
561561
return capabilityHelper
562562
.validate(bsConfig, { parallels: undefined })
563563
.then(function (data) {
564-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
565-
})
566-
.catch((error) => {
567-
chai.assert.fail("Promise error");
564+
chai.assert.deepEqual(data, {});
568565
});
569566
});
570567

@@ -574,10 +571,7 @@ describe("capabilityHelper.js", () => {
574571
return capabilityHelper
575572
.validate(bsConfig, { parallels: undefined })
576573
.then(function (data) {
577-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
578-
})
579-
.catch((error) => {
580-
chai.assert.fail("Promise error");
574+
chai.assert.deepEqual(data, {});
581575
});
582576
});
583577

@@ -586,10 +580,7 @@ describe("capabilityHelper.js", () => {
586580
return capabilityHelper
587581
.validate(bsConfig, { parallels: 200 })
588582
.then(function (data) {
589-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
590-
})
591-
.catch((error) => {
592-
chai.assert.fail("Promise error");
583+
chai.assert.deepEqual(data, {});
593584
});
594585
});
595586

@@ -598,10 +589,7 @@ describe("capabilityHelper.js", () => {
598589
return capabilityHelper
599590
.validate(bsConfig, { parallels: -1 })
600591
.then(function (data) {
601-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
602-
})
603-
.catch((error) => {
604-
chai.assert.fail("Promise error");
592+
chai.assert.deepEqual(data, {});
605593
});
606594
});
607595

@@ -611,10 +599,7 @@ describe("capabilityHelper.js", () => {
611599
return capabilityHelper
612600
.validate(bsConfig, { parallels: -1 })
613601
.then(function (data) {
614-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
615-
})
616-
.catch((error) => {
617-
chai.assert.fail("Promise error");
602+
chai.assert.deepEqual(data, {});
618603
});
619604
});
620605

@@ -624,10 +609,7 @@ describe("capabilityHelper.js", () => {
624609
return capabilityHelper
625610
.validate(bsConfig, { parallels: -1 })
626611
.then(function (data) {
627-
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
628-
})
629-
.catch((error) => {
630-
chai.assert.fail("Promise error");
612+
chai.assert.deepEqual(data, {});
631613
});
632614
});
633615
});

0 commit comments

Comments
 (0)