Skip to content

Commit 3a533fa

Browse files
committed
Add file extension validator
1 parent cbbe4e0 commit 3a533fa

File tree

4 files changed

+45
-60
lines changed

4 files changed

+45
-60
lines changed

bin/helpers/archiver.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11

22
const fs = require('fs'),
33
archiver = require('archiver'),
4-
logger = require("./logger"),
5-
glob = require("glob"),
6-
path = require('path');
7-
8-
const getFiles = (jsGlobs, basePath, cb) => {
9-
files = [];
10-
11-
jsGlobs.forEach(function (item) {
12-
logger.log("Adding " + item + " to zip");
13-
files = glob.sync(basePath + item)
14-
});
15-
16-
files = files.map(file => path.relative(basePath, file))
17-
18-
if (cb){
19-
cb(files);
20-
}
21-
22-
return files;
23-
}
4+
logger = require("./logger");
245

256
const archiveSpecs = (runSettings, filePath) => {
267
return new Promise(function (resolve, reject) {
278
var output = fs.createWriteStream(filePath);
289

2910
var cypressFolderPath = runSettings.cypress
30-
var basePath = runSettings.cypress
3111

3212
var archive = archiver('zip', {
3313
zlib: { level: 9 } // Sets the compression level.
@@ -55,30 +35,6 @@ const archiveSpecs = (runSettings, filePath) => {
5535

5636
archive.pipe(output);
5737

58-
fileNames = [];
59-
60-
// getFiles(runSettings.specs, basePath, (files) => {
61-
// fileNames = fileNames.concat(files);
62-
// })
63-
64-
// getFiles(runSettings.supports, basePath, (files) => {
65-
// archive.append(JSON.stringify({"support": files}), {name: "cypress_helpers.json"})
66-
// fileNames = fileNames.concat(files);
67-
// })
68-
69-
// getFiles(runSettings.plugins, basePath, (files) => {
70-
// fileNames = fileNames.concat(files);
71-
// })
72-
73-
// getFiles(runSettings.fixtures, basePath, (files) => {
74-
// fileNames = fileNames.concat(files);
75-
// })
76-
77-
// fileNames.forEach(function(file) {
78-
// archive.file(basePath + file, { name: file });
79-
// });
80-
81-
// Add cypress.json
8238
archive.directory(cypressFolderPath, false);
8339

8440
archive.finalize();

bin/helpers/capabilityHelper.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
var logger = require("./logger");
2-
const Constants = require('./constants');
1+
const logger = require("./logger"),
2+
Constants = require('./constants'),
3+
glob = require("glob");
34

45
const caps = (bsConfig, zip) => {
56
return new Promise(function (resolve, reject) {
@@ -69,10 +70,49 @@ const validate = (bsConfig) => {
6970

7071
if(!bsConfig.run_settings.cypress) reject(Constants.validationMessages.EMPTY_SPEC_FILES);
7172

73+
if(invalidFiles(bsConfig.run_settings.cypress)) reject(Constants.validationMessages.INVALID_EXTENSION);
74+
7275
resolve(Constants.validationMessages.VALIDATED);
7376
});
7477
}
7578

79+
const invalidFiles = (testFolder)=> {
80+
var options = {
81+
dot: true
82+
}
83+
files = glob.sync(testFolder + "/**/*", options)
84+
var invalidFiles = []
85+
files.forEach(file => {
86+
if(isHiddenPath(file) || invalidExtension(file)){
87+
invalidFiles.push(file)
88+
}
89+
});
90+
91+
if(invalidFiles.length > 0) {
92+
logger.log("These files are not valid: " + invalidFiles.toString())
93+
return true
94+
} else {
95+
return false
96+
}
97+
}
98+
99+
var isHiddenPath = (path) => {
100+
return (/(^|\/)\.[^\/\.]/g).test(path);
101+
};
102+
103+
var invalidExtension = (file) => {
104+
let ext = file.split('.').pop();
105+
if (isFile(file) && !["js", "json", "txt"].includes(ext)) {
106+
return true;
107+
}
108+
109+
return false;
110+
}
111+
112+
var isFile = (path) => {
113+
return path.split('/').pop().indexOf('.') > -1;
114+
}
115+
76116
module.exports = {
77117
caps,
78118
validate

bin/helpers/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const validationMessages = {
2020
EMPTY_RUN_SETTINGS: "Empty run settings",
2121
EMPTY_SPEC_FILES: "No spec files specified in run_settings",
2222
VALIDATED: "browserstack.json file is validated",
23-
NOT_VALID: "browerstack.json is not valid"
23+
NOT_VALID: "browerstack.json is not valid",
24+
INVALID_EXTENSION: "Invalid files, please remove these files and try again."
2425
};
2526
const cliMessages = {
2627
VERSION: {

bin/templates/configTemplate.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ module.exports = function () {
1313
],
1414
"run_settings": {
1515
"cypress" : "/path/to/cypress_json",
16-
"specs": [
17-
"integration/examples/*.js"
18-
],
19-
"plugins": [
20-
"plugins/*.js"
21-
],
22-
"supports": [
23-
"support/*.js"
24-
],
25-
"fixtures": [
26-
"fixtures/*.js"
27-
],
2816
"project": "project-name",
2917
"customBuildName": "build-name"
3018
},

0 commit comments

Comments
 (0)