Skip to content

Commit 1ab53ed

Browse files
committed
tests: add tests for recipe command
1 parent 900bbe6 commit 1ab53ed

3 files changed

Lines changed: 139 additions & 18 deletions

File tree

lib/cli/command.recipe.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
var logger = require('../logger').logger;
2+
var logger = require("../logger").logger;
33

44
/**
55
* $ browser-sync recipe <name> <options>
@@ -12,32 +12,31 @@ var logger = require('../logger').logger;
1212
*/
1313
module.exports = function (opts) {
1414

15-
var path = require('path');
16-
var fs = require('fs-extra');
15+
var path = require("path");
16+
var fs = require("fs-extra");
1717
var input = opts.cli.input.slice(1);
18-
var resolved = require.resolve('bs-recipes');
18+
var resolved = require.resolve("bs-recipes");
1919
var dir = path.dirname(resolved);
2020

21-
var logRecipes = function (cb) {
22-
fs.readdir(path.join(dir, 'recipes'), function (err, output) {
23-
logger.info('Install one of the following with {cyan:browser-sync recipe <name>\n');
24-
output.forEach(function (name) {
25-
console.log(' ', name);
26-
});
27-
cb();
21+
var logRecipes = function () {
22+
var dirs = fs.readdirSync(path.join(dir, "recipes"), function (err, output) {
23+
});
24+
logger.info("Install one of the following with {cyan:browser-sync recipe <name>\n");
25+
dirs.forEach(function (name) {
26+
console.log(" " + name);
2827
});
2928
}
3029

3130
if (!input.length) {
32-
logger.info('No recipe name provided!');
33-
return logRecipes(opts.cb);
31+
logger.info("No recipe name provided!");
32+
logRecipes();
33+
return opts.cb();
3434
}
3535

3636
input = input[0];
3737
var flags = opts.cli.flags;
3838
var output = flags.output ? path.resolve(flags.output) : path.join(process.cwd(), input);
39-
var targetDir = path.join(dir, 'recipes', input);
40-
39+
var targetDir = path.join(dir, "recipes", input);
4140

4241
if (fs.existsSync(output)) {
4342
return opts.cb(new Error("Target folder exists remove it first and then try again"));
@@ -48,13 +47,14 @@ module.exports = function (opts) {
4847
if (err) {
4948
opts.cb(err);
5049
} else {
51-
logger.info('Recipe copied into {cyan:%s}', output);
52-
logger.info('Next, inside that folder, run {cyan:npm i && npm start}');
50+
logger.info("Recipe copied into {cyan:%s}", output);
51+
logger.info("Next, inside that folder, run {cyan:npm i && npm start}");
5352
opts.cb(null);
5453
}
5554
});
5655
} else {
57-
logger.info('Recipe {cyan:%s} not found. The following are available though', input);
56+
logger.info("Recipe {cyan:%s} not found. The following are available though", input);
5857
logRecipes();
58+
opts.cb();
5959
}
6060
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"mocha": "2.3.0",
7575
"q": "1.4.1",
7676
"request": "2.61.0",
77+
"rimraf": "^2.4.3",
7778
"sinon": "1.15.4",
7879
"slugify": "0.1.1",
7980
"socket.io-client": "1.3.7",

test/specs/commands/recipes.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"use strict";
2+
3+
var path = require("path");
4+
var browserSync = require(path.resolve("./"));
5+
6+
var pkg = require(path.resolve("package.json"));
7+
var sinon = require("sinon");
8+
var logger = require("../../../lib/logger").logger;
9+
var assert = require("chai").assert;
10+
var cli = require(path.resolve(pkg.bin));
11+
var fs = require("fs");
12+
var rim = require("rimraf").sync;
13+
14+
describe("E2E CLI `recipes` command", function () {
15+
it("works with no output flag", function (done) {
16+
rim("./server");
17+
cli({
18+
cli: {
19+
input: ["recipe", "server"],
20+
flags: {}
21+
},
22+
cb: function () {
23+
assert.isTrue(fs.existsSync("./server"));
24+
rim("./server");
25+
done();
26+
}
27+
});
28+
});
29+
it("lists all available when no second arg given", function (done) {
30+
var stub1 = sinon.stub(logger, "info");
31+
var stub2 = sinon.stub(console, "log");
32+
33+
cli({
34+
cli: {
35+
input: ["recipe"],
36+
flags: {}
37+
},
38+
cb: function () {
39+
sinon.assert.calledWith(stub1, "No recipe name provided!");
40+
sinon.assert.calledWith(stub1, "Install one of the following with {cyan:browser-sync recipe <name>\n");
41+
42+
logger.info.restore();
43+
console.log.restore();
44+
45+
var calls = stub2.getCalls().map(function (call) {
46+
return call.args[0].trim();
47+
});
48+
49+
assert.include(calls, "gulp.jade");
50+
assert.include(calls, "server");
51+
assert.include(calls, "html.injection");
52+
done();
53+
}
54+
});
55+
});
56+
it("Does not overwrite existing dir", function (done) {
57+
cli({
58+
cli: {
59+
input: ["recipe", "server"],
60+
flags: {
61+
output: "test/fixtures"
62+
}
63+
},
64+
cb: function (err) {
65+
assert.equal(err.message, "Target folder exists remove it first and then try again");
66+
done();
67+
}
68+
});
69+
});
70+
it("accepts --output flag", function (done) {
71+
var stub1 = sinon.stub(logger, "info");
72+
cli({
73+
cli: {
74+
input: ["recipe", "server"],
75+
flags: {
76+
output: "test/recipes/server1"
77+
}
78+
},
79+
cb: function () {
80+
var dir = path.resolve("./test/recipes/server1");
81+
assert.isTrue(fs.existsSync("test/recipes/server1"));
82+
rim("test/recipes/server1");
83+
var call1 = stub1.getCall(0).args;
84+
assert.equal(call1[0], "Recipe copied into {cyan:%s}");
85+
assert.equal(call1[1], dir);
86+
sinon.assert.calledWith(stub1, "Next, inside that folder, run {cyan:npm i && npm start}");
87+
logger.info.restore();
88+
done();
89+
}
90+
});
91+
});
92+
it("Loges recipes when not found", function (done) {
93+
var stub1 = sinon.stub(logger, "info");
94+
var stub2 = sinon.stub(console, "log");
95+
96+
cli({
97+
cli: {
98+
input: ["recipe", "beepboop"],
99+
flags: {}
100+
},
101+
cb: function (err) {
102+
var call1 = stub1.getCall(0).args;
103+
assert.equal(call1[0], "Recipe {cyan:%s} not found. The following are available though");
104+
105+
var calls = stub2.getCalls().map(function (call) {
106+
return call.args[0].trim();
107+
});
108+
109+
assert.include(calls, "gulp.jade");
110+
assert.include(calls, "server");
111+
assert.include(calls, "html.injection");
112+
113+
logger.info.restore();
114+
console.log.restore();
115+
116+
done();
117+
}
118+
});
119+
});
120+
});

0 commit comments

Comments
 (0)