]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/tools/config-rule.js
import 8.23.1 source
[pve-eslint.git] / eslint / tests / tools / config-rule.js
1 /**
2 * @fileoverview Tests for ConfigOps
3 * @author Ian VanSchooten
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const assert = require("chai").assert,
13 ConfigRule = require("../../tools/config-rule"),
14 builtInRules = require("../../lib/rules"),
15 schema = require("../fixtures/config-rule/schemas");
16
17 //------------------------------------------------------------------------------
18 // Tests
19 //------------------------------------------------------------------------------
20
21 const SEVERITY = 2;
22
23 describe("ConfigRule", () => {
24
25 describe("generateConfigsFromSchema()", () => {
26 let actualConfigs;
27
28 it("should create a config with only severity for an empty schema", () => {
29 actualConfigs = ConfigRule.generateConfigsFromSchema([]);
30 assert.deepStrictEqual(actualConfigs, [SEVERITY]);
31 });
32
33 it("should create a config with only severity with no arguments", () => {
34 actualConfigs = ConfigRule.generateConfigsFromSchema();
35 assert.deepStrictEqual(actualConfigs, [SEVERITY]);
36 });
37
38 describe("for a single enum schema", () => {
39
40 before(() => {
41 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.enum);
42 });
43
44 it("should create an array of configs", () => {
45 assert.isArray(actualConfigs);
46 assert.strictEqual(actualConfigs.length, 3);
47 });
48
49 it("should include the error severity (2) without options as the first config", () => {
50 assert.strictEqual(actualConfigs[0], SEVERITY);
51 });
52
53 it("should set all configs to error severity (2)", () => {
54 actualConfigs.forEach(actualConfig => {
55 if (Array.isArray(actualConfig)) {
56 assert.strictEqual(actualConfig[0], SEVERITY);
57 }
58 });
59 });
60
61 it("should return configs with each enumerated value in the schema", () => {
62 assert.sameDeepMembers(actualConfigs, [SEVERITY, [SEVERITY, "always"], [SEVERITY, "never"]]);
63 });
64 });
65
66 describe("for a object schema with a single enum property", () => {
67
68 before(() => {
69 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.objectWithEnum);
70 });
71
72 it("should return configs with option objects", () => {
73
74 // Skip first config (severity only)
75 actualConfigs.slice(1).forEach(actualConfig => {
76 const actualConfigOption = actualConfig[1]; // severity is first element, option is second
77
78 assert.isObject(actualConfigOption);
79 });
80 });
81
82 it("should use the object property name from the schema", () => {
83 const propName = "enumProperty";
84
85 assert.strictEqual(actualConfigs.length, 3);
86 actualConfigs.slice(1).forEach(actualConfig => {
87 const actualConfigOption = actualConfig[1];
88
89 assert.property(actualConfigOption, propName);
90 });
91 });
92
93 it("should have each enum as option object values", () => {
94 const propName = "enumProperty",
95 actualValues = [];
96
97 actualConfigs.slice(1).forEach(actualConfig => {
98 const configOption = actualConfig[1];
99
100 actualValues.push(configOption[propName]);
101 });
102 assert.sameMembers(actualValues, ["always", "never"]);
103 });
104 });
105
106 describe("for a object schema with a multiple enum properties", () => {
107
108 before(() => {
109 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.objectWithMultipleEnums);
110 });
111
112 it("should create configs for all properties in each config", () => {
113 const expectedProperties = ["firstEnum", "anotherEnum"];
114
115 assert.strictEqual(actualConfigs.length, 7);
116 actualConfigs.slice(1).forEach(actualConfig => {
117 const configOption = actualConfig[1];
118 const actualProperties = Object.keys(configOption);
119
120 assert.sameMembers(actualProperties, expectedProperties);
121 });
122 });
123
124 it("should create configs for every possible combination", () => {
125 const expectedConfigs = [
126 { firstEnum: "always", anotherEnum: "var" },
127 { firstEnum: "always", anotherEnum: "let" },
128 { firstEnum: "always", anotherEnum: "const" },
129 { firstEnum: "never", anotherEnum: "var" },
130 { firstEnum: "never", anotherEnum: "let" },
131 { firstEnum: "never", anotherEnum: "const" }
132 ];
133 const actualConfigOptions = actualConfigs.slice(1).map(actualConfig => actualConfig[1]);
134
135 assert.sameDeepMembers(actualConfigOptions, expectedConfigs);
136 });
137
138 });
139
140 describe("for a object schema with a single boolean property", () => {
141
142 before(() => {
143 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.objectWithBool);
144 });
145
146 it("should return configs with option objects", () => {
147 assert.strictEqual(actualConfigs.length, 3);
148 actualConfigs.slice(1).forEach(actualConfig => {
149 const actualConfigOption = actualConfig[1];
150
151 assert.isObject(actualConfigOption);
152 });
153 });
154
155 it("should use the object property name from the schema", () => {
156 const propName = "boolProperty";
157
158 assert.strictEqual(actualConfigs.length, 3);
159 actualConfigs.slice(1).forEach(actualConfig => {
160 const actualConfigOption = actualConfig[1];
161
162 assert.property(actualConfigOption, propName);
163 });
164 });
165
166 it("should include both true and false configs", () => {
167 const propName = "boolProperty",
168 actualValues = [];
169
170 actualConfigs.slice(1).forEach(actualConfig => {
171 const configOption = actualConfig[1];
172
173 actualValues.push(configOption[propName]);
174 });
175 assert.sameMembers(actualValues, [true, false]);
176 });
177 });
178
179 describe("for a object schema with a multiple bool properties", () => {
180
181 before(() => {
182 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.objectWithMultipleBools);
183 });
184
185 it("should create configs for all properties in each config", () => {
186 const expectedProperties = ["firstBool", "anotherBool"];
187
188 assert.strictEqual(actualConfigs.length, 5);
189 actualConfigs.slice(1).forEach(config => {
190 const configOption = config[1];
191 const actualProperties = Object.keys(configOption);
192
193 assert.sameMembers(actualProperties, expectedProperties);
194 });
195 });
196
197 it("should create configs for every possible combination", () => {
198 const expectedConfigOptions = [
199 { firstBool: true, anotherBool: true },
200 { firstBool: true, anotherBool: false },
201 { firstBool: false, anotherBool: true },
202 { firstBool: false, anotherBool: false }
203 ];
204 const actualConfigOptions = actualConfigs.slice(1).map(config => config[1]);
205
206 assert.sameDeepMembers(actualConfigOptions, expectedConfigOptions);
207 });
208 });
209
210 describe("for a schema with an enum and an object", () => {
211
212 before(() => {
213 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.mixedEnumObject);
214 });
215
216 it("should create configs with only the enum values", () => {
217 assert.strictEqual(actualConfigs[1].length, 2);
218 assert.strictEqual(actualConfigs[2].length, 2);
219 const actualOptions = [actualConfigs[1][1], actualConfigs[2][1]];
220
221 assert.sameMembers(actualOptions, ["always", "never"]);
222 });
223
224 it("should create configs with a string and an object", () => {
225 assert.strictEqual(actualConfigs.length, 7);
226 actualConfigs.slice(3).forEach(config => {
227 assert.isString(config[1]);
228 assert.isObject(config[2]);
229 });
230 });
231 });
232
233 describe("for a schema with an enum followed by an object with no usable properties", () => {
234 before(() => {
235 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.mixedEnumObjectWithNothing);
236 });
237
238 it("should create config only for the enum", () => {
239 const expectedConfigs = [2, [2, "always"], [2, "never"]];
240
241 assert.sameDeepMembers(actualConfigs, expectedConfigs);
242 });
243 });
244
245 describe("for a schema with an enum preceded by an object with no usable properties", () => {
246 before(() => {
247 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.mixedObjectWithNothingEnum);
248 });
249
250 it("should not create a config for the enum", () => {
251 const expectedConfigs = [2];
252
253 assert.sameDeepMembers(actualConfigs, expectedConfigs);
254 });
255 });
256
257 describe("for a schema with an enum preceded by a string", () => {
258 before(() => {
259 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.mixedStringEnum);
260 });
261
262 it("should not create a config for the enum", () => {
263 const expectedConfigs = [2];
264
265 assert.sameDeepMembers(actualConfigs, expectedConfigs);
266 });
267 });
268
269 describe("for a schema with oneOf", () => {
270
271 before(() => {
272 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.oneOf);
273 });
274
275 it("should create a set of configs", () => {
276 assert.isArray(actualConfigs);
277 });
278 });
279
280 describe("for a schema with nested objects", () => {
281
282 before(() => {
283 actualConfigs = ConfigRule.generateConfigsFromSchema(schema.nestedObjects);
284 });
285
286 it("should create a set of configs", () => {
287 assert.isArray(actualConfigs);
288 });
289 });
290 });
291
292 describe("createCoreRuleConfigs()", () => {
293
294 const rulesConfig = ConfigRule.createCoreRuleConfigs();
295
296 it("should create a rulesConfig containing all core rules", () => {
297 const
298 expectedRules = Array.from(builtInRules.keys()),
299 actualRules = Object.keys(rulesConfig);
300
301 assert.sameMembers(actualRules, expectedRules);
302 });
303
304 it("should allow to ignore deprecated rules", () => {
305 const expectedRules = Array.from(builtInRules.entries())
306 .filter(([, rule]) => {
307 const isDeprecated = (typeof rule === "function") ? rule.deprecated : rule.meta.deprecated;
308
309 return !isDeprecated;
310 })
311 .map(([id]) => id),
312 actualRules = Object.keys(ConfigRule.createCoreRuleConfigs(true));
313
314 assert.sameMembers(actualRules, expectedRules);
315
316 // Make sure it doesn't contain deprecated rules.
317 assert.notInclude(actualRules, "newline-after-var");
318 });
319
320 it("should create arrays of configs for rules", () => {
321 assert.isArray(rulesConfig.quotes);
322 assert.include(rulesConfig.quotes, 2);
323 });
324
325 it("should create configs for rules with meta", () => {
326 assert(rulesConfig["accessor-pairs"].length > 1);
327 });
328 });
329 });