forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignore-pattern.js
159 lines (137 loc) · 6.45 KB
/
ignore-pattern.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
/**
* @fileoverview Tests for IgnorePattern class.
* @author Toru Nagashima <https://github.com/mysticatea>
*/
"use strict";
const assert = require("assert");
const path = require("path");
const sinon = require("sinon");
const { IgnorePattern } = require("../../../../lib/cli-engine/config-array/ignore-pattern");
describe("IgnorePattern", () => {
describe("constructor(patterns, basePath)", () => {
it("should bind the first argument to 'patterns' property.", () => {
const p = new IgnorePattern(["a.js"], process.cwd());
assert.deepStrictEqual(p.patterns, ["a.js"]);
});
it("should bind the second argument to 'basePath' property.", () => {
const p = new IgnorePattern(["a.js"], process.cwd());
assert.strictEqual(p.basePath, process.cwd());
});
it("should throw an error if the second argument was not an absolute path.", () => {
assert.throws(() => new IgnorePattern([], "a.js"), "");
});
});
describe("getPatternsRelativeTo(newBasePath)", () => {
it("should return 'patterns' as-is if the argument is the same as 'basePath'.", () => {
const basePath1 = path.join(process.cwd(), "foo/bar");
const p = new IgnorePattern(["a.js", "/b.js", "!c.js", "!/d.js"], basePath1);
assert.deepStrictEqual(
p.getPatternsRelativeTo(basePath1),
["a.js", "/b.js", "!c.js", "!/d.js"]
);
});
it("should return modified 'patterns' if the argument is different from 'basePath'.", () => {
const basePath1 = path.join(process.cwd(), "foo/bar");
const basePath2 = process.cwd();
const p = new IgnorePattern(["a.js", "/b.js", "!c.js", "!/d.js"], basePath1);
assert.deepStrictEqual(
p.getPatternsRelativeTo(basePath2),
["/foo/bar/**/a.js", "/foo/bar/b.js", "!/foo/bar/**/c.js", "!/foo/bar/d.js"]
);
});
});
describe("static createIgnore(ignorePatterns)", () => {
describe("with two patterns should return a function, and the function", () => {
/**
* performs static createIgnre assertions against the cwd.
* @param {string} cwd cwd to be the base path for assertions
* @returns {void}
*/
function assertions(cwd) {
const basePath1 = path.join(cwd, "foo/bar");
const basePath2 = path.join(cwd, "abc/");
const ignores = IgnorePattern.createIgnore([
new IgnorePattern(["*.js", "/*.ts", "!a.*", "!/b.*"], basePath1),
new IgnorePattern(["*.js", "/*.ts", "!a.*", "!/b.*"], basePath2)
]);
const patterns = [
["a.js", false],
["a.ts", false],
["b.js", false],
["b.ts", false],
["c.js", false],
["c.ts", false],
["dir/a.js", false],
["dir/a.ts", false],
["dir/b.js", false],
["dir/b.ts", false],
["dir/c.js", false],
["dir/c.ts", false],
["foo/bar/a.js", false],
["foo/bar/a.ts", false],
["foo/bar/b.js", false],
["foo/bar/b.ts", false],
["foo/bar/c.js", true],
["foo/bar/c.ts", true],
["foo/bar/dir/a.js", false],
["foo/bar/dir/a.ts", false],
["foo/bar/dir/b.js", true],
["foo/bar/dir/b.ts", false],
["foo/bar/dir/c.js", true],
["foo/bar/dir/c.ts", false],
["abc/a.js", false],
["abc/a.ts", false],
["abc/b.js", false],
["abc/b.ts", false],
["abc/c.js", true],
["abc/c.ts", true],
["abc/dir/a.js", false],
["abc/dir/a.ts", false],
["abc/dir/b.js", true],
["abc/dir/b.ts", false],
["abc/dir/c.js", true],
["abc/dir/c.ts", false]
];
for (const [filename, expected] of patterns) {
it(`should return ${expected} if '${filename}' was given.`, () => {
assert.strictEqual(ignores(path.join(cwd, filename)), expected);
});
}
it("should return false if '.dot.js' and false was given.", () => {
assert.strictEqual(ignores(path.join(cwd, ".dot.js"), false), true);
});
it("should return true if '.dot.js' and true were given.", () => {
assert.strictEqual(ignores(path.join(cwd, ".dot.js"), true), false);
});
it("should return false if '.dot/foo.js' and false was given.", () => {
assert.strictEqual(ignores(path.join(cwd, ".dot/foo.js"), false), true);
});
it("should return true if '.dot/foo.js' and true were given.", () => {
assert.strictEqual(ignores(path.join(cwd, ".dot/foo.js"), true), false);
});
}
assertions(process.cwd());
assertions(path.parse(process.cwd()).root);
});
});
describe("static createIgnore(ignorePatterns)", () => {
it("with common ancestor of drive root on windows should not throw", () => {
try {
// when not on windows return win32 values so local runs on linux produce the same issue as windows.
if (process.platform !== "win32") {
sinon.stub(process, "platform").value("win32");
sinon.stub(path, "sep").value(path.win32.sep);
sinon.replace(path, "isAbsolute", path.win32.isAbsolute);
}
const ignores = IgnorePattern.createIgnore([
new IgnorePattern(["*.js"], "C:\\foo\\bar"),
new IgnorePattern(["*.js"], "C:\\abc\\")
]);
// calls to this should not throw when getCommonAncestor returns root of drive
ignores("C:\\abc\\contract.d.ts");
} finally {
sinon.restore();
}
});
});
});