-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
plugins.js
244 lines (198 loc) · 8.81 KB
/
plugins.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @fileoverview Tests for Plugins
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("chai").assert,
Plugins = require("../../../lib/config/plugins"),
Environments = require("../../../lib/config/environments");
const proxyquire = require("proxyquire").noCallThru().noPreserveCache();
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("Plugins", () => {
describe("get", () => {
it("should return null when plugin doesn't exist", () => {
assert.isNull((new Plugins(new Environments(), () => {})).get("foo"));
});
});
describe("load()", () => {
let StubbedPlugins,
rules,
environments,
plugin,
scopedPlugin;
beforeEach(() => {
plugin = {};
scopedPlugin = {};
environments = new Environments();
rules = new Map();
StubbedPlugins = new (proxyquire("../../../lib/config/plugins", {
"eslint-plugin-example": plugin,
"@scope/eslint-plugin-example": scopedPlugin,
"eslint-plugin-throws-on-load": {
get rules() {
throw new Error("error thrown while loading this module");
}
}
}))(environments, rules.set.bind(rules));
});
it("should load a plugin when referenced by short name", () => {
StubbedPlugins.load("example");
assert.strictEqual(StubbedPlugins.get("example"), plugin);
});
it("should load a plugin when referenced by long name", () => {
StubbedPlugins.load("eslint-plugin-example");
assert.strictEqual(StubbedPlugins.get("example"), plugin);
});
it("should register environments when plugin has environments", () => {
plugin.environments = {
foo: {
globals: { foo: true }
},
bar: {
globals: { bar: false }
}
};
StubbedPlugins.load("eslint-plugin-example");
assert.deepStrictEqual(environments.get("example/foo"), plugin.environments.foo);
assert.deepStrictEqual(environments.get("example/bar"), plugin.environments.bar);
});
it("should register rules when plugin has rules", () => {
plugin.rules = {
baz: {},
qux: {}
};
StubbedPlugins.load("eslint-plugin-example");
assert.deepStrictEqual(rules.get("example/baz"), plugin.rules.baz);
assert.deepStrictEqual(rules.get("example/qux"), plugin.rules.qux);
});
it("should throw an error when a plugin has whitespace", () => {
assert.throws(() => {
StubbedPlugins.load("whitespace ");
}, /Whitespace found in plugin name 'whitespace '/);
assert.throws(() => {
StubbedPlugins.load("whitespace\t");
}, /Whitespace found in plugin name/);
assert.throws(() => {
StubbedPlugins.load("whitespace\n");
}, /Whitespace found in plugin name/);
assert.throws(() => {
StubbedPlugins.load("whitespace\r");
}, /Whitespace found in plugin name/);
});
it("should throw an error when a plugin doesn't exist", () => {
assert.throws(() => {
StubbedPlugins.load("nonexistentplugin");
}, /Failed to load plugin/);
});
it("should rethrow an error that a plugin throws on load", () => {
try {
StubbedPlugins.load("throws-on-load");
} catch (err) {
assert.strictEqual(
err.message,
"error thrown while loading this module",
"should rethrow the same error that was thrown on plugin load"
);
return;
}
assert.fail(null, null, "should throw an error if a plugin fails to load");
});
it("should load a scoped plugin when referenced by short name", () => {
StubbedPlugins.load("@scope/example");
assert.strictEqual(StubbedPlugins.get("@scope/example"), scopedPlugin);
});
it("should load a scoped plugin when referenced by long name", () => {
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.strictEqual(StubbedPlugins.get("@scope/example"), scopedPlugin);
});
it("should register environments when scoped plugin has environments", () => {
scopedPlugin.environments = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.strictEqual(environments.get("@scope/example/foo"), scopedPlugin.environments.foo);
});
it("should register rules when scoped plugin has rules", () => {
scopedPlugin.rules = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.strictEqual(rules.get("@scope/example/foo"), scopedPlugin.rules.foo);
});
describe("when referencing a scope plugin and omitting @scope/", () => {
it("should load a scoped plugin when referenced by short name, but should not get the plugin if '@scope/' is omitted", () => {
StubbedPlugins.load("@scope/example");
assert.strictEqual(StubbedPlugins.get("example"), null);
});
it("should load a scoped plugin when referenced by long name, but should not get the plugin if '@scope/' is omitted", () => {
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.strictEqual(StubbedPlugins.get("example"), null);
});
it("should register environments when scoped plugin has environments, but should not get the environment if '@scope/' is omitted", () => {
scopedPlugin.environments = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.strictEqual(environments.get("example/foo"), null);
});
it("should register rules when scoped plugin has rules, but should not get the rule if '@scope/' is omitted", () => {
scopedPlugin.rules = {
foo: {}
};
StubbedPlugins.load("@scope/eslint-plugin-example");
assert.isFalse(rules.has("example/foo"));
});
});
});
describe("loadAll()", () => {
let StubbedPlugins,
plugin1,
plugin2,
rules;
const environments = new Environments();
beforeEach(() => {
plugin1 = {};
plugin2 = {};
rules = new Map();
StubbedPlugins = new (proxyquire("../../../lib/config/plugins", {
"eslint-plugin-example1": plugin1,
"eslint-plugin-example2": plugin2
}))(environments, rules.set.bind(rules));
});
it("should load plugins when passed multiple plugins", () => {
StubbedPlugins.loadAll(["example1", "example2"]);
assert.strictEqual(StubbedPlugins.get("example1"), plugin1);
assert.strictEqual(StubbedPlugins.get("example2"), plugin2);
});
it("should load environments from plugins when passed multiple plugins", () => {
plugin1.environments = {
foo: {}
};
plugin2.environments = {
bar: {}
};
StubbedPlugins.loadAll(["example1", "example2"]);
assert.strictEqual(environments.get("example1/foo"), plugin1.environments.foo);
assert.strictEqual(environments.get("example2/bar"), plugin2.environments.bar);
});
it("should load rules from plugins when passed multiple plugins", () => {
plugin1.rules = {
foo: {}
};
plugin2.rules = {
bar: {}
};
StubbedPlugins.loadAll(["example1", "example2"]);
assert.strictEqual(rules.get("example1/foo"), plugin1.rules.foo);
assert.strictEqual(rules.get("example2/bar"), plugin2.rules.bar);
});
it("should throw an error if plugins is not an array", () => {
assert.throws(() => StubbedPlugins.loadAll("example1"), "\"plugins\" value must be an array");
});
});
});