Skip to content

Commit 0446485

Browse files
committed
Merge branch 'master' into specs-option
2 parents 391e88a + dc7e70d commit 0446485

File tree

8 files changed

+238
-25
lines changed

8 files changed

+238
-25
lines changed

bin/commands/runs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ module.exports = function run(args) {
4747
return build.createBuild(bsConfig, zip).then(function (data) {
4848
let message = `${data.message}! ${Constants.userMessages.BUILD_CREATED} with build id: ${data.build_id}`;
4949
let dashboardLink = `${Constants.userMessages.VISIT_DASHBOARD} ${config.dashboardUrl}${data.build_id}`;
50+
if ((utils.isUndefined(bsConfig.run_settings.parallels) && utils.isUndefined(args.parallels)) || (!utils.isUndefined(bsConfig.run_settings.parallels) && bsConfig.run_settings.parallels == Constants.constants.DEFAULT_PARALLEL_MESSAGE)) {
51+
logger.warn(Constants.userMessages.NO_PARALLELS);
52+
}
53+
54+
if(!args.disableNpmWarning && bsConfig.run_settings.npm_dependencies && Object.keys(bsConfig.run_settings.npm_dependencies).length <= 0) logger.warn(Constants.userMessages.NO_NPM_DEPENDENCIES);
55+
5056
logger.info(message);
5157
logger.info(dashboardLink);
5258
utils.sendUsageReport(bsConfig, args, `${message}\n${dashboardLink}`, Constants.messageTypes.SUCCESS, null);

bin/helpers/capabilityHelper.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ const caps = (bsConfig, zip) => {
4747

4848
// Local Identifier
4949
obj.localIdentifier = null;
50-
if (obj.local === true && (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier))
51-
{
50+
if (obj.local === true && (bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier)) {
5251
obj.localIdentifier = bsConfig.connection_settings.localIdentifier || bsConfig.connection_settings.local_identifier;
5352
logger.log(`Local Identifier is set to: ${obj.localIdentifier}`);
5453
}
@@ -78,6 +77,8 @@ const caps = (bsConfig, zip) => {
7877
}
7978
}
8079

80+
if(obj.parallels === Constants.constants.DEFAULT_PARALLEL_MESSAGE) obj.parallels = undefined
81+
8182
if (obj.project) logger.log(`Project name is: ${obj.project}`);
8283

8384
if (obj.customBuildName) logger.log(`Build name is: ${obj.customBuildName}`);
@@ -94,16 +95,18 @@ const caps = (bsConfig, zip) => {
9495
}
9596

9697
const validate = (bsConfig, args) => {
97-
return new Promise(function(resolve, reject){
98+
return new Promise(function (resolve, reject) {
9899
if (!bsConfig) reject(Constants.validationMessages.EMPTY_BROWSERSTACK_JSON);
99100

100101
if (!bsConfig.auth) reject(Constants.validationMessages.INCORRECT_AUTH_PARAMS);
101102

103+
if( bsConfig.auth.username == "<Your BrowserStack username>" || bsConfig.auth.access_key == "<Your BrowserStack access key>" ) reject(Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS);
104+
102105
if (!bsConfig.browsers || bsConfig.browsers.length === 0) reject(Constants.validationMessages.EMPTY_BROWSER_LIST);
103106

104107
if (!bsConfig.run_settings) reject(Constants.validationMessages.EMPTY_RUN_SETTINGS);
105108

106-
if (!bsConfig.run_settings.cypress_proj_dir) reject(Constants.validationMessages.EMPTY_SPEC_FILES);
109+
if (!bsConfig.run_settings.cypress_proj_dir) reject(Constants.validationMessages.EMPTY_CYPRESS_PROJ_DIR);
107110

108111
// validate parallels specified in browserstack.json if parallels are not specified via arguments
109112
if (!Utils.isUndefined(args) && Utils.isUndefined(args.parallels) && !Utils.isParallelValid(bsConfig.run_settings.parallels)) reject(Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION);
@@ -113,10 +116,16 @@ const validate = (bsConfig, args) => {
113116

114117
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);
115118

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

bin/helpers/constants.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const userMessages = {
1212
ZIP_DELETED: "Zip file deleted successfully.",
1313
API_DEPRECATED: "This version of API is deprecated, please use latest version of API.",
1414
FAILED_TO_ZIP: "Failed to zip files.",
15-
VISIT_DASHBOARD: "Visit the Automate dashboard for test reporting:"
15+
VISIT_DASHBOARD: "Visit the Automate dashboard for test reporting:",
16+
NO_PARALLELS: "Your tests will run sequentially. Read more about running your tests in parallel here: https://www.browserstack.com/docs/automate/cypress/run-tests-in-parallel",
17+
NO_NPM_DEPENDENCIES: "No npm dependencies specified. Read more here: https://www.browserstack.com/docs/automate/cypress/npm-packages. You can suppress this warning by using --disable-npm-warning flag."
1618
};
1719

1820
const validationMessages = {
@@ -21,14 +23,17 @@ const validationMessages = {
2123
EMPTY_TEST_SUITE: "Test suite is empty",
2224
EMPTY_BROWSERSTACK_JSON: "Empty browserstack.json",
2325
EMPTY_RUN_SETTINGS: "Empty run settings",
24-
EMPTY_SPEC_FILES: "No spec files specified in run_settings",
26+
EMPTY_CYPRESS_PROJ_DIR: "cypress_proj_dir is not set in run_settings. See https://www.browserstack.com/docs/automate/cypress/sample-tutorial to learn more.",
2527
VALIDATED: "browserstack.json file is validated",
2628
NOT_VALID: "browerstack.json is not valid",
2729
NOT_VALID_JSON: "browerstack.json is not a valid json",
2830
INVALID_EXTENSION: "Invalid files, please remove these files and try again.",
2931
INVALID_PARALLELS_CONFIGURATION: "Invalid value specified for parallels to use. Maximum parallels to use should be a number greater than 0.",
3032
CYPRESS_JSON_NOT_FOUND: "cypress.json file is not found at cypress_proj_dir path ",
31-
INVALID_CYPRESS_JSON: "cypress.json is not a valid json"
33+
INVALID_CYPRESS_JSON: "cypress.json is not a valid json",
34+
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",
35+
LOCAL_NOT_SET: "To test <baseUrl value> on BrowserStack, you will have to set up Local testing. Read more here: https://www.browserstack.com/docs/automate/cypress/local-testing",
36+
INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists."
3237
};
3338

3439
const cliMessages = {
@@ -62,7 +67,8 @@ const cliMessages = {
6267
COMMON: {
6368
DISABLE_USAGE_REPORTING: "Disable usage reporting",
6469
USERNAME: "Your BrowserStack username",
65-
ACCESS_KEY: "Your BrowserStack access key"
70+
ACCESS_KEY: "Your BrowserStack access key",
71+
NO_NPM_WARNING: "No NPM warning if npm_dependencies is empty"
6672
}
6773
}
6874

@@ -75,9 +81,14 @@ const messageTypes = {
7581
NULL: null
7682
}
7783

84+
const constants = {
85+
DEFAULT_PARALLEL_MESSAGE: "Here goes the number of parallels you want to run"
86+
}
87+
7888
module.exports = Object.freeze({
7989
userMessages,
8090
cliMessages,
8191
validationMessages,
8292
messageTypes,
93+
constants
8394
});

bin/helpers/utils.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exports.validateBstackJson = (bsConfigPath) => {
1414
resolve(bsConfig);
1515
}
1616
catch (e) {
17-
reject(e);
17+
reject("Couldn't find the browserstack.json file at \""+ bsConfigPath +"\". Please use --config-file <path to browserstack.json>.");
1818
}
1919
});
2020
}
@@ -34,9 +34,24 @@ exports.getErrorCodeFromMsg = (errMsg) => {
3434
case Constants.validationMessages.EMPTY_RUN_SETTINGS:
3535
errorCode = "bstack_json_invalid_no_run_settings";
3636
break;
37-
case Constants.validationMessages.EMPTY_SPEC_FILES:
38-
errorCode = "bstack_json_invalid_values";
37+
case Constants.validationMessages.EMPTY_CYPRESS_PROJ_DIR:
38+
errorCode = "bstack_json_invalid_no_cypress_proj_dir";
3939
break;
40+
case Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS:
41+
errorCode = "bstack_json_default_auth_keys";
42+
break;
43+
case Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION:
44+
errorCode = "invalid_parallels_specified";
45+
break;
46+
case Constants.validationMessages.LOCAL_NOT_SET:
47+
errorCode = "cypress_json_base_url_no_local";
48+
break;
49+
case Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE:
50+
errorCode = "invalid_directory_structure";
51+
break;
52+
}
53+
if(errMsg.includes("Please use --config-file <path to browserstack.json>.")){
54+
errorCode = "bstack_json_path_invalid";
4055
}
4156
return errorCode;
4257
}
@@ -130,7 +145,7 @@ exports.isUndefined = value => (value === undefined || value === null);
130145
exports.isFloat = value => (Number(value) && Number(value) % 1 !== 0);
131146

132147
exports.isParallelValid = (value) => {
133-
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1);
148+
return this.isUndefined(value) || !(isNaN(value) || this.isFloat(value) || parseInt(value, 10) === 0 || parseInt(value, 10) < -1 ) || value === Constants.constants.DEFAULT_PARALLEL_MESSAGE;
134149
}
135150

136151
exports.getUserAgent = () => {
@@ -150,3 +165,17 @@ exports.configCreated = (args) => {
150165
logger.info(message);
151166
this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
152167
}
168+
169+
exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
170+
// Getting absolute path
171+
cypressDir = path.resolve(cypressDir);
172+
cypressProjDir = path.resolve(cypressProjDir);
173+
if(cypressProjDir === cypressDir) return true;
174+
let parentTokens = cypressDir.split('/').filter(i => i.length);
175+
let childTokens = cypressProjDir.split('/').filter(i => i.length);
176+
return parentTokens.every((t, i) => childTokens[i] === t);
177+
}
178+
179+
exports.getLocalFlag = (connectionSettings) => {
180+
return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local
181+
}

bin/runner.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ var argv = yargs
175175
describe: Constants.cliMessages.RUN.ENV_DESCRIPTION,
176176
type: "string",
177177
default: undefined
178+
},
179+
'disable-npm-warning': {
180+
default: false,
181+
description: Constants.cliMessages.COMMON.NO_NPM_WARNING,
182+
type: "boolean"
178183
}
179184
})
180185
.help('help')

test/unit/bin/commands/runs.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ describe("runs", () => {
454454
zipUploadStub = sandbox.stub();
455455
createBuildStub = sandbox.stub();
456456
deleteZipStub = sandbox.stub();
457+
isUndefinedStub = sandbox.stub();
457458
});
458459

459460
afterEach(() => {
@@ -478,7 +479,8 @@ describe("runs", () => {
478479
setTestEnvs: setTestEnvsStub,
479480
setUsageReportingFlag: setUsageReportingFlagStub,
480481
setParallels: setParallelsStub,
481-
getConfigPath: getConfigPathStub
482+
getConfigPath: getConfigPathStub,
483+
isUndefined: isUndefinedStub
482484
},
483485
"../helpers/capabilityHelper": {
484486
validate: capabilityValidatorStub,

0 commit comments

Comments
 (0)