Skip to content

Commit 4ce46e5

Browse files
committed
Support non-default BrowserStack config generation
1 parent 1487a4e commit 4ce46e5

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

bin/commands/init.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,36 @@ const fileHelpers = require("../helpers/fileHelpers"),
66
util = require("util"),
77
path = require('path');
88

9-
module.exports = function init(args) {
10-
if (args.p) {
11-
var path_to_bsconf = path.join(args.p + "/browserstack.json");
12-
} else {
13-
var path_to_bsconf = "./browserstack.json";
9+
10+
function get_path(args) {
11+
if (args._.length > 1 && args.p) {
12+
let filename = args._[1];
13+
if (filename !== path.basename(filename)) {
14+
let message = Constants.userMessages.CONFLICTING_INIT_ARGUMENTS;
15+
logger.error(message);
16+
utils.sendUsageReport(null, args, message, Constants.messageTypes.ERROR, 'conflicting_path_json_init');
17+
return;
18+
}
19+
20+
return path.join(args.p, filename);
21+
} else if (args.p) {
22+
return path.join(args.p, "browserstack.json");
23+
} else if (args._.length > 1) {
24+
return path.join(process.cwd(), args._[1]);
1425
}
1526

16-
var config = {
27+
return path.join(process.cwd(), "browserstack.json");
28+
}
29+
30+
31+
module.exports = function init(args) {
32+
33+
let path_to_json = get_path(args);
34+
if (path_to_json === undefined) return;
35+
36+
let config = {
1737
file: require('../templates/configTemplate')(),
18-
path: path_to_bsconf
38+
path: path_to_json
1939
};
2040

2141
return fileHelpers.dirExists(config.path, function(dirExists){

bin/helpers/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ 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+
CONFLICTING_INIT_ARGUMENTS: "Conflicting arguments given. You can use --path only with a file name, and not with a file path."
1617
};
1718

1819
const validationMessages = {

bin/runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var argv = yargs
2222
.demand(1, Constants.cliMessages.VERSION.DEMAND)
2323
.command('init', Constants.cliMessages.INIT.INFO, function(yargs) {
2424
argv = yargs
25-
.usage("usage: $0 init [options]")
25+
.usage("usage: $0 init [filename] [options]")
2626
.options({
2727
'p': {
2828
alias: "path",

test/unit/bin/commands/init.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const chai = require("chai"),
2+
assert = chai.assert,
3+
expect = chai.expect,
24
sinon = require("sinon"),
35
chaiAsPromised = require("chai-as-promised"),
6+
rewire = require("rewire"),
47
util = require("util");
58

69
const Constants = require("../../../../bin/helpers/constants"),
710
logger = require("../../../../bin/helpers/logger").winstonLogger,
8-
testObjects = require("../../support/fixtures/testObjects");
11+
testObjects = require("../../support/fixtures/testObjects")
12+
utils = require("../../../../bin/helpers/utils");
913

1014
const proxyquire = require("proxyquire").noCallThru();
1115

16+
const get_path = rewire("../../../../bin/commands/init").__get__("get_path");;
17+
1218
chai.use(chaiAsPromised);
1319
logger.transports["console.info"].silent = true;
1420

@@ -29,6 +35,71 @@ describe("init", () => {
2935
sinon.restore();
3036
});
3137

38+
describe("get_path", () => {
39+
it("filename passed, -path passed", () => {
40+
let args = {
41+
_: ["init", "filename.json"],
42+
p: '/sample-path',
43+
path: '/sample-path',
44+
$0: "browserstack-cypress",
45+
};
46+
47+
assert(get_path(args), '/sample-path/filename.json');
48+
});
49+
50+
it("filename passed, -path not passed", () => {
51+
let args = {
52+
_: ["init", "filename.json"],
53+
p: false,
54+
path: false,
55+
$0: "browserstack-cypress",
56+
};
57+
58+
assert(get_path(args), 'filename.json');
59+
});
60+
61+
it("filepath passed, -path passed", () => {
62+
let args = {
63+
_: ["init", "/sample-path/filename.json"],
64+
p: '/sample-path2',
65+
path: '/sample-path2',
66+
"disable-usage-reporting": undefined,
67+
disableUsageReporting: undefined,
68+
$0: "browserstack-cypress",
69+
};
70+
71+
loggerStub = sandbox.stub(logger, 'error');
72+
usageStub = sandbox.stub(utils, 'sendUsageReport');
73+
74+
expect(get_path(args)).to.be.undefined;
75+
sinon.assert.calledOnce(loggerStub);
76+
sinon.assert.calledOnce(usageStub);
77+
});
78+
79+
it("filename not passed, -path passed", () => {
80+
let args = {
81+
_: ["init"],
82+
p: '/sample-path',
83+
path: '/sample-path',
84+
$0: "browserstack-cypress",
85+
};
86+
87+
assert(get_path(args), '/sample-path/browserstack.json');
88+
});
89+
90+
it("filename not passed, -path not passed", () => {
91+
let args = {
92+
_: ["init"],
93+
p: false,
94+
path: false,
95+
$0: "browserstack-cypress",
96+
};
97+
98+
assert(get_path(args), 'browserstack.json');
99+
});
100+
});
101+
102+
32103
describe("init", () => {
33104
it("fail if given path is not present", () => {
34105
dirExistsStub = sandbox.stub().yields(false);

0 commit comments

Comments
 (0)