Skip to content

Commit 6c20b8b

Browse files
Changes for enforce_settings to enforce run_settings over cypress conf
1 parent 5f2a9f8 commit 6c20b8b

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

bin/commands/runs.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ module.exports = function run(args, rawArgs) {
186186
let specFiles = utils.getNumberOfSpecFiles(bsConfig, args, cypressConfigFile);
187187
markBlockEnd('getNumberOfSpecFiles');
188188

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

191191
// return the number of parallels user specified
192192
let userSpecifiedParallels = utils.getParallels(bsConfig, args);
@@ -248,6 +248,14 @@ module.exports = function run(args, rawArgs) {
248248
if (process.env.BROWSERSTACK_TEST_ACCESSIBILITY === 'true') {
249249
supportFileCleanup();
250250
}
251+
// Set config args for enforce_settings
252+
if ( !utils.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) ) {
253+
markBlockStart('setEnforceSettingsConfig');
254+
logger.debug('Started setting the configs');
255+
utils.setEnforceSettingsConfig(bsConfig);
256+
logger.debug('Completed setting the configs');
257+
markBlockEnd('setEnforceSettingsConfig');
258+
}
251259
// Create build
252260
//setup Local Testing
253261
markBlockStart('localSetup');

bin/helpers/capabilityHelper.js

Lines changed: 9 additions & 1 deletion
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,9 @@ 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+
logger.debug("Validating baseurl and integrationFolder in browserstack.json");
243+
if (!Utils.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && !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));
244+
if (!Utils.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && !Utils.isUndefined(bsConfig.run_settings.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,bsConfig.run_settings.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
237245
} catch(error){
238246
reject(Constants.validationMessages.INVALID_CYPRESS_JSON)
239247
}

bin/helpers/constants.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const userMessages = {
7070
UPLOADING_TESTS_SUCCESS: "Uploaded tests successfully",
7171
UPLOADING_NPM_PACKAGES: "Uploading required node_modules to BrowserStack",
7272
UPLOADING_NPM_PACKAGES_SUCCESS: "Uploaded node_modules successfully",
73+
SKIP_CYPRESSCONFIG_INSTALL:
74+
"Skipping Cypress Config Install as the enforce_settings has been passed.",
7375
SKIP_UPLOADING_TESTS:
7476
"Skipping zip upload since BrowserStack already has your test suite that has not changed since the last run.",
7577
SKIP_UPLOADING_NPM_PACKAGES:
@@ -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+
"No specs have been provided in run_settings. It is required to be passed on browserstack.json if enforce_settings is set.",
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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ 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(Constants.userMessages.SKIP_NPM_INSTALL);
138+
return resolve(obj);
139+
}
137140
logger.info(Constants.userMessages.NPM_INSTALL);
138141
instrumentBlocks.markBlockStart("packageInstaller.folderSetup");
139142
logger.debug("Started setting up package folder");

bin/helpers/utils.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ exports.isPositiveInteger = (str) => {
628628

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

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

633635
exports.isInteger = (value) => Number.isInteger(value);
@@ -1077,7 +1079,8 @@ exports.getNumberOfSpecFiles = (bsConfig, args, cypressConfig) => {
10771079
if (bsConfig.run_settings.cypressTestSuiteType === Constants.CYPRESS_V10_AND_ABOVE_TYPE) {
10781080
defaultSpecFolder = Constants.DEFAULT_CYPRESS_10_SPEC_PATH
10791081
testFolderPath = defaultSpecFolder
1080-
if(!this.isUndefined(cypressConfig) && !this.isUndefined(cypressConfig.e2e)) {
1082+
// Read cypress config if enforce_settings is not present
1083+
if(this.isUndefinedOrFalse(bsConfig.run_settings.enforce_settings) && !this.isUndefined(cypressConfig) && !this.isUndefined(cypressConfig.e2e)) {
10811084
if(!this.isUndefined(cypressConfig.e2e.specPattern)) {
10821085
globCypressConfigSpecPatterns = Array.isArray(cypressConfig.e2e.specPattern) ?
10831086
cypressConfig.e2e.specPattern : [cypressConfig.e2e.specPattern];
@@ -1286,6 +1289,24 @@ exports.setConfig = (bsConfig, args) => {
12861289
}
12871290
}
12881291

1292+
// set configs if enforce_settings is passed
1293+
exports.setEnforceSettingsConfig = (bsConfig) => {
1294+
let config_args = (!this.isUndefined(bsConfig) && !this.isUndefined(bsConfig.run_settings) && !this.isUndefined(bsConfig.run_settings.config)) ? bsConfig.run_settings.config : undefined;
1295+
if ( this.isUndefined(config_args) || !config_args.includes("video") ) {
1296+
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' ;
1297+
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';
1298+
config_args = this.isUndefined(config_args) ? video_args : config_args + ',' + video_args;
1299+
logger.debug(`Setting video_args for enforce_settings to ${video_args}`);
1300+
}
1301+
if ( this.isUndefined(config_args) || !config_args.includes("baseUrl") ) {
1302+
let base_url_args = (!this.isUndefined(bsConfig) && !this.isUndefined(bsConfig.run_settings) && !this.isUndefined(bsConfig.run_settings.config)) ? "baseUrl='"+bsConfig.run_settings.baseUrl+"'" : "baseUrl=''";
1303+
config_args = this.isUndefined(config_args) ? base_url_args : config_args + ',' + base_url_args;
1304+
logger.debug(`Setting base_url_args for enforce_settings to ${base_url_args}`);
1305+
}
1306+
if ( !this.isUndefined(config_args) ) bsConfig["run_settings"]["config"] = config_args;
1307+
logger.debug(`Setting conifg_args for enforce_settings to ${config_args}`);
1308+
}
1309+
12891310
// blindly send other passed configs with run_settings and handle at backend
12901311
exports.setOtherConfigs = (bsConfig, args) => {
12911312
if(isTestObservabilitySession() && process.env.BS_TESTOPS_JWT) {
@@ -1499,11 +1520,14 @@ exports.fetchFolderSize = async (dir) => {
14991520
}
15001521
}
15011522

1502-
exports.getVideoConfig = (cypressConfig) => {
1523+
exports.getVideoConfig = (cypressConfig, bsConfig = {}) => {
15031524
let conf = {
15041525
video: true,
15051526
videoUploadOnPasses: true
15061527
}
1528+
// Reading from bsconfig first to give precedance and cypress config will be empty in case of enforce_settings
1529+
if (!this.isUndefined(bsConfig.run_settings.video)) conf.video = bsConfig.run_settings.video;
1530+
if (!this.isUndefined(bsConfig.run_settings.videoUploadOnPasses)) conf.videoUploadOnPasses = bsConfig.run_settings.videoUploadOnPasses;
15071531
if (!this.isUndefined(cypressConfig.video)) conf.video = cypressConfig.video;
15081532
if (!this.isUndefined(cypressConfig.videoUploadOnPasses)) conf.videoUploadOnPasses = cypressConfig.videoUploadOnPasses;
15091533

0 commit comments

Comments
 (0)