Skip to content

Commit c4faf12

Browse files
committed
Adding cypressConfigFilePath and userProvidedCypressConfigFile options
cypressConfigFilePath has cypress_config_file or proj_dir + cypress.json based on userProvidedCypressConfigFile value. This enables the current users to use cypress_proj_dir as is for now until we remove it.
1 parent 8c21fd0 commit c4faf12

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

bin/helpers/archiver.js

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

44
const archiver = require("archiver"),
5-
logger = require("./logger").winstonLogger;
5+
logger = require("./logger").winstonLogger,
6+
path = require('path');
67

78
const archiveSpecs = (runSettings, filePath) => {
89
return new Promise(function (resolve, reject) {
910
var output = fs.createWriteStream(filePath);
1011

11-
var cypressFolderPath = runSettings.cypress_proj_dir;
12+
var cypressFolderPath = path.dirname(runSettings.cypressConfigFilePath);
1213

1314
var archive = archiver('zip', {
1415
zlib: { level: 9 } // Sets the compression level.
@@ -38,7 +39,7 @@ const archiveSpecs = (runSettings, filePath) => {
3839

3940
let allowedFileTypes = [ 'js', 'json', 'txt', 'ts', 'feature', 'features' ];
4041
allowedFileTypes.forEach(fileType => {
41-
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ['node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json'] });
42+
archive.glob(`**/*.${fileType}`, { cwd: cypressFolderPath, matchBase: true, ignore: ['node_modules/**', 'package-lock.json', 'package.json', 'browserstack-package.json', 'cypress.json'] });
4243
});
4344

4445
let packageJSON = {};
@@ -56,6 +57,13 @@ const archiveSpecs = (runSettings, filePath) => {
5657
archive.append(packageJSONString, { name: 'browserstack-package.json' });
5758
}
5859

60+
// do not add cypress.json if arg provided is false
61+
if (runSettings.cypress_config_file && runSettings.cypress_config_filename !== 'false') {
62+
let cypressJSON = JSON.parse(fs.readFileSync(runSettings.cypressConfigFilePath));
63+
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
64+
archive.append(cypressJSONString, { name: 'cypress.json' });
65+
}
66+
5967
archive.finalize();
6068
});
6169
}

bin/helpers/capabilityHelper.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ const caps = (bsConfig, zip) => {
6767
obj.callbackURL = bsConfig.run_settings.callback_url;
6868
obj.projectNotifyURL = bsConfig.run_settings.project_notify_URL;
6969
obj.parallels = bsConfig.run_settings.parallels;
70+
71+
if (bsConfig.run_settings.cypress_config_filename) {
72+
obj.cypress_config_filename = bsConfig.run_settings.cypress_config_filename;
73+
}
7074
}
7175

7276
if(obj.parallels === Constants.constants.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
73-
77+
7478
if (obj.project) logger.log(`Project name is: ${obj.project}`);
7579

7680
if (obj.customBuildName) logger.log(`Build name is: ${obj.customBuildName}`);
@@ -106,18 +110,24 @@ const validate = (bsConfig, args) => {
106110
// if parallels specified via arguments validate only arguments
107111
if (!Utils.isUndefined(args) && !Utils.isUndefined(args.parallels) && !Utils.isParallelValid(args.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION);
108112

109-
if (!fs.existsSync(path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json'))) reject(Constants.validationMessages.CYPRESS_JSON_NOT_FOUND + bsConfig.run_settings.cypress_proj_dir);
113+
// validate if config file provided exists or not when cypress_config_file provided
114+
// validate existing cypress_proj_dir key otherwise.
115+
let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath;
116+
117+
if (!fs.existsSync(cypressConfigFilePath) && bsConfig.run_settings.cypress_config_filename !== 'false') reject(Constants.validationMessages.CYPRESS_JSON_NOT_FOUND + cypressConfigFilePath);
110118

111119
try {
112-
let cypressJson = fs.readFileSync(path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json'));
113-
cypressJson = JSON.parse(cypressJson);
114-
// Cypress Json Base Url & Local true check
115-
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET);
116-
117-
// Detect if the user is not using the right directory structure, and throw an error
118-
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypress_proj_dir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
119-
120-
} catch (error) {
120+
if (bsConfig.run_settings.cypress_config_filename !== 'false') {
121+
let cypressJsonContent = fs.readFileSync(cypressConfigFilePath);
122+
cypressJson = JSON.parse(cypressJsonContent);
123+
124+
// Cypress Json Base Url & Local true check
125+
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET);
126+
127+
// Detect if the user is not using the right directory structure, and throw an error
128+
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypress_proj_dir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
129+
}
130+
} catch(error){
121131
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
122132
}
123133

bin/helpers/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,15 @@ exports.verifyCypressConfigFileOption = () => {
122122
}
123123

124124
exports.setCypressConfigFilename = (bsConfig, args) => {
125-
let userPassedCypessConfigFile = this.verifyCypressConfigFileOption();
125+
let userProvidedCypessConfigFile = this.verifyCypressConfigFileOption();
126126

127-
if (userPassedCypessConfigFile || this.isUndefined(bsConfig.run_settings.cypress_config_file)) {
127+
if (userProvidedCypessConfigFile || this.isUndefined(bsConfig.run_settings.cypress_config_file)) {
128128
bsConfig.run_settings.cypress_config_filename = path.basename(args.cypressConfigFile);
129129
bsConfig.run_settings.cypress_config_file = args.cypressConfigFile;
130+
bsConfig.run_settings.userProvidedCypessConfigFile = userProvidedCypessConfigFile;
130131
}
132+
133+
bsConfig.run_settings.cypressConfigFilePath = userProvidedCypessConfigFile ? bsConfig.run_settings.cypress_config_file : path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json');
131134
}
132135

133136
exports.isUndefined = value => (value === undefined || value === null);

0 commit comments

Comments
 (0)