Skip to content

Commit fecf84e

Browse files
committed
add code changes to add support for home directory
1 parent f6748f0 commit fecf84e

File tree

5 files changed

+66
-8
lines changed

5 files changed

+66
-8
lines changed

bin/helpers/archiver.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
2-
const fs = require("fs");
3-
42
const archiver = require("archiver"),
53
Constants = require('../helpers/constants'),
64
logger = require("./logger").winstonLogger,
75
utils = require('../helpers/utils'),
8-
path = require('path');
6+
path = require('path'),
7+
fs = require("fs");
98

109
const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
1110
return new Promise(function (resolve, reject) {
@@ -14,7 +13,17 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
1413
}
1514
var output = fs.createWriteStream(filePath);
1615

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

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

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

6271
if (Object.keys(packageJSON).length > 0) {
6372
let packageJSONString = JSON.stringify(packageJSON, null, 4);
64-
archive.append(packageJSONString, {name: 'browserstack-package.json'});
73+
archive.append(packageJSONString, {name: `${cypressAppendFilesZipLocation}browserstack-package.json`});
6574
}
6675

6776
// do not add cypress.json if arg provided is false
@@ -73,7 +82,7 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
7382
fs.readFileSync(runSettings.cypressConfigFilePath)
7483
);
7584
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
76-
archive.append(cypressJSONString, {name: 'cypress.json'});
85+
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
7786
}
7887

7988
archive.finalize();

bin/helpers/capabilityHelper.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const logger = require("./logger").winstonLogger,
22
Constants = require("./constants"),
33
Utils = require("./utils"),
4-
fs = require('fs');
4+
fs = require('fs'),
5+
path = require('path');
56

67
const caps = (bsConfig, zip) => {
78
return new Promise(function (resolve, reject) {
@@ -129,6 +130,16 @@ const caps = (bsConfig, zip) => {
129130
})
130131
}
131132

133+
const addCypressZipStartLocation = (runSettings) => {
134+
let resolvedHomeDirectoryPath = path.resolve(runSettings.home_directory);
135+
let resolvedCypressConfigFilePath = path.resolve(runSettings.cypressConfigFilePath);
136+
// For example
137+
// resolvedHomeDirectoryPath -> '/Users/<user_name>/path'
138+
// resolvedCypressConfigFilePath -> '/Users/<user_name>/path/cypress.json' or '/Users/<user_name>/path/more_path/cypress.json'
139+
runSettings.cypressZipStartLocation = path.dirname(resolvedCypressConfigFilePath.split(resolvedHomeDirectoryPath)[1]); // cypressZipStartLocation -> '/' or '/more_path'
140+
runSettings.cypressZipStartLocation = runSettings.cypressZipStartLocation.substring(1); // cypressZipStartLocation -> '' or 'more_path'
141+
}
142+
132143
const validate = (bsConfig, args) => {
133144
return new Promise(function (resolve, reject) {
134145
logger.info(Constants.userMessages.VALIDATING_CONFIG);
@@ -185,6 +196,27 @@ const validate = (bsConfig, args) => {
185196
} catch(error){
186197
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
187198
}
199+
200+
//check if home_directory is present or not in user run_settings
201+
if (!Utils.isUndefined(bsConfig.run_settings.home_directory)) {
202+
// check if home_directory exists or not
203+
if (!fs.existsSync(bsConfig.run_settings.home_directory)) {
204+
reject(Constants.validationMessages.HOME_DIRECTORY_NOT_FOUND);
205+
}
206+
207+
// check if home_directory is a directory or not
208+
if (!fs.statSync(bsConfig.run_settings.home_directory).isDirectory()) {
209+
reject(Constants.validationMessages.HOME_DIRECTORY_NOT_A_DIRECTORY);
210+
}
211+
212+
// check if cypress config file (cypress.json) is a part of home_directory or not
213+
if (!path.resolve(bsConfig.run_settings.cypressConfigFilePath).includes(path.resolve(bsConfig.run_settings.home_directory))) {
214+
reject(Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_PART_OF_HOME_DIRECTORY);
215+
}
216+
217+
addCypressZipStartLocation(bsConfig.run_settings);
218+
}
219+
188220
resolve(cypressJson);
189221
});
190222
}

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
@@ -84,6 +84,9 @@ const validationMessages = {
8484
INVALID_LOCAL_IDENTIFIER: "Invalid value specified for local_identifier. For more info, check out https://www.browserstack.com/docs/automate/cypress/cli-reference",
8585
INVALID_BROWSER_ARGS: "Aborting as an unacceptable value was passed for --browser. Read more at https://www.browserstack.com/docs/automate/cypress/cli-reference",
8686
INVALID_LOCAL_ASYNC_ARGS: "Cannot run in --async mode when local is set to true. Please run the build after removing --async",
87+
HOME_DIRECTORY_NOT_FOUND: "Specified home directory could not be found. Please make sure the path of the home directory is correct.",
88+
HOME_DIRECTORY_NOT_A_DIRECTORY: "Specified home directory is not a directory. The home directory can only be a directory and not a file.",
89+
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."
8790
};
8891

8992
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>.")

0 commit comments

Comments
 (0)