/
others.js
243 lines (193 loc) 路 4.17 KB
/
others.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
// - issues
// - options
// - static methods
// - .test()
const {test} = require('tap')
const ignore = require('..')
const {
checkEnv,
SHOULD_TEST_WINDOWS
} = require('./fixtures/cases')
const {isPathValid} = ignore
const _test = checkEnv('IGNORE_ONLY_OTHERS')
? test
: () => {}
_test('.add(<Ignore>)', t => {
const a = ignore().add(['.abc/*', '!.abc/d/'])
const b = ignore().add(a).add('!.abc/e/')
const paths = [
'.abc/a.js', // filtered out
'.abc/d/e.js', // included
'.abc/e/e.js' // included by b, filtered out by a
]
t.same(a.filter(paths), ['.abc/d/e.js'])
t.same(b.filter(paths), ['.abc/d/e.js', '.abc/e/e.js'])
t.end()
})
_test('fixes babel class', t => {
const {constructor} = ignore()
try {
constructor()
} catch (e) {
t.end()
return
}
t.equal('there should be an error', 'no error found')
t.end()
})
_test('#32', t => {
const KEY_IGNORE = typeof Symbol !== 'undefined'
? Symbol.for('node-ignore')
: 'node-ignore'
const a = ignore().add(['.abc/*', '!.abc/d/'])
// aa is actually not an IgnoreBase instance
const aa = {}
/* eslint no-underscore-dangle: ["off"] */
aa._rules = a._rules.slice()
aa[KEY_IGNORE] = true
const b = ignore().add(aa).add('!.abc/e/')
const paths = [
'.abc/a.js', // filtered out
'.abc/d/e.js', // included
'.abc/e/e.js' // included by b, filtered out by a
]
t.same(a.filter(paths), ['.abc/d/e.js'])
t.same(b.filter(paths), ['.abc/d/e.js', '.abc/e/e.js'])
t.end()
})
_test('options.ignorecase', t => {
const ig = ignore({
ignorecase: false
})
ig.add('*.[jJ][pP]g')
t.equal(ig.ignores('a.jpg'), true)
t.equal(ig.ignores('a.JPg'), true)
t.equal(ig.ignores('a.JPG'), false)
t.end()
})
_test('special case: internal cache respects ignorecase', t => {
const rule = '*.[jJ][pP]g'
const ig = ignore({
ignorecase: false
})
ig.add(rule)
t.equal(ig.ignores('a.JPG'), false)
const ig2 = ignore({
ignorecase: true
})
ig2.add(rule)
t.equal(ig2.ignores('a.JPG'), true)
t.end()
})
_test('special case: invalid paths, throw', t => {
const ig = ignore()
const emptyMessage = 'path must be a string, but got ""'
t.throws(() => ig.ignores(''), emptyMessage)
t.throws(
() => ig.ignores(false),
'path must be a string, but got `false`'
)
t.throws(
() => ig.ignores('/a'),
'path must be `path.relative()`d, but got "/a"'
)
if (SHOULD_TEST_WINDOWS) {
t.throws(
() => ig.ignores('c:\\a'),
'path must be `path.relative()`d, but got "c:\\a"'
)
t.throws(
() => ig.ignores('C:\\a'),
'path must be `path.relative()`d, but got "C:\\a"'
)
}
t.throws(() => ig.filter(['']), emptyMessage)
t.throws(() => [''].filter(ig.createFilter()), emptyMessage)
t.end()
})
_test('isPathValid', t => {
const paths = [
'.',
'./foo',
'../foo',
'/foo',
false,
'foo'
]
if (SHOULD_TEST_WINDOWS) {
paths.push(
'..\\foo',
'.\\foo',
'\\foo',
'\\\\foo',
'C:\\foo',
'd:\\foo'
)
}
t.same(
paths.filter(isPathValid),
[
'foo'
]
)
t.end()
})
const IGNORE_TEST_CASES = [
[
'test: no rule',
null,
'foo',
[false, false]
],
[
'test: has rule, no match',
'bar',
'foo',
[false, false]
],
[
'test: only negative',
'!foo',
'foo',
[false, true]
],
[
'test: ignored then unignored',
['foo', '!foo'],
'foo',
[false, true]
],
[
'test: dir ignored then unignored -> not matched',
['foo', '!foo'],
'foo/bar',
[false, false]
],
[
'test: ignored by wildcard, then unignored',
['*.js', '!a/a.js'],
'a/a.js',
[false, true]
]
]
IGNORE_TEST_CASES.forEach(([d, patterns, path, [ignored, unignored]]) => {
_test(d, t => {
const ig = ignore()
if (patterns) {
ig.add(patterns)
}
t.same(ig.test(path), {
ignored, unignored
})
t.end()
})
})
_test('options.allowRelativePaths', t => {
const ig = ignore({
allowRelativePaths: true
})
ig.add('foo')
t.equal(ig.ignores('../foo/bar.js'), true)
t.throws(() => ignore().ignores('../foo/bar.js'))
t.end()
})