Skip to content

Commit abc4f2f

Browse files
committed
Merge branch 'master' into cypress_config_file_support
2 parents fa97586 + 9661f75 commit abc4f2f

File tree

4 files changed

+122
-10
lines changed

4 files changed

+122
-10
lines changed

bin/commands/init.js

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,47 @@ 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+
let filename = args._[1];
25+
if (filename !== path.basename(filename)) {
26+
// filename is an absolute path
27+
return filename;
28+
}
29+
return path.join(process.cwd(), args._[1]);
1430
}
1531

16-
var config = {
32+
return path.join(process.cwd(), "browserstack.json");
33+
}
34+
35+
36+
module.exports = function init(args) {
37+
38+
let path_to_json = get_path(args);
39+
if (path_to_json === undefined) return;
40+
41+
// append .json if filename passed is not of json type
42+
if (path.extname(path_to_json) !== '' && path.extname(path_to_json) !== ".json") path_to_json += ".json";
43+
44+
// append browserstack.json if filename is a path without filename
45+
if (path.extname(path_to_json) === '') path_to_json = path.join(path_to_json, "browserstack.json");
46+
47+
let config = {
1748
file: require('../templates/configTemplate')(),
18-
path: path_to_bsconf
49+
path: path_to_json
1950
};
2051

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

bin/helpers/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const userMessages = {
1313
API_DEPRECATED: "This version of API is deprecated, please use latest version of API.",
1414
FAILED_TO_ZIP: "Failed to zip files.",
1515
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
NO_PARALLELS: "Your tests will run sequentially. Read more about running your tests in parallel here: https://www.browserstack.com/docs/automate/cypress/run-tests-in-parallel",
1718
NO_NPM_DEPENDENCIES: "No npm dependencies specified. Read more here: https://www.browserstack.com/docs/automate/cypress/npm-packages. You can suppress this warning by using --disable-npm-warning flag."
1819
};

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: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
const chai = require("chai"),
2+
assert = chai.assert,
3+
expect = chai.expect,
24
sinon = require("sinon"),
35
chaiAsPromised = require("chai-as-promised"),
4-
util = require("util");
6+
rewire = require("rewire"),
7+
util = require("util"),
8+
path = require('path');
59

610
const Constants = require("../../../../bin/helpers/constants"),
711
logger = require("../../../../bin/helpers/logger").winstonLogger,
8-
testObjects = require("../../support/fixtures/testObjects");
12+
testObjects = require("../../support/fixtures/testObjects")
13+
utils = require("../../../../bin/helpers/utils");
914

1015
const proxyquire = require("proxyquire").noCallThru();
1116

17+
const get_path = rewire("../../../../bin/commands/init").__get__("get_path");;
18+
1219
chai.use(chaiAsPromised);
1320
logger.transports["console.info"].silent = true;
1421

@@ -29,6 +36,79 @@ describe("init", () => {
2936
sinon.restore();
3037
});
3138

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

0 commit comments

Comments
 (0)