Skip to content

Commit d8b4644

Browse files
committed
add more validations for cyp 10.3.0
1 parent 08f83f5 commit d8b4644

File tree

7 files changed

+84
-47
lines changed

7 files changed

+84
-47
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ module.exports = function run(args, rawArgs) {
120120
// Validate browserstack.json values and parallels specified via arguments
121121
markBlockStart('validateConfig');
122122
logger.debug("Started configs validation");
123-
return capabilityHelper.validate(bsConfig, args).then(function (cypressJson) {
123+
return capabilityHelper.validate(bsConfig, args).then(function (cypressConfigFile) {
124124
markBlockEnd('validateConfig');
125125
logger.debug("Completed configs validation");
126126
markBlockStart('preArchiveSteps');
127127
logger.debug("Started pre-archive steps");
128128
//get the number of spec files
129-
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressJson);
129+
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressConfigFile);
130130

131-
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressJson);
131+
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressConfigFile);
132132

133133
// return the number of parallels user specified
134134
let userSpecifiedParallels = utils.getParallels(bsConfig, args);

bin/helpers/archiver.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ const archiveSpecs = (runSettings, filePath, excludeFiles, md5data) => {
8585
let cypressConfigFileString = fs.readFileSync(runSettings.cypressConfigFilePath, {encoding: "utf-8"});
8686
archive.append(cypressConfigFileString, {name: `${cypressAppendFilesZipLocation}${runSettings.cypress_config_filename}`});
8787
} else if (runSettings.cypressTestSuiteType === Constants.CYPRESS_V9_AND_OLDER_TYPE) {
88-
let cypressJSON = JSON.parse(
89-
fs.readFileSync(runSettings.cypressConfigFilePath)
90-
);
88+
let cypressJSON = JSON.parse(fs.readFileSync(runSettings.cypressConfigFilePath));
9189
let cypressJSONString = JSON.stringify(cypressJSON, null, 4);
9290
archive.append(cypressJSONString, {name: `${cypressAppendFilesZipLocation}cypress.json`});
9391
}

bin/helpers/buildArtifacts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,13 @@ const unzipFile = async (filePath, fileName) => {
137137
const sendUpdatesToBstack = async (bsConfig, buildId, args, options, rawArgs, buildReportData) => {
138138
options.url = `${config.buildUrl}${buildId}/build_artifacts/status`;
139139

140-
let cypressJSON = utils.getCypressJSON(bsConfig);
140+
let cypressConfigFile = utils.getCypressConfigFile(bsConfig);
141141

142142
let reporter = null;
143143
if(!utils.isUndefined(args.reporter)) {
144144
reporter = args.reporter;
145-
} else if(cypressJSON !== undefined){
146-
reporter = cypressJSON.reporter;
145+
} else if(cypressConfigFile !== undefined){
146+
reporter = cypressConfigFile.reporter;
147147
}
148148

149149
let data = {

bin/helpers/capabilityHelper.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ const validate = (bsConfig, args) => {
189189
// validate if config file provided exists or not when cypress_config_file provided
190190
// validate the cypressProjectDir key otherwise.
191191
let cypressConfigFilePath = bsConfig.run_settings.cypressConfigFilePath;
192-
let cypressJson = {};
192+
let cypressConfigFile = {};
193193

194194
logger.debug(`Checking for cypress config file at ${cypressConfigFilePath}`);
195195
if (!fs.existsSync(cypressConfigFilePath) && bsConfig.run_settings.cypress_config_filename !== 'false') reject(Constants.validationMessages.INVALID_CYPRESS_CONFIG_FILE);
@@ -201,14 +201,23 @@ const validate = (bsConfig, args) => {
201201
logger.debug("Validating cypress.json");
202202
try {
203203
if (bsConfig.run_settings.cypress_config_filename !== 'false') {
204-
let cypressJsonContent = fs.readFileSync(cypressConfigFilePath);
205-
cypressJson = JSON.parse(cypressJsonContent);
204+
205+
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
206+
if (cypressConfigFilePath.endsWith("cypress.config.js")) {
207+
cypressConfigFile = require(cypressConfigFilePath);
208+
} else {
209+
cypressConfigFile = {};
210+
}
211+
} else {
212+
let cypressJsonContent = fs.readFileSync(cypressConfigFilePath);
213+
cypressConfigFile = JSON.parse(cypressJsonContent);
214+
}
206215

207216
// Cypress Json Base Url & Local true check
208-
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", cypressJson.baseUrl));
217+
if (!Utils.isUndefined(cypressConfigFile.baseUrl) && cypressConfigFile.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", cypressConfigFile.baseUrl));
209218

210219
// Detect if the user is not using the right directory structure, and throw an error
211-
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
220+
if (!Utils.isUndefined(cypressConfigFile.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressConfigFile.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
212221
}
213222
} catch(error){
214223
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
@@ -236,6 +245,22 @@ const validate = (bsConfig, args) => {
236245
addCypressZipStartLocation(bsConfig.run_settings);
237246
}
238247

248+
// check if two config files are present at the same location
249+
let cypressFileDirectory = path.dirname(cypressConfigFilePath);
250+
let listOfFiles = fs.readdirSync(cypressFileDirectory);
251+
let configFilesPresent = [];
252+
for (const possibleCypressFileName of Constants.CYPRESS_CONFIG_FILE_NAMES) {
253+
if (listOfFiles.includes(possibleCypressFileName)) {
254+
configFilesPresent.push(possibleCypressFileName);
255+
}
256+
}
257+
258+
if (configFilesPresent.length === 0) reject(Constants.validationMessages.CYPRESS_CONFIG_FILE_NOT_FOUND)
259+
if (configFilesPresent.length > 1) {
260+
logger.warn(`We found the following cypress config files ${configFilesPresent.join(', ')} at this location: ${cypressFileDirectory}`);
261+
reject(Constants.validationMessages.MORE_THAN_ONE_CYPRESS_CONFIG_FILE_FOUND);
262+
}
263+
239264
if(!Utils.isUndefined(bsConfig.run_settings.spec_timeout)) {
240265
if(Utils.isPositiveInteger(bsConfig.run_settings.spec_timeout.toString().trim())) {
241266
if(Number(bsConfig.run_settings.spec_timeout) > Constants.SPEC_TIMEOUT_LIMIT) {
@@ -262,7 +287,7 @@ const validate = (bsConfig, args) => {
262287
if (!Utils.isUndefined(bsConfig.run_settings.nodeVersion) && typeof(bsConfig.run_settings.nodeVersion) === 'string' && !bsConfig.run_settings.nodeVersion.match(/^(\d+\.)?(\d+\.)?(\*|\d+)$/))
263288
logger.warn(Constants.validationMessages.NODE_VERSION_PARSING_ERROR);
264289

265-
resolve(cypressJson);
290+
resolve(cypressConfigFile);
266291
});
267292
}
268293

bin/helpers/checkUploaded.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ const checkSpecsMd5 = (runSettings, args, instrumentBlocks) => {
3737
runSettings.cypress_config_file &&
3838
runSettings.cypress_config_filename !== 'false'
3939
) {
40-
let cypressJSON = JSON.parse(
41-
fs.readFileSync(runSettings.cypressConfigFilePath)
42-
);
43-
let cypressJSONString = JSON.stringify(cypressJSON);
44-
outputHash.update(cypressJSONString);
40+
let cypressConfigFileString = "";
41+
if (runSettings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
42+
cypressConfigFileString = fs.readFileSync(runSettings.cypressConfigFilePath).toString();
43+
} else {
44+
let cypressJSON = JSON.parse(fs.readFileSync(runSettings.cypressConfigFilePath));
45+
cypressConfigFileString = JSON.stringify(cypressJSON);
46+
}
47+
outputHash.update(cypressConfigFileString);
4548
}
4649
resolve(outputHash.digest(Constants.hashingOptions.encoding));
4750
}).catch(function (error) {

bin/helpers/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ const validationMessages = {
8383
INVALID_EXTENSION: "Invalid files, please remove these files and try again.",
8484
INVALID_PARALLELS_CONFIGURATION: "Invalid value specified for parallels to use. Maximum parallels to use should be a number greater than 0.",
8585
INVALID_CYPRESS_CONFIG_FILE: "Invalid cypress_config_file",
86-
CYPRESS_JSON_NOT_FOUND: "cypress.json file is not found at cypress_proj_dir path ",
86+
CYPRESS_CONFIG_FILE_NOT_FOUND: "No cypress config file was found.",
87+
MORE_THAN_ONE_CYPRESS_CONFIG_FILE_FOUND: "Cypress does not allow more than one cypress config file.",
8788
INVALID_CYPRESS_JSON: "cypress.json is not a valid json",
8889
INVALID_DEFAULT_AUTH_PARAMS: "Your username and access key are required to run your tests on BrowserStack. Learn more at https://www.browserstack.com/docs/automate/cypress/authentication",
8990
LOCAL_NOT_SET: "To test <baseUrlValue> on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing",

bin/helpers/utils.js

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,14 @@ exports.setCypressConfigFilename = (bsConfig, args) => {
292292
bsConfig.run_settings.cypressProjectDir = path.dirname(bsConfig.run_settings.cypress_config_file);
293293
} else {
294294
logger.debug(`Looks like cypress config file was not provided. Looking for ${Constants.CYPRESS_CONFIG_FILE_NAMES.join(", ")} files at ${process.cwd()}`);
295-
for (const possible_cypress_file_name of Constants.CYPRESS_CONFIG_FILE_NAMES) {
295+
for (const possibleCypressFileName of Constants.CYPRESS_CONFIG_FILE_NAMES) {
296296
let directoryPath = !this.isUndefined(bsConfig.run_settings.cypress_proj_dir) ? bsConfig.run_settings.cypress_proj_dir : process.cwd();
297297
if (directoryPath.endsWith("/")) {
298-
directoryPath
298+
directoryPath = directoryPath.slice(0,-1);
299299
}
300-
if (fs.existsSync(path.join(directoryPath, possible_cypress_file_name))) {
301-
bsConfig.run_settings.cypressConfigFilePath = `${directoryPath}/${possible_cypress_file_name}`;
302-
bsConfig.run_settings.cypress_config_file = `${directoryPath}/${possible_cypress_file_name}`;
300+
if (fs.existsSync(path.join(directoryPath, possibleCypressFileName))) {
301+
bsConfig.run_settings.cypressConfigFilePath = `${directoryPath}/${possibleCypressFileName}`;
302+
bsConfig.run_settings.cypress_config_file = `${directoryPath}/${possibleCypressFileName}`;
303303
bsConfig.run_settings.cypress_config_filename = path.basename(bsConfig.run_settings.cypress_config_file);
304304
bsConfig.run_settings.cypressProjectDir = directoryPath;
305305
break;
@@ -312,11 +312,11 @@ exports.setCypressConfigFilename = (bsConfig, args) => {
312312
}
313313

314314
exports.setCypressTestSuiteType = (bsConfig) => {
315-
for (const possible_cypress_file_name of Constants.CYPRESS_CONFIG_FILE_NAMES) {
315+
for (const possibleCypressFileName of Constants.CYPRESS_CONFIG_FILE_NAMES) {
316316
if (bsConfig.run_settings.cypressConfigFilePath &&
317317
typeof(bsConfig.run_settings.cypressConfigFilePath) === 'string' &&
318-
bsConfig.run_settings.cypressConfigFilePath.endsWith(possible_cypress_file_name)) {
319-
bsConfig.run_settings.cypressTestSuiteType = Constants.CYPRESS_CONFIG_FILE_MAPPING[possible_cypress_file_name].type;
318+
bsConfig.run_settings.cypressConfigFilePath.endsWith(possibleCypressFileName)) {
319+
bsConfig.run_settings.cypressTestSuiteType = Constants.CYPRESS_CONFIG_FILE_MAPPING[possibleCypressFileName].type;
320320
break;
321321
}
322322
}
@@ -393,8 +393,10 @@ exports.setProjectId = (bsConfig, args) => {
393393
} else if(!this.isUndefined(bsConfig.run_settings["projectId"])) {
394394
return bsConfig.run_settings["projectId"];
395395
} else {
396-
let cypressJson = this.getCypressJSON(bsConfig);
397-
if (!this.isUndefined(cypressJson) && !this.isUndefined(cypressJson["projectId"])) { return cypressJson["projectId"]; }
396+
let cypressConfigFile = this.getCypressConfigFile(bsConfig);
397+
if (!this.isUndefined(cypressConfigFile) && !this.isUndefined(cypressConfigFile["projectId"])) {
398+
return cypressConfigFile["projectId"];
399+
}
398400
}
399401
}
400402

@@ -950,8 +952,8 @@ exports.getFilesToIgnore = (runSettings, excludeFiles, logging = true) => {
950952
return ignoreFiles;
951953
}
952954

953-
exports.getNumberOfSpecFiles = (bsConfig, args, cypressJson) => {
954-
let testFolderPath = cypressJson.integrationFolder || Constants.DEFAULT_CYPRESS_SPEC_PATH;
955+
exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
956+
let testFolderPath = cypressConfig.integrationFolder || Constants.DEFAULT_CYPRESS_SPEC_PATH;
955957
let globSearchPattern = this.sanitizeSpecsPattern(bsConfig.run_settings.specs) || `${testFolderPath}/**/*.+(${Constants.specFileTypes.join("|")})`;
956958
let ignoreFiles = args.exclude || bsConfig.run_settings.exclude;
957959
let files = glob.sync(globSearchPattern, {cwd: bsConfig.run_settings.cypressProjectDir, matchBase: true, ignore: ignoreFiles});
@@ -1109,18 +1111,26 @@ exports.readBsConfigJSON = (bsConfigPath) => {
11091111
}
11101112
}
11111113

1112-
exports.getCypressJSON = (bsConfig) => {
1113-
let cypressJSON = undefined;
1114-
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1115-
cypressJSON = JSON.parse(
1116-
fs.readFileSync(bsConfig.run_settings.cypressConfigFilePath)
1117-
);
1118-
} else if (bsConfig.run_settings.cypressProjectDir) {
1119-
cypressJSON = JSON.parse(
1120-
fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename))
1121-
);
1114+
exports.getCypressConfigFile = (bsConfig) => {
1115+
let cypressConfigFile = undefined;
1116+
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
1117+
if (bsConfig.run_settings.cypress_config_filename.endsWith("cypress.config.js")) {
1118+
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1119+
cypressConfigFile = require(path.resolve(bsConfig.run_settings.cypressConfigFilePath));
1120+
} else if (bsConfig.run_settings.cypressProjectDir) {
1121+
cypressConfigFile = require(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename));
1122+
}
1123+
} else {
1124+
cypressConfigFile = {};
1125+
}
1126+
} else {
1127+
if (bsConfig.run_settings.cypress_config_file && bsConfig.run_settings.cypress_config_filename !== 'false') {
1128+
cypressConfigFile = JSON.parse(fs.readFileSync(bsConfig.run_settings.cypressConfigFilePath))
1129+
} else if (bsConfig.run_settings.cypressProjectDir) {
1130+
cypressConfigFile = JSON.parse(fs.readFileSync(path.join(bsConfig.run_settings.cypressProjectDir, bsConfig.run_settings.cypress_config_filename)));
1131+
}
11221132
}
1123-
return cypressJSON;
1133+
return cypressConfigFile;
11241134
}
11251135

11261136
exports.setCLIMode = (bsConfig, args) => {
@@ -1254,13 +1264,13 @@ exports.fetchZipSize = (fileName) => {
12541264
}
12551265
}
12561266

1257-
exports.getVideoConfig = (cypressJson) => {
1267+
exports.getVideoConfig = (cypressConfig) => {
12581268
let conf = {
12591269
video: true,
12601270
videoUploadOnPasses: true
12611271
}
1262-
if (!this.isUndefined(cypressJson.video)) conf.video = cypressJson.video;
1263-
if (!this.isUndefined(cypressJson.videoUploadOnPasses)) conf.videoUploadOnPasses = cypressJson.videoUploadOnPasses;
1272+
if (!this.isUndefined(cypressConfig.video)) conf.video = cypressConfig.video;
1273+
if (!this.isUndefined(cypressConfig.videoUploadOnPasses)) conf.videoUploadOnPasses = cypressConfig.videoUploadOnPasses;
12641274

12651275
logger.debug(`Setting video = ${conf.video}`);
12661276
logger.debug(`Setting videoUploadOnPasses = ${conf.videoUploadOnPasses}`);

0 commit comments

Comments
 (0)