Skip to content

Commit eb94741

Browse files
Local True Bug Fix and added Unit test Cases
1 parent 2a0451f commit eb94741

File tree

4 files changed

+124
-8
lines changed

4 files changed

+124
-8
lines changed

bin/helpers/capabilityHelper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const validate = (bsConfig, args) => {
112112
let cypressJson = fs.readFileSync(path.join(bsConfig.run_settings.cypress_proj_dir, 'cypress.json'));
113113
cypressJson = JSON.parse(cypressJson);
114114
// Cypress Json Base Url & Local true check
115-
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost")) reject(Constants.validationMessages.LOCAL_NOT_SET);
115+
if (!Utils.isUndefined(cypressJson.baseUrl) && cypressJson.baseUrl.includes("localhost") && !Utils.getLocalFlag(bsConfig.connection_settings)) reject(Constants.validationMessages.LOCAL_NOT_SET);
116116

117117
// Detect if the user is not using the right directory structure, and throw an error
118118
if (!Utils.isUndefined(cypressJson.integrationFolder) && !Utils.isCypressProjDirValid(bsConfig.run_settings.cypress_proj_dir,cypressJson.integrationFolder)) reject(Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE);

bin/helpers/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,7 @@ exports.isCypressProjDirValid = (cypressDir, cypressProjDir) => {
145145
let childTokens = cypressProjDir.split('/').filter(i => i.length);
146146
return parentTokens.every((t, i) => childTokens[i] === t);
147147
}
148+
149+
exports.getLocalFlag = (connectionSettings) => {
150+
return !this.isUndefined(connectionSettings) && !this.isUndefined(connectionSettings.local) && connectionSettings.local
151+
}

test/unit/bin/helpers/capabilityHelper.js

Lines changed: 101 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ describe("capabilityHelper.js", () => {
504504
it("validate bsConfig", () => {
505505
let bsConfig = undefined;
506506
return capabilityHelper
507-
.validate(bsConfig, {parallels: undefined})
507+
.validate(bsConfig, { parallels: undefined })
508508
.then(function (data) {
509509
chai.assert.fail("Promise error");
510510
})
@@ -516,7 +516,7 @@ describe("capabilityHelper.js", () => {
516516
it("validate bsConfig.auth", () => {
517517
bsConfig = {};
518518
return capabilityHelper
519-
.validate(bsConfig, {parallels: undefined})
519+
.validate(bsConfig, { parallels: undefined })
520520
.then(function (data) {
521521
chai.assert.fail("Promise error");
522522
})
@@ -536,7 +536,7 @@ describe("capabilityHelper.js", () => {
536536
}
537537
};
538538
return capabilityHelper
539-
.validate(bsConfig, {parallels: undefined})
539+
.validate(bsConfig, { parallels: undefined })
540540
.then(function (data) {
541541
chai.assert.fail("Promise error");
542542
})
@@ -554,7 +554,7 @@ describe("capabilityHelper.js", () => {
554554
browsers: [],
555555
};
556556
return capabilityHelper
557-
.validate(bsConfig, {parallels: undefined})
557+
.validate(bsConfig, { parallels: undefined })
558558
.then(function (data) {
559559
chai.assert.fail("Promise error");
560560
})
@@ -578,7 +578,7 @@ describe("capabilityHelper.js", () => {
578578
],
579579
};
580580
return capabilityHelper
581-
.validate(bsConfig, {parallels: undefined})
581+
.validate(bsConfig, { parallels: undefined })
582582
.then(function (data) {
583583
chai.assert.fail("Promise error");
584584
})
@@ -604,7 +604,7 @@ describe("capabilityHelper.js", () => {
604604
};
605605

606606
return capabilityHelper
607-
.validate(bsConfig, {parallels: undefined})
607+
.validate(bsConfig, { parallels: undefined })
608608
.then(function (data) {
609609
chai.assert.fail("Promise error");
610610
})
@@ -631,13 +631,107 @@ describe("capabilityHelper.js", () => {
631631
},
632632
};
633633
capabilityHelper
634-
.validate(bsConfig, {parallels: undefined})
634+
.validate(bsConfig, { parallels: undefined })
635635
.then(function (data) {
636636
chai.assert.equal(data, Constants.validationMessages.VALIDATED);
637637
})
638638
.catch((error) => {
639639
chai.assert.fail("Promise error");
640640
});
641641
});
642+
describe("validate cypress.json", () => {
643+
beforeEach(() => {
644+
bsConfig = {
645+
auth: {},
646+
browsers: [
647+
{
648+
browser: "chrome",
649+
os: "Windows 10",
650+
versions: ["78", "77"],
651+
},
652+
],
653+
run_settings: {
654+
cypress_proj_dir: "random path"
655+
},
656+
};
657+
});
658+
it("validate cypress json is present", () => {
659+
//Stub for cypress json validation
660+
sinon.stub(fs, 'existsSync').returns(false);
661+
662+
return capabilityHelper
663+
.validate(bsConfig, { parallels: undefined })
664+
.then(function (data) {
665+
chai.assert.fail("Promise error");
666+
})
667+
.catch((error) => {
668+
chai.assert.equal(
669+
error,
670+
Constants.validationMessages.CYPRESS_JSON_NOT_FOUND + "random path"
671+
);
672+
fs.existsSync.restore();
673+
});
674+
});
675+
676+
it("validate cypress json is valid", () => {
677+
//Stub for cypress json validation
678+
sinon.stub(fs, 'existsSync').returns(true);
679+
sinon.stub(fs, 'readFileSync').returns("{invalid}");
680+
681+
return capabilityHelper
682+
.validate(bsConfig, { parallels: undefined })
683+
.then(function (data) {
684+
chai.assert.fail("Promise error");
685+
})
686+
.catch((error) => {
687+
chai.assert.equal(
688+
error,
689+
Constants.validationMessages.INVALID_CYPRESS_JSON
690+
);
691+
fs.existsSync.restore();
692+
fs.readFileSync.restore();
693+
});
694+
});
695+
696+
it("validate baseUrl is set to localhost and local is not set to true", () => {
697+
//Stub for cypress json validation
698+
sinon.stub(fs, 'existsSync').returns(true);
699+
sinon.stub(fs, 'readFileSync').returns('{ "baseUrl": "http://localhost:3000"}');
700+
701+
return capabilityHelper
702+
.validate(bsConfig, { parallels: undefined })
703+
.then(function (data) {
704+
chai.assert.fail("Promise error");
705+
})
706+
.catch((error) => {
707+
chai.assert.equal(
708+
error,
709+
Constants.validationMessages.LOCAL_NOT_SET
710+
);
711+
fs.existsSync.restore();
712+
fs.readFileSync.restore();
713+
});
714+
});
715+
716+
it("validate integrationFolder is set and is accessible from cypress_proj_dir", () => {
717+
//Stub for cypress json validation
718+
sinon.stub(fs, 'existsSync').returns(true);
719+
sinon.stub(fs, 'readFileSync').returns('{ "integrationFolder": "/absolute/path"}');
720+
721+
return capabilityHelper
722+
.validate(bsConfig, { parallels: undefined })
723+
.then(function (data) {
724+
chai.assert.fail("Promise error");
725+
})
726+
.catch((error) => {
727+
chai.assert.equal(
728+
error,
729+
Constants.validationMessages.INCORRECT_DIRECTORY_STRUCTURE
730+
);
731+
fs.existsSync.restore();
732+
fs.readFileSync.restore();
733+
});
734+
});
735+
});
642736
});
643737
});

test/unit/bin/helpers/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,22 @@ describe("utils", () => {
255255
expect(utils.isCypressProjDirValid("/absolute/path","/absolute")).to.be.false;
256256
});
257257
});
258+
259+
describe("getLocalFlag", () => {
260+
it("should return false if connectionSettings is undefined", () => {
261+
expect(utils.getLocalFlag(undefined)).to.be.false;
262+
});
263+
264+
it("should return false if connectionSettings.local is undefined", () => {
265+
expect(utils.getLocalFlag({})).to.be.false;
266+
});
267+
268+
it("should return false if connectionSettings.local is false", () => {
269+
expect(utils.getLocalFlag({"local": false})).to.be.false;
270+
});
271+
272+
it("should return true if connectionSettings.local is true", () => {
273+
expect(utils.getLocalFlag({"local": true})).to.be.true;
274+
});
275+
});
258276
});

0 commit comments

Comments
 (0)