Skip to content

Commit ec95b1c

Browse files
authored
Merge pull request #741 from browserstack/APS_8909_Enforce_Run_Settings
Changes for enforce_settings to enforce run_settings over cypress conf
2 parents 8b87783 + 51eaf05 commit ec95b1c

File tree

8 files changed

+638
-8
lines changed

8 files changed

+638
-8
lines changed

bin/commands/runs.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ module.exports = function run(args, rawArgs) {
189189
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressConfigFile);
190190
markBlockEnd('getNumberOfSpecFiles');
191191

192-
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressConfigFile);
192+
bsConfig['run_settings']['video_config'] = utils.getVideoConfig(cypressConfigFile, bsConfig);
193193

194194
// return the number of parallels user specified
195195
let userSpecifiedParallels = utils.getParallels(bsConfig, args);
@@ -251,6 +251,14 @@ module.exports = function run(args, rawArgs) {
251251
if (process.env.BROWSERSTACK_TEST_ACCESSIBILITY === 'true') {
252252
supportFileCleanup();
253253
}
254+
// Set config args for enforce_settings
255+
if ( !utils.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) ) {
256+
markBlockStart('setEnforceSettingsConfig');
257+
logger.debug('Started setting the configs');
258+
utils.setEnforceSettingsConfig(bsConfig);
259+
logger.debug('Completed setting the configs');
260+
markBlockEnd('setEnforceSettingsConfig');
261+
}
254262
// Create build
255263
//setup Local Testing
256264
markBlockStart('localSetup');

bin/helpers/capabilityHelper.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ const validate = (bsConfig, args) => {
184184
reject(Constants.validationMessages.EMPTY_CYPRESS_CONFIG_FILE);
185185
}
186186

187+
if ( bsConfig && bsConfig.run_settings && bsConfig.run_settings.enforce_settings && bsConfig.run_settings.enforce_settings.toString() === 'true' && Utils.isUndefined(bsConfig.run_settings.specs) ) {
188+
reject(Constants.validationMessages.EMPTY_SPECS_IN_BROWSERSTACK_JSON);
189+
}
190+
187191
// validate parallels specified in browserstack.json if parallels are not specified via arguments
188192
if (!Utils.isUndefined(args) && Utils.isUndefined(args.parallels) && !Utils.isParallelValid(bsConfig.run_settings.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION);
189193

@@ -214,7 +218,8 @@ const validate = (bsConfig, args) => {
214218

215219
logger.debug(`Validating ${bsConfig.run_settings.cypress_config_filename}`);
216220
try {
217-
if (bsConfig.run_settings.cypress_config_filename !== 'false') {
221+
// Not reading cypress config file upon enforce_settings
222+
if (Utils.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && bsConfig.run_settings.cypress_config_filename !== 'false') {
218223
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
219224
const completeCypressConfigFile = readCypressConfigFile(bsConfig)
220225
if (!Utils.isUndefined(completeCypressConfigFile)) {
@@ -234,6 +239,11 @@ const validate = (bsConfig, args) => {
234239
// Detect if the user is not using the right directory structure, and throw an error
235240
if (!Utils.isUndefined(cypressConfigFile.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressConfigFile.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
236241
}
242+
else {
243+
logger.debug("Validating baseurl and integrationFolder in browserstack.json");
244+
if (!Utils.isUndefined(bsConfig.run_settings.baseUrl) && bsConfig.run_settings.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", bsConfig.run_settings.baseUrl));
245+
if (!Utils.isUndefined(bsConfig.run_settings.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,bsConfig.run_settings.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
246+
}
237247
} catch(error){
238248
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
239249
}

bin/helpers/constants.js

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ const userMessages = {
7272
UPLOADING_NPM_PACKAGES_SUCCESS: "Uploaded node_modules successfully",
7373
SKIP_UPLOADING_TESTS:
7474
"Skipping zip upload since BrowserStack already has your test suite that has not changed since the last run.",
75+
SKIP_NPM_INSTALL:
76+
"Skipping NPM Install as the enforce_settings has been passed.",
7577
SKIP_UPLOADING_NPM_PACKAGES:
7678
"Skipping the upload of node_modules since BrowserStack has already cached your npm dependencies that have not changed since the last run.",
7779
LOCAL_TRUE: "you will now be able to test localhost / private URLs",
@@ -134,6 +136,8 @@ const validationMessages = {
134136
"cypress_proj_dir is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/sample-tutorial to learn more.",
135137
EMPTY_CYPRESS_CONFIG_FILE:
136138
"cypress_config_file is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/configuration-file to learn more.",
139+
EMPTY_SPECS_IN_BROWSERSTACK_JSON:
140+
"specs is required when enforce_settings is true in run_settings of browserstack.json",
137141
VALIDATED: "browserstack.json file is validated",
138142
NOT_VALID: "browerstack.json is not valid",
139143
NOT_VALID_JSON: "browerstack.json is not a valid json",

bin/helpers/packageInstaller.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ const packageSetupAndInstaller = (bsConfig, packageDir, instrumentBlocks) => {
133133
let obj = {
134134
packagesInstalled: false
135135
};
136-
136+
if (bsConfig && bsConfig.run_settings && bsConfig.run_settings.enforce_settings && bsConfig.run_settings.enforce_settings.toString() === 'true' ) {
137+
logger.info("Enforce_settings is enabled in run_settings");
138+
logger.debug(Constants.userMessages.SKIP_NPM_INSTALL);
139+
return resolve(obj);
140+
}
137141
logger.info(Constants.userMessages.NPM_INSTALL);
138142
instrumentBlocks.markBlockStart("packageInstaller.folderSetup");
139143
logger.debug("Started setting up package folder");

bin/helpers/utils.js

+62-5
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,8 @@ exports.setProjectId = (bsConfig, args, cypressConfigFile) => {
434434
} else if(!this.isUndefined(bsConfig.run_settings["projectId"])) {
435435
return bsConfig.run_settings["projectId"];
436436
} else {
437-
if (!this.isUndefined(cypressConfigFile) && !this.isUndefined(cypressConfigFile["projectId"])) {
437+
// ignore reading cypressconfig if enforce_settings is passed
438+
if (this.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && !this.isUndefined(cypressConfigFile) && !this.isUndefined(cypressConfigFile["projectId"])) {
438439
return cypressConfigFile["projectId"];
439440
}
440441
}
@@ -628,6 +629,8 @@ exports.isPositiveInteger = (str) => {
628629

629630
exports.isTrueString = value => (!this.isUndefined(value) && value.toString().toLowerCase() === 'true');
630631

632+
exports.isUndefinedOrFalse = value => ( this.isUndefined(value) || value.toString().toLowerCase() === 'false');
633+
631634
exports.isFloat = (value) => Number(value) && Number(value) % 1 !== 0;
632635

633636
exports.isInteger = (value) => Number.isInteger(value);
@@ -1077,7 +1080,8 @@ exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
10771080
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
10781081
defaultSpecFolder = Constants.DEFAULT_CYPRESS_10_SPEC_PATH
10791082
testFolderPath = defaultSpecFolder
1080-
if(!this.isUndefined(cypressConfig) && !this.isUndefined(cypressConfig.e2e)) {
1083+
// Read cypress config if enforce_settings is not present
1084+
if(this.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && !this.isUndefined(cypressConfig) && !this.isUndefined(cypressConfig.e2e)) {
10811085
if(!this.isUndefined(cypressConfig.e2e.specPattern)) {
10821086
globCypressConfigSpecPatterns = Array.isArray(cypressConfig.e2e.specPattern) ?
10831087
cypressConfig.e2e.specPattern : [cypressConfig.e2e.specPattern];
@@ -1286,6 +1290,50 @@ exports.setConfig = (bsConfig, args) => {
12861290
}
12871291
}
12881292

1293+
exports.setVideoCliConfig = (bsConfig, videoConfig) => {
1294+
// set cli config for video for cypress 13 and above to attain default value of true.
1295+
if(this.isUndefined(videoConfig) || this.isUndefined(videoConfig.video) || this.isUndefined(videoConfig.videoUploadOnPasses) || this.isUndefined(bsConfig)) return;
1296+
let user_cypress_version = (bsConfig && bsConfig.run_settings && bsConfig.run_settings.cypress_version) ? bsConfig.run_settings.cypress_version.toString() : undefined;
1297+
let cypress_major_version = (user_cypress_version && user_cypress_version.match(/^(\d+)/)) ? user_cypress_version.split(".")[0] : undefined;
1298+
let config_args = (bsConfig && bsConfig.run_settings && bsConfig.run_settings.config) ? bsConfig.run_settings.config : undefined;
1299+
if(this.isUndefined(user_cypress_version) || this.isUndefined(cypress_major_version) || parseInt(cypress_major_version) >= 13 ) {
1300+
let video_args = `video=${videoConfig.video},videoUploadOnPasses=${videoConfig.videoUploadOnPasses}`;
1301+
config_args = this.isUndefined(config_args) ? video_args : config_args + ',' + video_args;
1302+
logger.debug(`Setting default video true for cypress 13 and above in cli for cypress version ${user_cypress_version} with cli args - ${config_args}`)
1303+
}
1304+
if (bsConfig.run_settings && this.isNotUndefined(config_args)) bsConfig["run_settings"]["config"] = config_args;
1305+
}
1306+
1307+
// set configs if enforce_settings is passed
1308+
exports.setEnforceSettingsConfig = (bsConfig) => {
1309+
if ( this.isUndefined(bsConfig) || this.isUndefined(bsConfig.run_settings) ) return;
1310+
let config_args = (bsConfig && bsConfig.run_settings && bsConfig.run_settings.config) ? bsConfig.run_settings.config : undefined;
1311+
if ( this.isUndefined(config_args) || !config_args.includes("video") ) {
1312+
let video_args = (this.isUndefined(bsConfig.run_settings.video_config) || this.isUndefined(bsConfig.run_settings.video_config.video) || !bsConfig.run_settings.video_config.video ) ? 'video=false' : 'video=true' ;
1313+
video_args += (this.isUndefined(bsConfig.run_settings.video_config) || this.isUndefined(bsConfig.run_settings.video_config.videoUploadOnPasses) || !bsConfig.run_settings.video_config.videoUploadOnPasses ) ? ',videoUploadOnPasses=false' : ',videoUploadOnPasses=true';
1314+
config_args = this.isUndefined(config_args) ? video_args : config_args + ',' + video_args;
1315+
logger.debug(`Setting video_args for enforce_settings to ${video_args}`);
1316+
}
1317+
if ( (bsConfig && bsConfig.run_settings && bsConfig.run_settings.baseUrl) && (this.isUndefined(config_args) || !config_args.includes("baseUrl")) ) {
1318+
let base_url_args = 'baseUrl='+bsConfig.run_settings.baseUrl;
1319+
config_args = this.isUndefined(config_args) ? base_url_args : config_args + ',' + base_url_args;
1320+
logger.debug(`Setting base_url_args for enforce_settings to ${base_url_args}`);
1321+
}
1322+
// set specs in config of specpattern to override cypress config
1323+
if( this.isNotUndefined(bsConfig.run_settings.specs) && bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE && (this.isUndefined(config_args) || !config_args.includes("specPattern")) ) {
1324+
// doing this only for cypress 10 and above as --spec is given precedence for cypress 9.
1325+
let specConfigs = bsConfig.run_settings.specs;
1326+
// if multiple specs are passed, convert it into an array.
1327+
if(specConfigs && specConfigs.includes(',')) {
1328+
specConfigs = JSON.stringify(specConfigs.split(','));
1329+
}
1330+
let spec_pattern_args = 'specPattern="'+specConfigs+'"';
1331+
config_args = this.isUndefined(config_args) ? spec_pattern_args : config_args + ',' + spec_pattern_args;
1332+
}
1333+
if ( this.isNotUndefined(config_args) ) bsConfig["run_settings"]["config"] = config_args;
1334+
logger.debug(`Setting conifg_args for enforce_settings to ${config_args}`);
1335+
}
1336+
12891337
// blindly send other passed configs with run_settings and handle at backend
12901338
exports.setOtherConfigs = (bsConfig, args) => {
12911339
if(o11yHelpers.isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
@@ -1522,14 +1570,23 @@ exports.fetchFolderSize = async (dir) => {
15221570
}
15231571
}
15241572

1525-
exports.getVideoConfig = (cypressConfig) => {
1573+
exports.getVideoConfig = (cypressConfig, bsConfig = {}) => {
15261574
let conf = {
15271575
video: true,
15281576
videoUploadOnPasses: true
15291577
}
1530-
if (!this.isUndefined(cypressConfig.video)) conf.video = cypressConfig.video;
1531-
if (!this.isUndefined(cypressConfig.videoUploadOnPasses)) conf.videoUploadOnPasses = cypressConfig.videoUploadOnPasses;
1578+
// Reading bsconfig in case of enforce_settings
1579+
if ( this.isUndefined(bsConfig.run_settings) || this.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) ) {
1580+
if (!this.isUndefined(cypressConfig.video)) conf.video = cypressConfig.video;
1581+
if (!this.isUndefined(cypressConfig.videoUploadOnPasses)) conf.videoUploadOnPasses = cypressConfig.videoUploadOnPasses;
1582+
}
1583+
else {
1584+
if (!this.isUndefined(bsConfig.run_settings) && !this.isUndefined(bsConfig.run_settings.video)) conf.video = bsConfig.run_settings.video;
1585+
if (!this.isUndefined(bsConfig.run_settings) && !this.isUndefined(bsConfig.run_settings.videoUploadOnPasses)) conf.videoUploadOnPasses = bsConfig.run_settings.videoUploadOnPasses;
1586+
}
15321587

1588+
// set video in cli config in case of cypress 13 or above as default value is false there.
1589+
this.setVideoCliConfig(bsConfig,conf);
15331590
logger.debug(`Setting video = ${conf.video}`);
15341591
logger.debug(`Setting videoUploadOnPasses = ${conf.videoUploadOnPasses}`);
15351592
return conf;

0 commit comments

Comments
 (0)