/
override-tester.js
281 lines (240 loc) · 12.2 KB
/
override-tester.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* @fileoverview Tests for OverrideTester class.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
const assert = require("assert");
const { Console } = require("console");
const path = require("path");
const { Writable } = require("stream");
const { OverrideTester } = require("../../../../lib/cli-engine/config-array/override-tester");
describe("OverrideTester", () => {
describe("'create(files, excludedFiles, basePath)' should create a tester.", () => {
for (const { files, excludedFiles, basePath } of [
{ files: void 0, excludedFiles: void 0, basePath: process.cwd() },
{ files: [], excludedFiles: [], basePath: process.cwd() }
]) {
it(`should return null if ${JSON.stringify({ files, excludedFiles, basePath })} was given.`, () => {
assert.strictEqual(
OverrideTester.create(files, excludedFiles, basePath),
null
);
});
}
it("should return an 'OverrideTester' instance that has given parameters if strings were given.", () => {
const files = "*.js";
const excludedFiles = "ignore/*";
const basePath = process.cwd();
const tester = OverrideTester.create(files, excludedFiles, basePath);
assert.strictEqual(tester.patterns.length, 1);
assert.strictEqual(tester.patterns[0].includes.length, 1);
assert.strictEqual(tester.patterns[0].excludes.length, 1);
assert.strictEqual(tester.patterns[0].includes[0].pattern, files);
assert.strictEqual(tester.patterns[0].excludes[0].pattern, excludedFiles);
assert.strictEqual(tester.basePath, basePath);
});
it("should return an 'OverrideTester' instance that has given parameters if arrays were given.", () => {
const files = ["*.js"];
const excludedFiles = ["ignore/*"];
const basePath = process.cwd();
const tester = OverrideTester.create(files, excludedFiles, basePath);
assert.strictEqual(tester.patterns.length, 1);
assert.strictEqual(tester.patterns[0].includes.length, 1);
assert.strictEqual(tester.patterns[0].excludes.length, 1);
assert.strictEqual(tester.patterns[0].includes[0].pattern, files[0]);
assert.strictEqual(tester.patterns[0].excludes[0].pattern, excludedFiles[0]);
assert.strictEqual(tester.basePath, basePath);
});
});
describe("'and(a, b)' should return either or create another tester what includes both.", () => {
it("should return null if both were null.", () => {
assert.strictEqual(OverrideTester.and(null, null), null);
});
it("should return the first one if the second one was null.", () => {
const tester = OverrideTester.create("*.js");
assert.strictEqual(OverrideTester.and(tester, null), tester);
});
it("should return the second one if the first one was null.", () => {
const tester = OverrideTester.create("*.js");
assert.strictEqual(OverrideTester.and(null, tester), tester);
});
it("should return another one what includes both patterns if both are testers.", () => {
const tester1 = OverrideTester.create("*.js");
const tester2 = OverrideTester.create("*.ts");
const tester3 = OverrideTester.and(tester1, tester2);
assert.strictEqual(tester3.patterns.length, 2);
assert.strictEqual(tester3.patterns[0], tester1.patterns[0]);
assert.strictEqual(tester3.patterns[1], tester2.patterns[0]);
});
});
describe("'test(filePath)' method", () => {
it("should throw an error if no arguments were given.", () => {
assert.throws(() => {
OverrideTester.create(["*.js"], [], process.cwd()).test();
}, /'filePath' should be an absolute path, but got undefined/u);
});
it("should throw an error if a non-string value was given.", () => {
assert.throws(() => {
OverrideTester.create(["*.js"], [], process.cwd()).test(100);
}, /'filePath' should be an absolute path, but got 100/u);
});
it("should throw an error if a relative path was given.", () => {
assert.throws(() => {
OverrideTester.create(["*.js"], [], process.cwd()).test("foo/bar.js");
}, /'filePath' should be an absolute path, but got foo\/bar\.js/u);
});
it("should return true only when both conditions are matched if the tester was created by 'and' factory function.", () => {
const tester = OverrideTester.and(
OverrideTester.create(["*.js"], [], process.cwd()),
OverrideTester.create(["test/**"], [], process.cwd())
);
assert.strictEqual(tester.test(path.resolve("test/a.js")), true);
assert.strictEqual(tester.test(path.resolve("lib/a.js")), false);
assert.strictEqual(tester.test(path.resolve("test/a.ts")), false);
});
/**
* Test if a given file path matches to the given condition.
*
* The test cases which depend on this function were moved from
* 'tests/lib/config/config-ops.js' when refactoring to keep the
* cumulated test cases.
*
* Previously, the testing logic of `overrides` properties had been
* implemented in `ConfigOps.pathMatchesGlobs()` function. But
* currently, it's implemented in `OverrideTester` class.
*
* @param {string} filePath The file path to test patterns against
* @param {string|string[]} files One or more glob patterns
* @param {string|string[]} [excludedFiles] One or more glob patterns
* @returns {boolean} The result.
*/
function test(filePath, files, excludedFiles) {
const basePath = process.cwd();
const tester = OverrideTester.create(files, excludedFiles, basePath);
return tester.test(path.resolve(basePath, filePath));
}
/**
* Emits a test that confirms the specified file path matches the specified combination of patterns.
* @param {string} filePath The file path to test patterns against
* @param {string|string[]} patterns One or more glob patterns
* @param {string|string[]} [excludedPatterns] One or more glob patterns
* @returns {void}
*/
function match(filePath, patterns, excludedPatterns) {
it(`matches ${filePath} given '${patterns.join("','")}' includes and '${excludedPatterns.join("','")}' excludes`, () => {
const result = test(filePath, patterns, excludedPatterns);
assert.strictEqual(result, true);
});
}
/**
* Emits a test that confirms the specified file path does not match the specified combination of patterns.
* @param {string} filePath The file path to test patterns against
* @param {string|string[]} patterns One or more glob patterns
* @param {string|string[]} [excludedPatterns] One or more glob patterns
* @returns {void}
*/
function noMatch(filePath, patterns, excludedPatterns) {
it(`does not match ${filePath} given '${patterns.join("','")}' includes and '${excludedPatterns.join("','")}' excludes`, () => {
const result = test(filePath, patterns, excludedPatterns);
assert.strictEqual(result, false);
});
}
/**
* Emits a test that confirms the specified pattern throws an error.
* @param {string} filePath The file path to test the pattern against
* @param {string} pattern The glob pattern that should trigger the error condition
* @param {string} expectedMessage The expected error's message
* @returns {void}
*/
function error(filePath, pattern, expectedMessage) {
it(`emits an error given '${pattern}'`, () => {
let errorMessage;
try {
test(filePath, pattern);
} catch (e) {
errorMessage = e.message;
}
assert.strictEqual(errorMessage, expectedMessage);
});
}
// files in the project root
match("foo.js", ["foo.js"], []);
match("foo.js", ["*"], []);
match("foo.js", ["*.js"], []);
match("foo.js", ["**/*.js"], []);
match("bar.js", ["*.js"], ["foo.js"]);
match("foo.js", ["./foo.js"], []);
match("foo.js", ["./*"], []);
match("foo.js", ["./**"], []);
noMatch("foo.js", ["*"], ["foo.js"]);
noMatch("foo.js", ["*.js"], ["foo.js"]);
noMatch("foo.js", ["**/*.js"], ["foo.js"]);
// files in a subdirectory
match("subdir/foo.js", ["foo.js"], []);
match("subdir/foo.js", ["*"], []);
match("subdir/foo.js", ["*.js"], []);
match("subdir/foo.js", ["**/*.js"], []);
match("subdir/foo.js", ["subdir/*.js"], []);
match("subdir/foo.js", ["subdir/foo.js"], []);
match("subdir/foo.js", ["subdir/*"], []);
match("subdir/second/foo.js", ["subdir/**"], []);
match("subdir/foo.js", ["./**"], []);
match("subdir/foo.js", ["./subdir/**"], []);
match("subdir/foo.js", ["./subdir/*"], []);
noMatch("subdir/foo.js", ["./foo.js"], []);
noMatch("subdir/foo.js", ["*"], ["subdir/**"]);
noMatch("subdir/very/deep/foo.js", ["*.js"], ["subdir/**"]);
noMatch("subdir/second/foo.js", ["subdir/*"], []);
noMatch("subdir/second/foo.js", ["subdir/**"], ["subdir/second/*"]);
// error conditions
error("foo.js", ["/*.js"], "Invalid override pattern (expected relative path not containing '..'): /*.js");
error("foo.js", ["/foo.js"], "Invalid override pattern (expected relative path not containing '..'): /foo.js");
error("foo.js", ["../**"], "Invalid override pattern (expected relative path not containing '..'): ../**");
});
describe("'JSON.stringify(...)' should return readable JSON; not include 'Minimatch' objects", () => {
it("should return an object that has three properties 'includes', 'excludes', and 'basePath' if that 'patterns' property include one object.", () => {
const files = "*.js";
const excludedFiles = "test/*";
const basePath = process.cwd();
const tester = OverrideTester.create(files, excludedFiles, basePath);
assert.strictEqual(
JSON.stringify(tester),
`{"includes":["${files}"],"excludes":["${excludedFiles}"],"basePath":${JSON.stringify(basePath)}}`
);
});
it("should return an object that has two properties 'AND' and 'basePath' if that 'patterns' property include two or more objects.", () => {
const files1 = "*.js";
const excludedFiles1 = "test/*";
const files2 = "*.story.js";
const excludedFiles2 = "src/*";
const basePath = process.cwd();
const tester = OverrideTester.and(
OverrideTester.create(files1, excludedFiles1, basePath),
OverrideTester.create(files2, excludedFiles2, basePath)
);
assert.deepStrictEqual(
JSON.parse(JSON.stringify(tester)),
{
AND: [
{ includes: [files1], excludes: [excludedFiles1] },
{ includes: [files2], excludes: [excludedFiles2] }
],
basePath
}
);
});
});
describe("'console.log(...)' should print readable string; not include 'Minimatch' objects", () => {
const localConsole = new Console(new Writable());
it("should use 'toJSON()' method.", () => {
const tester = OverrideTester.create("*.js", "", process.cwd());
let called = false;
tester.toJSON = () => {
called = true;
return "";
};
localConsole.log(tester);
assert(called);
});
});
});