Skip to content

Commit 80d63e0

Browse files
Merge pull request #220 from browserstack/revert-217-revert-188-CYP-810-add-support-for-home-directory
Revert "Revert "Add support for home directory in CLI""
2 parents 9e9acc3 + 065ed05 commit 80d63e0

File tree

7 files changed

+203
-9
lines changed

7 files changed

+203
-9
lines changed

bin/helpers/archiver.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
2-
const fs = require("fs");
2+
const fs = require("fs"),
3+
path = require("path");
34

45
const archiver = require("archiver"),
56
Constants = require('../helpers/constants'),
67
logger = require("./logger").winstonLogger,
7-
utils = require('../helpers/utils'),
8-
path = require('path');
8+
utils = require('../helpers/utils');
99

1010
const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
1111
return new Promise(function (resolve, reject) {
@@ -14,7 +14,17 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
1414
}
1515
var output = fs.createWriteStream(filePath);
1616

17-
var cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);
17+
var cypressFolderPath = '';
18+
let cypressAppendFilesZipLocation = '';
19+
if (runSettings.home_directory) {
20+
cypressFolderPath = runSettings.home_directory;
21+
cypressAppendFilesZipLocation = runSettings.cypressZipStartLocation;
22+
if (cypressAppendFilesZipLocation !== '') {
23+
cypressAppendFilesZipLocation += '/';
24+
}
25+
} else {
26+
cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);
27+
}
1828

1929
logger.info(`Creating tests.zip with files in ${cypressFolderPath}`);
2030

@@ -61,7 +71,7 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
6171

6272
if (Object.keys(packageJSON).length > 0) {
6373
let packageJSONString = JSON.stringify(packageJSON, null, 4);
64-
archive.append(packageJSONString, {name: 'browserstack-package.json'});
74+
archive.append(packageJSONString, {name: `${cypressAppendFilesZipLocation}browserstack-package.json`});
6575
}
6676

6777
// do not add cypress.json if arg provided is false
@@ -73,7 +83,7 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
7383
fs.readFileSync(runSettings.cypressConfigFilePath)
7484
);
7585
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
76-
archive.append(cypressJSONString, {name: 'cypress.json'});
86+
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
7787
}
7888

7989
archive.finalize();

bin/helpers/capabilityHelper.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const fs = require('fs'),
2+
path = require('path');
3+
14
const logger = require("./logger").winstonLogger,
25
Constants = require("./constants"),
3-
Utils = require("./utils"),
4-
fs = require('fs');
6+
Utils = require("./utils");
57

68
const caps = (bsConfig, zip) => {
79
return new Promise(function (resolve, reject) {
@@ -129,6 +131,13 @@ const caps = (bsConfig, zip) => {
129131
})
130132
}
131133

134+
const addCypressZipStartLocation = (runSettings) => {
135+
let resolvedHomeDirectoryPath = path.resolve(runSettings.home_directory);
136+
let resolvedCypressConfigFilePath = path.resolve(runSettings.cypressConfigFilePath);
137+
runSettings.cypressZipStartLocation = path.dirname(resolvedCypressConfigFilePath.split(resolvedHomeDirectoryPath)[1]);
138+
runSettings.cypressZipStartLocation = runSettings.cypressZipStartLocation.substring(1);
139+
}
140+
132141
const validate = (bsConfig, args) => {
133142
return new Promise(function (resolve, reject) {
134143
logger.info(Constants.userMessages.VALIDATING_CONFIG);
@@ -185,11 +194,33 @@ const validate = (bsConfig, args) => {
185194
} catch(error){
186195
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
187196
}
197+
198+
//check if home_directory is present or not in user run_settings
199+
if (!Utils.isUndefined(bsConfig.run_settings.home_directory)) {
200+
// check if home_directory exists or not
201+
if (!fs.existsSync(bsConfig.run_settings.home_directory)) {
202+
reject(Constants.validationMessages.HOME_DIRECTORY_NOT_FOUND);
203+
}
204+
205+
// check if home_directory is a directory or not
206+
if (!fs.statSync(bsConfig.run_settings.home_directory).isDirectory()) {
207+
reject(Constants.validationMessages.HOME_DIRECTORY_NOT_A_DIRECTORY);
208+
}
209+
210+
// check if cypress config file (cypress.json) is a part of home_directory or not
211+
if (!path.resolve(bsConfig.run_settings.cypressConfigFilePath).includes(path.resolve(bsConfig.run_settings.home_directory))) {
212+
reject(Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY);
213+
}
214+
215+
addCypressZipStartLocation(bsConfig.run_settings);
216+
}
217+
188218
resolve(cypressJson);
189219
});
190220
}
191221

192222
module.exports = {
193223
caps,
224+
addCypressZipStartLocation,
194225
validate
195226
}

bin/helpers/checkUploaded.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ const checkSpecsMd5 = (runSettings, args, instrumentBlocks) => {
1515
if (args["force-upload"]) {
1616
return resolve("force-upload");
1717
}
18-
let cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);
18+
let cypressFolderPath = undefined;
19+
if (runSettings.home_directory) {
20+
cypressFolderPath = runSettings.home_directory;
21+
} else {
22+
cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);
23+
}
1924
let ignoreFiles = utils.getFilesToIgnore(runSettings, args.exclude, false);
2025
let options = {
2126
cwd: cypressFolderPath,

bin/helpers/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ const validationMessages = {
8787
INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
8888
INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference",
8989
INVALID_LOCAL_ASYNC_ARGS: "Cannot run in --async mode when local is set to true. Please run the build after removing --async",
90+
HOME_DIRECTORY_NOT_FOUND: "Specified home directory could not be found. Please make sure the path of the home directory is correct.",
91+
HOME_DIRECTORY_NOT_A_DIRECTORY: "Specified home directory is not a directory. The home directory can only be a directory and not a file.",
92+
CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY: "Could not find cypress.json within the specified home directory. Please make sure cypress.json resides within the home directory."
9093
};
9194

9295
const cliMessages = {

bin/helpers/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ exports.getErrorCodeFromMsg = (errMsg) => {
8585
case Constants.validationMessages.INVALID_LOCAL_ASYNC_ARGS:
8686
errorCode = 'invalid_local_async_args';
8787
break;
88+
case Constants.validationMessages.HOME_DIRECTORY_NOT_FOUND:
89+
errorCode = 'home_directory_not_found';
90+
break;
91+
case Constants.validationMessages.HOME_DIRECTORY_NOT_A_DIRECTORY:
92+
errorCode = 'home_directory_not_a_directory';
93+
break;
94+
case Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY:
95+
errorCode = 'cypress_config_file_not_part_of_home_directory';
96+
break;
8897
}
8998
if (
9099
errMsg.includes("Please use --config-file <path to browserstack.json>.")

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,17 @@ describe("capabilityHelper.js", () => {
554554
});
555555
});
556556

557+
describe("addCypressZipStartLocation", () => {
558+
it("returns correct zip start location", () => {
559+
let runSettings = {
560+
home_directory: "/some/path",
561+
cypressConfigFilePath: "/some/path/that/is/nested/file.json"
562+
};
563+
capabilityHelper.addCypressZipStartLocation(runSettings);
564+
chai.assert.equal(runSettings.cypressZipStartLocation, "that/is/nested");
565+
});
566+
});
567+
557568
describe("validate", () => {
558569

559570
describe("validate parallels specified in bsconfig and arguments", () => {
@@ -1053,5 +1064,115 @@ describe("capabilityHelper.js", () => {
10531064
})
10541065
});
10551066
});
1067+
1068+
describe("validate home directory", () => {
1069+
beforeEach(() => {
1070+
bsConfig = {
1071+
auth: {},
1072+
browsers: [
1073+
{
1074+
browser: "chrome",
1075+
os: "Windows 10",
1076+
versions: ["78", "77"],
1077+
},
1078+
],
1079+
run_settings: {
1080+
cypress_proj_dir: "random path",
1081+
cypressConfigFilePath: "random path",
1082+
cypressProjectDir: "random path"
1083+
},
1084+
};
1085+
});
1086+
1087+
it("does not exist", () => {
1088+
bsConfig.run_settings.cypressConfigFilePath = 'false';
1089+
bsConfig.run_settings.cypress_config_filename = 'false';
1090+
bsConfig.run_settings.home_directory = '/some/random';
1091+
1092+
sinon.stub(fs, 'existsSync').returns(false);
1093+
fs.existsSync.restore();
1094+
1095+
return capabilityHelper
1096+
.validate(bsConfig, {})
1097+
.then(function (data) {
1098+
chai.assert.fail("Promise error");
1099+
})
1100+
.catch((error) => {
1101+
chai.assert.equal(
1102+
error,
1103+
Constants.validationMessages.HOME_DIRECTORY_NOT_FOUND
1104+
);
1105+
});
1106+
});
1107+
1108+
it("is not a directory", () => {
1109+
bsConfig.run_settings.cypressConfigFilePath = 'false';
1110+
bsConfig.run_settings.cypress_config_filename = 'false';
1111+
bsConfig.run_settings.home_directory = '/some/random/file.ext';
1112+
1113+
sinon.stub(fs, 'existsSync').returns(true);
1114+
sinon.stub(fs, 'statSync').returns({ isDirectory: () => false });
1115+
1116+
return capabilityHelper
1117+
.validate(bsConfig, {})
1118+
.then(function (data) {
1119+
chai.assert.fail("Promise error");
1120+
})
1121+
.catch((error) => {
1122+
chai.assert.equal(
1123+
error,
1124+
Constants.validationMessages.HOME_DIRECTORY_NOT_A_DIRECTORY
1125+
);
1126+
fs.existsSync.restore();
1127+
fs.statSync.restore();
1128+
});
1129+
});
1130+
1131+
it("does not contain cypressConfigFilePath", () => {
1132+
bsConfig.run_settings.cypressConfigFilePath = 'false';
1133+
bsConfig.run_settings.cypress_config_filename = 'false';
1134+
bsConfig.run_settings.home_directory = '/some/random';
1135+
1136+
sinon.stub(fs, 'existsSync').returns(true);
1137+
sinon.stub(fs, 'statSync').returns({ isDirectory: () => true });
1138+
1139+
return capabilityHelper
1140+
.validate(bsConfig, {})
1141+
.then(function (data) {
1142+
chai.assert.fail("Promise error");
1143+
})
1144+
.catch((error) => {
1145+
chai.assert.equal(
1146+
error,
1147+
Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY
1148+
);
1149+
fs.existsSync.restore();
1150+
fs.statSync.restore();
1151+
});
1152+
});
1153+
1154+
it("does not contain cypressConfigFilePath with special chars", () => {
1155+
bsConfig.run_settings.cypressConfigFilePath = 'false';
1156+
bsConfig.run_settings.cypress_config_filename = 'false';
1157+
bsConfig.run_settings.home_directory = '/$some!@#$%^&*()_+=-[]{};:<>?\'\\\//random';
1158+
1159+
sinon.stub(fs, 'existsSync').returns(true);
1160+
sinon.stub(fs, 'statSync').returns({ isDirectory: () => true });
1161+
1162+
return capabilityHelper
1163+
.validate(bsConfig, {})
1164+
.then(function (data) {
1165+
chai.assert.fail("Promise error");
1166+
})
1167+
.catch((error) => {
1168+
chai.assert.equal(
1169+
error,
1170+
Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY
1171+
);
1172+
fs.existsSync.restore();
1173+
fs.statSync.restore();
1174+
});
1175+
});
1176+
});
10561177
});
10571178
});

test/unit/bin/helpers/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ describe('utils', () => {
105105
constant.validationMessages.INVALID_LOCAL_ASYNC_ARGS
106106
)
107107
).to.eq('invalid_local_async_args');
108+
expect(
109+
utils.getErrorCodeFromMsg(
110+
constant.validationMessages.HOME_DIRECTORY_NOT_FOUND
111+
)
112+
).to.eq('home_directory_not_found');
113+
expect(
114+
utils.getErrorCodeFromMsg(
115+
constant.validationMessages.HOME_DIRECTORY_NOT_A_DIRECTORY
116+
)
117+
).to.eq('home_directory_not_a_directory');
118+
expect(
119+
utils.getErrorCodeFromMsg(
120+
constant.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY
121+
)
122+
).to.eq('cypress_config_file_not_part_of_home_directory');
108123
expect(
109124
utils.getErrorCodeFromMsg('Invalid browserstack.json file.')
110125
).to.eq('bstack_json_invalid');

0 commit comments

Comments
 (0)