forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_plugin.js
125 lines (107 loc) · 3.28 KB
/
test_plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
'use strict';
var fs = require('fs');
var path = require('path');
var assert = require('chai').assert;
var rewire = require('rewire');
var log = require('../lib/log');
var Plugin = rewire('../lib/plugin');
var h = rewire('../lib/helper');
describe('plugin', function() {
before(function() {
log.init();
});
describe('#init', function() {
var leetcode = new Plugin(0, 'Leetcode', '2.0', '');
var cache = new Plugin(1, 'Cache', '1.0', '');
var retry = new Plugin(2, 'Retry', '3.0', '');
var core = new Plugin(3, 'Core', '4.0', '');
before(function() {
var noop = function() {};
cache.init = noop;
leetcode.init = noop;
retry.init = noop;
core.init = noop;
h.getCodeDirData = function() {
return [
{name: 'cache', data: cache},
{name: '.leetcode', data: leetcode}, // disabled
{name: 'retry', data: retry},
{name: 'bad', data: null}
];
};
Plugin.__set__('h', h);
});
it('should init ok', function() {
assert.deepEqual(Plugin.plugins, []);
Plugin.init(core);
assert.deepEqual(Plugin.plugins.length, 3);
var names = Plugin.plugins.map(function(p) { return p.name; });
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
assert.equal(core.next, retry);
assert.equal(retry.next, cache);
assert.equal(cache.next, null);
assert.equal(leetcode.next, null);
});
}); // #init
describe('#install', function() {
var expect;
before(function() {
var cp = {
exec: function(cmd, opts, cb) {
expect = cmd;
return cb();
}
};
Plugin.__set__('cp', cp);
});
it('should install no deps ok', function(done) {
expect = '';
var plugin = new Plugin(100, 'test', '2017.12.26', 'desc', []);
plugin.install(function() {
assert.equal(expect, '');
done();
});
});
it('should install deps ok', function(done) {
var deps = ['a', 'b:linux', 'b:darwin', 'b:win32', 'c:bad', 'd'];
var plugin = new Plugin(100, 'test', '2017.12.26', 'desc', deps);
plugin.install(function() {
assert.equal(expect, 'npm install --save a b d');
done();
});
});
}); // #install
describe('#copy', function() {
var src = path.resolve('./tmp/copy.src.js');
var dst = path.resolve('./tmp/copy.test.js');
function clean() {
if (fs.existsSync(src)) fs.unlinkSync(src);
if (fs.existsSync(dst)) fs.unlinkSync(dst);
h.getPluginFile = function() { return dst; };
}
beforeEach(clean);
after(clean);
it('should copy from http error', function(done) {
Plugin.copy('non-exists', function(e, fullpath) {
assert.equal(e, 'HTTP Error: 404');
assert.equal(fs.existsSync(dst), false);
done();
});
}).timeout(5000);
it('should copy from local ok', function(done) {
var data = [
'module.exports = {',
' x: 123,',
' install: function(cb) { cb(); }',
'};'
];
fs.writeFileSync(src, data.join('\n'));
Plugin.install(src, function(e, plugin) {
assert.notExists(e);
assert.equal(plugin.x, 123);
assert.equal(fs.existsSync(dst), true);
done();
});
});
});
});