forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_plugin.js
154 lines (130 loc) · 4.07 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const fs = require('fs');
const path = require('path');
const assert = require('chai').assert;
const rewire = require('rewire');
const log = require('../lib/log');
const Plugin = rewire('../lib/plugin');
const h = rewire('../lib/helper');
describe('plugin', function() {
before(function() {
log.init();
});
describe('#init', function() {
const leetcode = new Plugin(0, 'Leetcode', '2.0', '');
const cache = new Plugin(1, 'Cache', '1.0', '');
const retry = new Plugin(2, 'Retry', '3.0', '');
const core = new Plugin(3, 'Core', '4.0', '');
before(function() {
const noop = () => {};
cache.init = noop;
leetcode.init = noop;
retry.init = noop;
core.init = noop;
h.getCodeDirData = function() {
return [
{name: 'cache', data: cache, file: 'cache.js'},
{name: 'leetcode', data: leetcode, file: '.leetcode.js'}, // disabled
{name: 'retry', data: retry, file: 'retry.js'},
{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);
const names = Plugin.plugins.map(p => 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() {
let expect;
before(function() {
const cp = {
exec: function(cmd, opts, cb) {
expect = cmd;
return cb();
}
};
Plugin.__set__('cp', cp);
});
it('should install no deps ok', function(done) {
expect = '';
const plugin = new Plugin(100, 'test', '2017.12.26', 'desc', []);
plugin.install(function() {
assert.equal(expect, '');
done();
});
});
it('should install deps ok', function(done) {
const deps = ['a', 'b:linux', 'b:darwin', 'b:win32', 'c:bad', 'd'];
const 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() {
const src = path.resolve('./tmp/copy.src.js');
const 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 = () => 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) {
const 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();
});
});
});
describe('#enable', function() {
const file = path.resolve('./tmp/leetcode.js');
function clean() {
if (fs.existsSync(file)) fs.unlinkSync(file);
h.getPluginFile = () => file;
}
beforeEach(clean);
after(clean);
it('should ok', function() {
const p = new Plugin(0, 'Leetcode', '2.0', '');
assert.equal(p.enabled, true);
p.setFile('.leetcode.js');
fs.writeFileSync(file, '');
assert.equal(p.enabled, false);
assert.equal(p.file, '.leetcode.js');
p.enable(false);
assert.equal(p.enabled, false);
assert.equal(p.file, '.leetcode.js');
p.enable(true);
assert.equal(p.enabled, true);
assert.equal(p.file, 'leetcode.js');
});
}); // #enable
});