Skip to content

Commit 8f0215c

Browse files
committed
Handle path not found while doing init
1 parent 3131e98 commit 8f0215c

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

bin/commands/init.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ module.exports = function init(args) {
2222
utils.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
2323
}
2424

25-
return fileHelpers.fileExists(config.path, function(exists){
26-
if (exists) {
27-
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
28-
logger.error(message);
29-
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
25+
return fileHelpers.dirExists(config.path, function(dirExists){
26+
if (dirExists) {
27+
fileHelpers.fileExists(config.path, function(exists){
28+
if (exists) {
29+
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
30+
logger.error(message);
31+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
32+
} else {
33+
fileHelpers.write(config, null, allDone);
34+
}
35+
});
3036
} else {
31-
fileHelpers.write(config, null, allDone);
37+
let message = Constants.userMessages.DIR_NOT_FOUND;
38+
logger.error(message);
39+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'path_to_init_not_found');
3240
}
3341
});
3442
}

bin/helpers/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const userMessages = {
77
ZIP_UPLOAD_FAILED: "Zip Upload failed.",
88
CONFIG_FILE_CREATED: "BrowserStack Config File created, you can now run browserstack-cypress --config-file run",
99
CONFIG_FILE_EXISTS: "File already exists, delete the browserstack.json file manually. skipping...",
10+
DIR_NOT_FOUND: "Given path does not exist. Failed to create browserstack.json.",
1011
ZIP_DELETE_FAILED: "Could not delete local file.",
1112
ZIP_DELETED: "Zip file deleted successfully.",
1213
API_DEPRECATED: "This version of API is deprecated, please use latest version of API.",

bin/helpers/fileHelpers.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ exports.deleteZip = () => {
3535
}
3636
});
3737
}
38+
39+
exports.dirExists = function (filePath, cb) {
40+
let exists = false;
41+
if (fs.existsSync(path.dirname(filePath), cb)) {
42+
exists = true;
43+
}
44+
cb && cb(exists);
45+
}

test/unit/bin/commands/init.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,41 @@ describe("init", () => {
1515
let args = testObjects.initSampleArgs;
1616
var sandbox;
1717

18-
before(() => {
18+
beforeEach(() => {
1919
sandbox = sinon.createSandbox();
2020
sendUsageReportStub = sandbox.stub().callsFake(function () {
2121
return "end";
2222
});
2323
});
2424

25-
after(() => {
25+
afterEach(() => {
2626
sandbox.restore();
2727
sinon.restore();
2828
});
2929

3030
describe("init", () => {
31+
it("fail if given path is not present", () => {
32+
dirExistsStub = sandbox.stub().yields(false);
33+
writeStub = sandbox.stub();
34+
35+
const init = proxyquire("../../../../bin/commands/init", {
36+
"../helpers/utils": {
37+
sendUsageReport: sendUsageReportStub,
38+
},
39+
"../helpers/fileHelpers": {
40+
dirExists: dirExistsStub,
41+
write: writeStub,
42+
},
43+
});
44+
45+
init(args);
46+
sinon.assert.calledOnce(dirExistsStub);
47+
sinon.assert.notCalled(writeStub);
48+
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.DIR_NOT_FOUND, Constants.messageTypes.ERROR, 'path_to_init_not_found');
49+
});
50+
3151
it("fail if browserstack.json is already present", () => {
52+
dirExistsStub = sandbox.stub().yields(true);
3253
fileExistsStub = sandbox.stub().yields(true);
3354
writeStub = sandbox.stub();
3455

@@ -37,18 +58,21 @@ describe("init", () => {
3758
sendUsageReport: sendUsageReportStub,
3859
},
3960
"../helpers/fileHelpers": {
61+
dirExists: dirExistsStub,
4062
fileExists: fileExistsStub,
4163
write: writeStub,
4264
},
4365
});
4466

4567
init(args);
68+
sinon.assert.calledOnce(dirExistsStub);
4669
sinon.assert.calledOnce(fileExistsStub);
4770
sinon.assert.notCalled(writeStub);
4871
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.CONFIG_FILE_EXISTS, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
4972
});
5073

5174
it("create browserstack.json if not already present", () => {
75+
dirExistsStub = sandbox.stub().yields(true);
5276
fileExistsStub = sandbox.stub().yields(false);
5377
writeStub = sandbox.stub();
5478

@@ -57,12 +81,14 @@ describe("init", () => {
5781
sendUsageReport: sendUsageReportStub,
5882
},
5983
"../helpers/fileHelpers": {
84+
dirExists: dirExistsStub,
6085
fileExists: fileExistsStub,
6186
write: writeStub,
6287
},
6388
});
6489

6590
init(args);
91+
sinon.assert.calledOnce(dirExistsStub);
6692
sinon.assert.calledOnce(fileExistsStub);
6793
sinon.assert.calledOnce(writeStub);
6894
});

0 commit comments

Comments
 (0)