Skip to content

Commit 3336663

Browse files
committed
Add plugin UT.
Signed-off-by: Eric Wang <[email protected]>
1 parent 3414013 commit 3336663

File tree

3 files changed

+117
-33
lines changed

3 files changed

+117
-33
lines changed

lib/plugin.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var cp = require('child_process');
12
var fs = require('fs');
23
var path = require('path');
34

@@ -38,7 +39,7 @@ Plugin.prototype.install = function(cb) {
3839
var cmd = 'npm install --save ' + this.deps.join(' ');
3940
log.debug(cmd);
4041
var spin = h.spin(cmd);
41-
require('child_process').exec(cmd, {cwd: h.getCodeDir()}, function() {
42+
cp.exec(cmd, {cwd: h.getCodeDir()}, function() {
4243
spin.stop();
4344
return cb();
4445
});

test/test_log.js

+7
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,11 @@ describe('log', function() {
9797
assert.equal(result, chalk.red('[ERROR] some error [0]'));
9898
});
9999
});
100+
101+
describe('#printf', function() {
102+
it('should ok', function() {
103+
log.printf('%s and %d and %%', 'string', 100);
104+
assert.equal(result, 'string and 100 and %');
105+
});
106+
});
100107
});

test/test_plugin.js

+108-32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
14
var assert = require('chai').assert;
25
var rewire = require('rewire');
36

@@ -6,44 +9,117 @@ var Plugin = rewire('../lib/plugin');
69
var h = rewire('../lib/helper');
710

811
describe('plugin', function() {
9-
var leetcode = new Plugin(0, 'Leetcode', '2.0', '');
10-
var cache = new Plugin(1, 'Cache', '1.0', '');
11-
var retry = new Plugin(2, 'Retry', '3.0', '');
12-
var core = new Plugin(3, 'Core', '4.0', '');
13-
1412
before(function() {
1513
log.init();
16-
17-
var noop = function() {};
18-
cache.init = noop;
19-
leetcode.init = noop;
20-
retry.init = noop;
21-
core.init = noop;
22-
23-
h.getCodeDirData = function() {
24-
return [
25-
{name: 'cache', data: cache},
26-
{name: 'leetcode', data: leetcode},
27-
{name: 'retry', data: retry},
28-
{name: 'bad', data: null}
29-
];
30-
};
31-
Plugin.__set__('h', h);
3214
});
3315

34-
it('should init ok', function() {
35-
assert.deepEqual(Plugin.plugins, []);
36-
Plugin.init(core);
37-
assert.deepEqual(Plugin.plugins.length, 3);
16+
describe('#init', function() {
17+
var leetcode = new Plugin(0, 'Leetcode', '2.0', '');
18+
var cache = new Plugin(1, 'Cache', '1.0', '');
19+
var retry = new Plugin(2, 'Retry', '3.0', '');
20+
var core = new Plugin(3, 'Core', '4.0', '');
21+
22+
before(function() {
23+
var noop = function() {};
24+
cache.init = noop;
25+
leetcode.init = noop;
26+
retry.init = noop;
27+
core.init = noop;
28+
29+
h.getCodeDirData = function() {
30+
return [
31+
{name: 'cache', data: cache},
32+
{name: '.leetcode', data: leetcode}, // disabled
33+
{name: 'retry', data: retry},
34+
{name: 'bad', data: null}
35+
];
36+
};
37+
Plugin.__set__('h', h);
38+
});
39+
40+
it('should init ok', function() {
41+
assert.deepEqual(Plugin.plugins, []);
42+
Plugin.init(core);
43+
assert.deepEqual(Plugin.plugins.length, 3);
44+
45+
var names = Plugin.plugins.map(function(p) { return p.name; });
46+
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
3847

39-
var names = Plugin.plugins.map(function(p) {
40-
return p.name;
48+
assert.equal(core.next, retry);
49+
assert.equal(retry.next, cache);
50+
assert.equal(cache.next, null);
51+
assert.equal(leetcode.next, null);
4152
});
42-
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
53+
}); // #init
4354

44-
assert.equal(core.next, retry);
45-
assert.equal(retry.next, cache);
46-
assert.equal(cache.next, leetcode);
47-
assert.equal(leetcode.next, null);
55+
describe('#install', function() {
56+
var expect;
57+
before(function() {
58+
var cp = {
59+
exec: function(cmd, opts, cb) {
60+
expect = cmd;
61+
return cb();
62+
}
63+
};
64+
Plugin.__set__('cp', cp);
65+
});
66+
67+
it('should install no deps ok', function(done) {
68+
expect = '';
69+
var plugin = new Plugin(100, 'test', '2017.12.26', 'desc', []);
70+
plugin.install(function() {
71+
assert.equal(expect, '');
72+
done();
73+
});
74+
});
75+
76+
it('should install deps ok', function(done) {
77+
var deps = ['a', 'b:linux', 'b:darwin', 'b:win32', 'c:bad', 'd'];
78+
var plugin = new Plugin(100, 'test', '2017.12.26', 'desc', deps);
79+
plugin.install(function() {
80+
assert.equal(expect, 'npm install --save a b d');
81+
done();
82+
});
83+
});
84+
}); // #install
85+
86+
describe('#copy', function() {
87+
var src = path.resolve('./tmp/copy.src.js');
88+
var dst = path.resolve('./tmp/copy.test.js');
89+
90+
function clean() {
91+
if (fs.existsSync(src)) fs.unlinkSync(src);
92+
if (fs.existsSync(dst)) fs.unlinkSync(dst);
93+
h.getPluginFile = function() { return dst; };
94+
}
95+
96+
beforeEach(clean);
97+
after(clean);
98+
99+
it('should copy from http error', function(done) {
100+
this.timeout(5000);
101+
Plugin.copy('non-exists', function(e, fullpath) {
102+
assert.equal(e, 'HTTP Error: 404');
103+
assert.equal(fs.existsSync(dst), false);
104+
done();
105+
});
106+
});
107+
108+
it('should copy from local ok', function(done) {
109+
var data = [
110+
'module.exports = {',
111+
' x: 123,',
112+
' install: function(cb) { cb(); }',
113+
'};'
114+
];
115+
fs.writeFileSync(src, data.join('\n'));
116+
117+
Plugin.install(src, function(e, plugin) {
118+
assert.notExists(e);
119+
assert.equal(plugin.x, 123);
120+
assert.equal(fs.existsSync(dst), true);
121+
done();
122+
});
123+
});
48124
});
49125
});

0 commit comments

Comments
 (0)