Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Error Texts #102

Merged
merged 3 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = function run(args) {

// display browserstack.json is not valid only if validation of browserstack.json field has failed, otherwise display just the error message
// If parallels specified in arguments are invalid do not display browserstack.json is invalid message
if (!(err === Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION && !utils.isUndefined(args.parallels))) {
if (utils.isJSONInvalid(err, args)) {
logger.error(Constants.validationMessages.NOT_VALID);
}

Expand Down
2 changes: 1 addition & 1 deletion bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const validate = (bsConfig, args) => {
cypressJson = JSON.parse(cypressJsonContent);

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

// Detect if the user is not using the right directory structure, and throw an error
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypressProjectDir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);
Expand Down
4 changes: 2 additions & 2 deletions bin/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const userMessages = {
BUILD_INFO_FAILED: "Failed to get build info.",
BUILD_STOP_FAILED: "Failed to stop build.",
BUILD_REPORT_MESSAGE: "See the entire build report here:",
ZIP_UPLOADER_NOT_REACHABLE: "Could not reach to zip uploader.",
ZIP_UPLOADER_NOT_REACHABLE: "Could not reach BrowserStack APIs. Please check your network or see if you need to whitelist *.browserstack.com",
ZIP_UPLOAD_FAILED: "Zip Upload failed.",
CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run",
CONFIG_FILE_EXISTS: "File already exists, delete the browserstack.json file manually. skipping...",
Expand Down Expand Up @@ -56,7 +56,7 @@ const validationMessages = {
CYPRESS_JSON_NOT_FOUND: "cypress.json file is not found at cypress_proj_dir path ",
INVALID_CYPRESS_JSON: "cypress.json is not a valid json",
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",
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",
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",
INCORRECT_DIRECTORY_STRUCTURE: "No tests to run. Note that your Cypress tests should be in the same directory where the cypress.json exists."
};

Expand Down
19 changes: 19 additions & 0 deletions bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,22 @@ exports.versionChangedMessage = (preferredVersion, actualVersion) => {
message = message.replace("<actualVersion>", actualVersion);
return message
}

exports.isJSONInvalid = (err, args) => {
let invalid = true

if (err === Constants.validationMessages.INVALID_PARALLELS_CONFIGURATION && !this.isUndefined(args.parallels)) {
return false
}

if (this.deleteBaseUrlFromError(err) === this.deleteBaseUrlFromError(Constants.validationMessages.LOCAL_NOT_SET)) {
return false
}

return invalid
}

exports.deleteBaseUrlFromError = (err) => {
return err.replace(/To test ([\s\S]*)on BrowserStack/g, 'To test on BrowserStack');
}

4 changes: 3 additions & 1 deletion test/unit/bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe("runs", () => {
beforeEach(() => {
sandbox = sinon.createSandbox();
validateBstackJsonStub = sandbox.stub();
isJSONInvalidStub = sandbox.stub();
setUsernameStub = sandbox.stub();
setAccessKeyStub = sandbox.stub();
setBuildNameStub = sandbox.stub();
Expand Down Expand Up @@ -127,7 +128,8 @@ describe("runs", () => {
setLocal: setLocalStub,
setLocalIdentifier: setLocalIdentifierStub,
deleteResults: deleteResultsStub,
setDefaults: setDefaultsStub
setDefaults: setDefaultsStub,
isJSONInvalid: isJSONInvalidStub
},
'../helpers/capabilityHelper': {
validate: capabilityValidatorStub,
Expand Down
2 changes: 1 addition & 1 deletion test/unit/bin/helpers/capabilityHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ describe("capabilityHelper.js", () => {
.catch((error) => {
chai.assert.equal(
error,
Constants.validationMessages.LOCAL_NOT_SET
Constants.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", "http://localhost:3000")
);
fs.existsSync.restore();
fs.readFileSync.restore();
Expand Down
32 changes: 31 additions & 1 deletion test/unit/bin/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('utils', () => {
sandbox = sinon.createSandbox();
sandbox.stub(utils,'getBrowserCombinations').returns(['a','b']);
});

afterEach(() => {
sandbox.restore();
sinon.restore();
Expand Down Expand Up @@ -1146,4 +1146,34 @@ describe('utils', () => {
expect(utils.versionChangedMessage(preferredVersion, actualVersion)).to.eq(message)
});
})

describe('#isJSONInvalid', () => {
it('JSON is valid when error is parallel misconfiguration', () => {
let error = constant.validationMessages.INVALID_PARALLELS_CONFIGURATION;
let args = {"parallels": 4}
expect(utils.isJSONInvalid(error, args)).to.eq(false)
});

it('JSON is valid when local is not set for localhost url', () => {
let error = constant.validationMessages.LOCAL_NOT_SET.replace("<baseUrlValue>", "localhost:4000");
expect(utils.isJSONInvalid(error, {})).to.eq(false)
});

it('JSON is invalid for errors apart from Local or Prallell misconfiguration', () => {
let error = constant.validationMessages.INCORRECT_AUTH_PARAMS;
expect(utils.isJSONInvalid(error, {})).to.eq(true)
});
})

describe('#deleteBaseUrlFromError', () => {
it('Replace baseUrl in Local error string', () => {
let error = constant.validationMessages.LOCAL_NOT_SET;
expect(utils.deleteBaseUrlFromError(error)).to.match(/To test on BrowserStack/)
});

it('should not replace baseUrl in other error string', () => {
let error = constant.validationMessages.NOT_VALID_JSON;
expect(utils.deleteBaseUrlFromError(error)).not.to.match(/To test on BrowserStack/)
});
});
});