Skip to content

Commit f9cdba7

Browse files
authored
Merge pull request #39 from browserstack/CYP_9999_fix_logging_in_init
Handle absolute path and verify init path
2 parents f3118b9 + 35d0dea commit f9cdba7

File tree

15 files changed

+190
-39
lines changed

15 files changed

+190
-39
lines changed

bin/commands/info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const config = require("../helpers/config"),
77
utils = require("../helpers/utils");
88

99
module.exports = function info(args) {
10-
let bsConfigPath = process.cwd() + args.cf;
10+
let bsConfigPath = utils.getConfigPath(args.cf);
1111

1212
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1313
// accept the username from command line if provided

bin/commands/init.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
const fileHelpers = require("../helpers/fileHelpers"),
33
Constants = require("../helpers/constants"),
44
logger = require("../helpers/logger").winstonLogger,
5-
utils = require("../helpers/utils");
5+
utils = require("../helpers/utils"),
6+
util = require("util"),
7+
path = require('path');
68

79
module.exports = function init(args) {
810
if (args.p) {
9-
var path_to_bsconf = args.p + "/browserstack.json";
11+
var path_to_bsconf = path.join(args.p + "/browserstack.json");
1012
} else {
1113
var path_to_bsconf = "./browserstack.json";
1214
}
@@ -16,19 +18,21 @@ module.exports = function init(args) {
1618
path: path_to_bsconf
1719
};
1820

19-
function allDone() {
20-
let message = Constants.userMessages.CONFIG_FILE_CREATED
21-
logger.info(message);
22-
utils.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
23-
}
24-
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');
21+
return fileHelpers.dirExists(config.path, function(dirExists){
22+
if (dirExists) {
23+
fileHelpers.fileExists(config.path, function(exists){
24+
if (exists) {
25+
let message = Constants.userMessages.CONFIG_FILE_EXISTS;
26+
logger.error(message);
27+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
28+
} else {
29+
fileHelpers.write(config, null, args, utils.configCreated);
30+
}
31+
});
3032
} else {
31-
fileHelpers.write(config, null, allDone);
33+
let message = util.format(Constants.userMessages.DIR_NOT_FOUND, path.dirname(config.path));
34+
logger.error(message);
35+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'path_to_init_not_found');
3236
}
3337
});
3438
}

bin/commands/runs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const archiver = require("../helpers/archiver"),
1010
fileHelpers = require("../helpers/fileHelpers");
1111

1212
module.exports = function run(args) {
13-
let bsConfigPath = process.cwd() + args.cf;
13+
let bsConfigPath = utils.getConfigPath(args.cf);
1414

1515
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1616
utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

bin/commands/stop.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const config = require("../helpers/config"),
77
utils = require("../helpers/utils");
88

99
module.exports = function stop(args) {
10-
let bsConfigPath = process.cwd() + args.cf;
10+
let bsConfigPath = utils.getConfigPath(args.cf);
1111

1212
return utils.validateBstackJson(bsConfigPath).then(function (bsConfig) {
1313
// accept the username from command line if provided

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 in %s",
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: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const logger = require("./logger").winstonLogger,
66
Constants = require("../helpers/constants"),
77
config = require("../helpers/config");
88

9-
exports.write = function(f, message, cb) {
9+
exports.write = function(f, message, args, cb) {
1010
message = message || 'Creating';
1111
fs.writeFile(f.path, f.file, function() {
12-
logger.info(message + " file: ./" + path.relative(process.cwd(), f.path));
13-
cb && cb()
12+
logger.info(message + " file: " + f.path);
13+
cb && cb(args)
1414
});
1515
}
1616

@@ -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+
}

bin/helpers/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const os = require("os");
3+
const path = require("path");
34

45
const usageReporting = require('./usageReporting'),
56
logger = require('./logger').winstonLogger,
@@ -105,3 +106,17 @@ exports.isParallelValid = (value) => {
105106
exports.getUserAgent = () => {
106107
return `BStack-Cypress-CLI/1.3.0 (${os.arch()}/${os.platform()}/${os.release()})`;
107108
}
109+
110+
exports.isAbsolute = (configPath) => {
111+
return path.isAbsolute(configPath)
112+
}
113+
114+
exports.getConfigPath = (configPath) => {
115+
return this.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
116+
}
117+
118+
exports.configCreated = (args) => {
119+
let message = Constants.userMessages.CONFIG_FILE_CREATED
120+
logger.info(message);
121+
this.sendUsageReport(null, args, message, Constants.messageTypes.SUCCESS, null);
122+
}

bin/runner.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var argv = yargs
5151
'cf': {
5252
alias: 'config-file',
5353
describe: Constants.cliMessages.BUILD.DESC,
54-
default: '/browserstack.json',
54+
default: 'browserstack.json',
5555
type: 'string',
5656
nargs: 1,
5757
demand: true,
@@ -91,7 +91,7 @@ var argv = yargs
9191
'cf': {
9292
alias: 'config-file',
9393
describe: Constants.cliMessages.BUILD.DESC,
94-
default: '/browserstack.json',
94+
default: 'browserstack.json',
9595
type: 'string',
9696
nargs: 1,
9797
demand: true,
@@ -130,7 +130,7 @@ var argv = yargs
130130
'cf': {
131131
alias: 'config-file',
132132
describe: Constants.cliMessages.RUN.DESC,
133-
default: '/browserstack.json',
133+
default: 'browserstack.json',
134134
type: 'string',
135135
nargs: 1,
136136
demand: true,

test/unit/bin/commands/info.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ describe("buildInfo", () => {
2525
setUsernameStub = sandbox.stub();
2626
setAccessKeyStub = sandbox.stub();
2727
validateBstackJsonStub = sandbox.stub();
28+
getConfigPathStub = sandbox.stub();
2829
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
2930
getUserAgentStub = sandbox.stub().returns("random user-agent");
3031
sendUsageReportStub = sandbox.stub().callsFake(function () {
@@ -54,6 +55,7 @@ describe("buildInfo", () => {
5455
sendUsageReport: sendUsageReportStub,
5556
setUsageReportingFlag: setUsageReportingFlagStub,
5657
getUserAgent: getUserAgentStub,
58+
getConfigPath: getConfigPathStub
5759
},
5860
request: { get: requestStub },
5961
});
@@ -63,6 +65,7 @@ describe("buildInfo", () => {
6365
return info(args)
6466
.then(function (_bsConfig) {
6567
sinon.assert.calledOnce(requestStub);
68+
sinon.assert.calledOnce(getConfigPathStub);
6669
sinon.assert.calledOnce(getUserAgentStub);
6770
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
6871
}).catch((error) => {
@@ -88,6 +91,7 @@ describe("buildInfo", () => {
8891
sendUsageReport: sendUsageReportStub,
8992
setUsageReportingFlag: setUsageReportingFlagStub,
9093
getUserAgent: getUserAgentStub,
94+
getConfigPath: getConfigPathStub
9195
},
9296
request: { get: requestStub },
9397
});
@@ -98,6 +102,7 @@ describe("buildInfo", () => {
98102
.then(function (_bsConfig) {
99103
sinon.assert.calledOnce(requestStub);
100104
sinon.assert.calledOnce(getUserAgentStub);
105+
sinon.assert.calledOnce(getConfigPathStub);
101106
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
102107
})
103108
.catch((error) => {
@@ -112,6 +117,7 @@ describe("buildInfo", () => {
112117
setUsernameStub = sandbox.stub();
113118
setAccessKeyStub = sandbox.stub();
114119
validateBstackJsonStub = sandbox.stub();
120+
getConfigPathStub = sandbox.stub();
115121
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
116122
getUserAgentStub = sandbox.stub().returns("random user-agent");
117123
sendUsageReportStub = sandbox.stub().callsFake(function () {
@@ -143,6 +149,7 @@ describe("buildInfo", () => {
143149
sendUsageReport: sendUsageReportStub,
144150
setUsageReportingFlag: setUsageReportingFlagStub,
145151
getUserAgent: getUserAgentStub,
152+
getConfigPath: getConfigPathStub
146153
},
147154
request: { get: requestStub },
148155
});
@@ -153,6 +160,7 @@ describe("buildInfo", () => {
153160
.then(function (_bsConfig) {
154161
sinon.assert.calledOnce(requestStub);
155162
sinon.assert.calledOnce(getUserAgentStub);
163+
sinon.assert.calledOnce(getConfigPathStub);
156164
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
157165
})
158166
.catch((error) => {
@@ -183,6 +191,7 @@ describe("buildInfo", () => {
183191
sendUsageReport: sendUsageReportStub,
184192
setUsageReportingFlag: setUsageReportingFlagStub,
185193
getUserAgent: getUserAgentStub,
194+
getConfigPath: getConfigPathStub
186195
},
187196
request: { get: requestStub },
188197
});
@@ -193,6 +202,7 @@ describe("buildInfo", () => {
193202
.then(function (_bsConfig) {
194203
sinon.assert.calledOnce(requestStub);
195204
sinon.assert.calledOnce(getUserAgentStub);
205+
sinon.assert.calledOnce(getConfigPathStub);
196206
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
197207
})
198208
.catch((error) => {
@@ -218,6 +228,7 @@ describe("buildInfo", () => {
218228
sendUsageReport: sendUsageReportStub,
219229
setUsageReportingFlag: setUsageReportingFlagStub,
220230
getUserAgent: getUserAgentStub,
231+
getConfigPath: getConfigPathStub
221232
},
222233
request: { get: requestStub },
223234
});
@@ -228,6 +239,7 @@ describe("buildInfo", () => {
228239
.then(function (_bsConfig) {
229240
sinon.assert.calledOnce(requestStub);
230241
sinon.assert.calledOnce(getUserAgentStub);
242+
sinon.assert.calledOnce(getConfigPathStub);
231243
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
232244
})
233245
.catch((error) => {
@@ -244,6 +256,7 @@ describe("buildInfo", () => {
244256
setUsernameStub = sandbox.stub();
245257
setAccessKeyStub = sandbox.stub();
246258
validateBstackJsonStub = sandbox.stub();
259+
getConfigPathStub = sandbox.stub();
247260
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
248261
getUserAgentStub = sandbox.stub().returns("random user-agent");
249262
sendUsageReportStub = sandbox.stub().callsFake(function () {
@@ -273,6 +286,7 @@ describe("buildInfo", () => {
273286
sendUsageReport: sendUsageReportStub,
274287
setUsageReportingFlag: setUsageReportingFlagStub,
275288
getUserAgent: getUserAgentStub,
289+
getConfigPath: getConfigPathStub
276290
},
277291
request: { get: requestStub },
278292
});
@@ -283,6 +297,7 @@ describe("buildInfo", () => {
283297
.then(function (_bsConfig) {
284298
sinon.assert.calledOnce(requestStub);
285299
sinon.assert.calledOnce(getUserAgentStub);
300+
sinon.assert.calledOnce(getConfigPathStub);
286301
sinon.assert.calledOnceWithExactly(sendUsageReportStub, bsConfig, args, message, messageType, errorCode);
287302
}).catch((error) => {
288303
chai.assert.isNotOk(error,'Promise error');
@@ -297,6 +312,7 @@ describe("buildInfo", () => {
297312
sandbox = sinon.createSandbox();
298313
setUsernameStub = sandbox.stub();
299314
setAccessKeyStub = sandbox.stub();
315+
getConfigPathStub = sandbox.stub();
300316
validateBstackJsonStub = sandbox.stub();
301317
setUsageReportingFlagStub = sandbox.stub().returns(undefined);
302318
sendUsageReportStub = sandbox.stub().callsFake(function () {
@@ -319,6 +335,7 @@ describe("buildInfo", () => {
319335
getErrorCodeFromErr: getErrorCodeFromErrStub,
320336
sendUsageReport: sendUsageReportStub,
321337
setUsageReportingFlag: setUsageReportingFlagStub,
338+
getConfigPath: getConfigPathStub
322339
},
323340
});
324341

test/unit/bin/commands/init.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const chai = require("chai"),
22
sinon = require("sinon"),
3-
chaiAsPromised = require("chai-as-promised");
3+
chaiAsPromised = require("chai-as-promised"),
4+
util = require("util");
45

56
const Constants = require("../../../../bin/helpers/constants"),
67
logger = require("../../../../bin/helpers/logger").winstonLogger,
@@ -15,20 +16,48 @@ describe("init", () => {
1516
let args = testObjects.initSampleArgs;
1617
var sandbox;
1718

18-
before(() => {
19+
beforeEach(() => {
1920
sandbox = sinon.createSandbox();
21+
configCreatedStub = sandbox.stub()
2022
sendUsageReportStub = sandbox.stub().callsFake(function () {
2123
return "end";
2224
});
2325
});
2426

25-
after(() => {
27+
afterEach(() => {
2628
sandbox.restore();
2729
sinon.restore();
2830
});
2931

3032
describe("init", () => {
33+
it("fail if given path is not present", () => {
34+
dirExistsStub = sandbox.stub().yields(false);
35+
writeStub = sandbox.stub();
36+
formatStub = sandbox.stub().callsFake(function (args) {
37+
return args;
38+
});
39+
40+
const init = proxyquire("../../../../bin/commands/init", {
41+
"../helpers/utils": {
42+
sendUsageReport: sendUsageReportStub,
43+
},
44+
"../helpers/fileHelpers": {
45+
dirExists: dirExistsStub,
46+
write: writeStub,
47+
},
48+
"util": {
49+
format: formatStub
50+
}
51+
});
52+
53+
init(args);
54+
sinon.assert.calledOnce(dirExistsStub);
55+
sinon.assert.notCalled(writeStub);
56+
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.DIR_NOT_FOUND, Constants.messageTypes.ERROR, 'path_to_init_not_found');
57+
});
58+
3159
it("fail if browserstack.json is already present", () => {
60+
dirExistsStub = sandbox.stub().yields(true);
3261
fileExistsStub = sandbox.stub().yields(true);
3362
writeStub = sandbox.stub();
3463

@@ -37,32 +66,38 @@ describe("init", () => {
3766
sendUsageReport: sendUsageReportStub,
3867
},
3968
"../helpers/fileHelpers": {
69+
dirExists: dirExistsStub,
4070
fileExists: fileExistsStub,
4171
write: writeStub,
4272
},
4373
});
4474

4575
init(args);
76+
sinon.assert.calledOnce(dirExistsStub);
4677
sinon.assert.calledOnce(fileExistsStub);
4778
sinon.assert.notCalled(writeStub);
4879
sinon.assert.calledOnceWithExactly(sendUsageReportStub, null, args, Constants.userMessages.CONFIG_FILE_EXISTS, Constants.messageTypes.ERROR, 'bstack_json_already_exists');
4980
});
5081

5182
it("create browserstack.json if not already present", () => {
83+
dirExistsStub = sandbox.stub().yields(true);
5284
fileExistsStub = sandbox.stub().yields(false);
5385
writeStub = sandbox.stub();
5486

5587
const init = proxyquire("../../../../bin/commands/init", {
5688
"../helpers/utils": {
5789
sendUsageReport: sendUsageReportStub,
90+
configCreated: configCreatedStub
5891
},
5992
"../helpers/fileHelpers": {
93+
dirExists: dirExistsStub,
6094
fileExists: fileExistsStub,
6195
write: writeStub,
6296
},
6397
});
6498

6599
init(args);
100+
sinon.assert.calledOnce(dirExistsStub);
66101
sinon.assert.calledOnce(fileExistsStub);
67102
sinon.assert.calledOnce(writeStub);
68103
});

0 commit comments

Comments
 (0)