Skip to content

Commit 832af98

Browse files
Merge pull request #55 from browserstack/CYP_403_exclude
Support for excluding files & folders from uploads
2 parents 8c30b5e + 4e0174f commit 832af98

File tree

8 files changed

+100
-13
lines changed

8 files changed

+100
-13
lines changed

bin/commands/runs.js

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

4848
// Archive the spec files
49-
return archiver.archive(bsConfig.run_settings, config.fileName).then(function (data) {
49+
return archiver.archive(bsConfig.run_settings, config.fileName, args.exclude).then(function (data) {
5050

5151
// Uploaded zip file
5252
return zipUploader.zipUpload(bsConfig, config.fileName).then(function (zip) {
@@ -56,7 +56,7 @@ module.exports = function run(args) {
5656
let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`;
5757
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${config.dashboardUrl}${data.build_id}`;
5858
utils.exportResults(data.build_id, `${config.dashboardUrl}${data.build_id}`);
59-
if ((utils.isUndefined(bsConfig.run_settings.parallels) && utils.isUndefined(args.parallels)) || (!utils.isUndefined(bsConfig.run_settings.parallels) && bsConfig.run_settings.parallels == Constants.constants.DEFAULT_PARALLEL_MESSAGE)) {
59+
if ((utils.isUndefined(bsConfig.run_settings.parallels) && utils.isUndefined(args.parallels)) || (!utils.isUndefined(bsConfig.run_settings.parallels) && bsConfig.run_settings.parallels == Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE)) {
6060
logger.warn(Constants.userMessages.NO_PARALLELS);
6161
}
6262

bin/helpers/archiver.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
const fs = require("fs");
33

44
const archiver = require("archiver"),
5-
logger = require("./logger").winstonLogger;
5+
Constants = require('../helpers/constants'),
6+
logger = require("./logger").winstonLogger,
7+
utils = require('../helpers/utils');
68

7-
const archiveSpecs = (runSettings, filePath) => {
9+
const archiveSpecs = (runSettings, filePath, excludeFiles) => {
810
return new Promise(function (resolve, reject) {
911
var output = fs.createWriteStream(filePath);
1012

@@ -36,9 +38,10 @@ const archiveSpecs = (runSettings, filePath) => {
3638

3739
archive.pipe(output);
3840

39-
let allowedFileTypes = [ 'js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf', 'jpg', 'jpeg', 'png', 'zip' ];
40-
allowedFileTypes.forEach(fileType => {
41-
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ['**/node_modules/**', './node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'tests.zip'] });
41+
let ignoreFiles = getFilesToIgnore(runSettings, excludeFiles);
42+
43+
Constants.allowedFileTypes.forEach(fileType => {
44+
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ignoreFiles });
4245
});
4346

4447
let packageJSON = {};
@@ -60,4 +63,21 @@ const archiveSpecs = (runSettings, filePath) => {
6063
});
6164
}
6265

66+
const getFilesToIgnore = (runSettings, excludeFiles) => {
67+
let ignoreFiles = Constants.filesToIgnoreWhileUploading;
68+
69+
// exclude files asked by the user
70+
// args will take precedence over config file
71+
if (!utils.isUndefined(excludeFiles)) {
72+
let excludePatterns = utils.fixCommaSeparatedString(excludeFiles).split(',');
73+
ignoreFiles = ignoreFiles.concat(excludePatterns);
74+
logger.info(`Excluding files matching: ${JSON.stringify(excludePatterns)}`);
75+
} else if (!utils.isUndefined(runSettings.exclude) && runSettings.exclude.length) {
76+
ignoreFiles = ignoreFiles.concat(runSettings.exclude);
77+
logger.info(`Excluding files matching: ${JSON.stringify(runSettings.exclude)}`);
78+
}
79+
80+
return ignoreFiles;
81+
}
82+
6383
exports.archive = archiveSpecs

bin/helpers/capabilityHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const caps = (bsConfig, zip) => {
7777
}
7878
}
7979

80-
if(obj.parallels === Constants.constants.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
80+
if(obj.parallels === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
8181

8282
if (obj.project) logger.log(`Project name is: ${obj.project}`);
8383

bin/helpers/constants.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ const cliMessages = {
6161
DESC: "Path to BrowserStack config",
6262
CONFIG_DEMAND: "config file is required",
6363
BUILD_NAME: "The build name you want to use to name your test runs",
64+
EXCLUDE: "Exclude files matching a pattern from zipping and uploading",
65+
DEFAULT_PARALLEL_MESSAGE: "Here goes the number of parallels you want to run",
6466
SPECS_DESCRIPTION: 'Specify the spec files to run',
6567
ENV_DESCRIPTION: "Specify the environment variables for your spec files"
6668
},
@@ -81,14 +83,15 @@ const messageTypes = {
8183
NULL: null
8284
}
8385

84-
const constants = {
85-
DEFAULT_PARALLEL_MESSAGE: "Here goes the number of parallels you want to run"
86-
}
86+
const allowedFileTypes = ['js', 'json', 'txt', 'ts', 'feature', 'features', 'pdf', 'jpg', 'jpeg', 'png', 'zip'];
87+
88+
const filesToIgnoreWhileUploading = ['node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'tests.zip']
8789

8890
module.exports = Object.freeze({
8991
userMessages,
9092
cliMessages,
9193
validationMessages,
9294
messageTypes,
93-
constants
95+
allowedFileTypes,
96+
filesToIgnoreWhileUploading
9497
});

bin/helpers/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ exports.isUndefined = value => (value === undefined || value === null);
152152
exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0);
153153

154154
exports.isParallelValid = (value) => {
155-
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE;
155+
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1) || value === Constants.cliMessages.RUN.DEFAULT_PARALLEL_MESSAGE;
156156
}
157157

158158
exports.getUserAgent = () => {

bin/runner.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ var argv = yargs
165165
type: "string",
166166
default: undefined
167167
},
168+
'e': {
169+
alias: 'exclude',
170+
describe: Constants.cliMessages.RUN.EXCLUDE,
171+
type: "string",
172+
default: undefined
173+
},
168174
's': {
169175
alias: ['specs', 'spec'],
170176
describe: Constants.cliMessages.RUN.SPECS_DESCRIPTION,

bin/templates/configTemplate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = function () {
5555
"cypress_proj_dir" : "/path/to/directory-that-contains-<cypress.json>-file",
5656
"project_name": "project-name",
5757
"build_name": "build-name",
58+
"exclude": [],
5859
"parallels": "Here goes the number of parallels you want to run",
5960
"npm_dependencies": {
6061
},

test/unit/bin/helpers/archiver.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const chai = require("chai"),
2+
rewire = require("rewire"),
3+
chaiAsPromised = require("chai-as-promised");
4+
5+
const utils = require("../../../../bin/helpers/utils"),
6+
Constants = require("../../../../bin/helpers/constants"),
7+
logger = require("../../../../bin/helpers/logger").winstonLogger;
8+
9+
chai.use(chaiAsPromised);
10+
logger.transports["console.info"].silent = true;
11+
12+
const archiver = rewire("../../../../bin/helpers/archiver");
13+
14+
_getFilesToIgnore = archiver.__get__("getFilesToIgnore");
15+
16+
describe("archiver.js", () => {
17+
18+
describe("getFilesToIgnore", () => {
19+
it("no args, no exclude in runSettings", () => {
20+
chai.expect(_getFilesToIgnore({}, undefined)).to.be.eql(Constants.filesToIgnoreWhileUploading);
21+
});
22+
23+
it("args passed, no exclude in runSettings", () => {
24+
let excludeFiles = "file1.js, file2.json";
25+
let argsToArray = utils.fixCommaSeparatedString(excludeFiles).split(',');
26+
chai.expect(_getFilesToIgnore({}, excludeFiles)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(argsToArray));
27+
28+
excludeFiles = "file1.js,file2.json";
29+
argsToArray = utils.fixCommaSeparatedString(excludeFiles).split(',');
30+
chai.expect(_getFilesToIgnore({}, excludeFiles)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(argsToArray));
31+
32+
excludeFiles = " file1.js , file2.json ";
33+
argsToArray = utils.fixCommaSeparatedString(excludeFiles).split(',');
34+
chai.expect(_getFilesToIgnore({}, excludeFiles)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(argsToArray));
35+
});
36+
37+
it("args passed, exclude added in runSettings", () => {
38+
// args preceed over config file
39+
let excludeFiles = "file1.js, file2.json ";
40+
let argsToArray = utils.fixCommaSeparatedString(excludeFiles).split(',');
41+
42+
let runSettings = { exclude: [] };
43+
chai.expect(_getFilesToIgnore(runSettings, excludeFiles)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(argsToArray));
44+
45+
runSettings = { exclude: ["sample1.js", "sample2.json"] };
46+
chai.expect(_getFilesToIgnore(runSettings, excludeFiles)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(argsToArray));
47+
});
48+
49+
it("no args, exclude added in runSettings", () => {
50+
let runSettings = { exclude: [] };
51+
chai.expect(_getFilesToIgnore(runSettings, undefined)).to.be.eql(Constants.filesToIgnoreWhileUploading);
52+
53+
runSettings = { exclude: ["sample1.js", "sample2.json"] };
54+
chai.expect(_getFilesToIgnore(runSettings, undefined)).to.be.eql(Constants.filesToIgnoreWhileUploading.concat(runSettings.exclude));
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)