-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
test.js
294 lines (261 loc) · 8.35 KB
/
test.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
282
283
284
285
286
287
288
289
290
291
292
293
294
import {fileURLToPath} from 'node:url';
import path from 'node:path';
import {Readable} from 'node:stream';
import test from 'ava';
import {pEvent} from 'p-event';
import Vinyl from 'vinyl';
import filter from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('filter', async t => {
const stream = filter('included.js');
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'included.js'),
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'ignored.js'),
}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.relative, 'included.js');
});
test('filter with restore set to false', async t => {
const stream = filter('included.js', {restore: false});
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'included.js'),
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'ignored.js'),
}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.relative, 'included.js');
});
test('forward multimatch options', async t => {
const stream = filter('*.js', {matchBase: true});
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'nested', 'resource.js'),
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'nested', 'resource.css'),
}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.relative, path.join('nested', 'resource.js'));
});
test('filter using a function', async t => {
const stream = filter(file => file.path === 'included.js');
stream.write(new Vinyl({path: 'included.js'}));
stream.write(new Vinyl({path: 'ignored.js'}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.path, 'included.js');
});
test('filter files with negate pattern and leading dot', async t => {
const stream = filter(['*', '!*.json', '!*rc'], {dot: true});
stream.write(new Vinyl({path: 'included.js'}));
stream.write(new Vinyl({path: 'package.json'}));
stream.write(new Vinyl({path: '.jshintrc'}));
stream.write(new Vinyl({path: 'app.js'}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.path, 'included.js');
});
test('filter with respect to current working directory', async t => {
const stream = filter('test/**/*.js');
stream.write(new Vinyl({
base: path.join(__dirname, 'test'),
path: path.join(__dirname, 'test', 'included.js'),
}));
stream.write(new Vinyl({
base: __dirname,
path: path.join(__dirname, 'ignored.js'),
}));
stream.end();
const data = await pEvent(stream, 'data');
t.is(data.relative, 'included.js');
});
test('filter.restore - bring back the previously filtered files', async t => {
const stream = filter('*.json', {restore: true});
const completeStream = stream.pipe(stream.restore);
stream.write(new Vinyl({path: 'package.json'}));
stream.write(new Vinyl({path: 'app.js'}));
stream.write(new Vinyl({path: 'package2.json'}));
stream.end();
const data = await pEvent(completeStream, 'data');
t.is(data.path, 'package.json');
});
test('filter.restore - work when using multiple filters', async t => {
const streamFilter1 = filter(['*.js'], {restore: true});
const streamFilter2 = filter(['*.json'], {restore: true});
const completeStream = streamFilter1
.pipe(streamFilter2)
.pipe(streamFilter1.restore)
.pipe(streamFilter2.restore);
streamFilter1.write(new Vinyl({path: 'package.json'}));
streamFilter1.write(new Vinyl({path: 'app.js'}));
streamFilter1.write(new Vinyl({path: 'main.css'}));
streamFilter1.end();
const data = await pEvent(completeStream, 'data');
t.is(data.path, 'package.json');
});
test('filter.restore - end when not using the passthrough option', async t => {
const stream = filter('*.json', {restore: true, passthrough: false});
const restoreStream = stream.restore;
stream.write(new Vinyl({path: 'package.json'}));
stream.write(new Vinyl({path: 'app.js'}));
stream.write(new Vinyl({path: 'package2.json'}));
stream.end();
const data = await pEvent(restoreStream, 'data');
t.is(data.path, 'app.js');
});
test('filter.restore - not end before the restore stream didn\'t end', async t => {
const stream = filter('*.json', {restore: true});
const restoreStream = stream.restore;
stream.write(new Vinyl({path: 'package.json'}));
stream.write(new Vinyl({path: 'app.js'}));
stream.end();
const data = await pEvent(restoreStream, 'data');
t.is(data.path, 'app.js');
});
test('filter.restore - pass files as they come', async t => {
const stream = filter('*.json', {restore: true});
const restoreStream = stream.restore;
stream.pipe(restoreStream);
stream.write(new Vinyl({path: 'package.json'}));
stream.write(new Vinyl({path: 'app.js'}));
stream.write(new Vinyl({path: 'package2.json'}));
stream.write(new Vinyl({path: 'app2.js'}));
stream.end();
const data = await pEvent(restoreStream, 'data');
t.is(data.path, 'package.json');
});
test('filter.restore - work when restore stream is not used', async t => {
t.plan(1);
const stream = filter('*.json');
for (let index = 0; index < stream._writableState.highWaterMark + 1; index++) {
stream.write(new Vinyl({path: 'nonmatch.js'}));
}
const finish = pEvent(stream, 'finish');
stream.end();
await finish;
t.pass();
});
test('path matching', async t => {
const testFilesPaths = [
'/test.js',
'/A/test.js',
'/A/C/test.js',
'/A/B/test.js',
'/A/B/C/test.js',
'/A/B/C/d.js',
];
const testFiles = testFilesPaths.map(filePath => new Vinyl({cwd: '/A/B', path: filePath}));
const testCases = [
{
description: 'Filename by suffix',
pattern: ['*.js'],
expectedFiles: testFiles,
},
{
description: 'Filename by suffix, excluding d.js',
pattern: ['*.js', '!d.js'],
expectedFiles: testFiles.slice(0, -1),
},
{
description: 'Absolute filter by suffix',
pattern: ['/**/*.js'],
expectedFiles: testFiles,
},
{
description: 'Absolute filter by suffix with prefix',
pattern: ['/A/**/*.js'],
expectedFiles: testFiles.slice(1),
},
{
description: 'Absolute filter by suffix with prefix equal to base',
pattern: ['/A/B/**/*.js'],
expectedFiles: testFiles.slice(3),
},
{
description: 'Relative filter',
pattern: ['**/*.js'],
expectedFiles: testFiles.slice(3),
},
{
description: 'Relative filter but explicit',
pattern: ['./**/*.js'],
expectedFiles: testFiles.slice(3),
},
{
description: 'Relative filter with .. prefix',
pattern: ['../**/*.js'],
expectedFiles: testFiles.slice(1),
},
{
description: 'Relative filter with path prefix',
pattern: ['C/**/*.js'],
expectedFiles: testFiles.slice(4),
},
{
description: 'Relative filter with path prefix, but then ..',
pattern: ['C/../**/*.js'],
expectedFiles: testFiles.slice(3),
},
{
description: 'Absolute filter starting with !',
pattern: ['/**/*', '!/**/*.js'],
expectedFiles: [],
},
{
description: 'Absolute filter starting with !, filters out all test.js',
pattern: ['/**/*', '!/**/test.js'],
expectedFiles: [testFiles[5]],
},
{
description: 'Absolute filter starting with !, . omitted',
pattern: ['/**/*', '!**/*.js'],
expectedFiles: testFiles.slice(0, 3),
},
{
description: 'Relative filter starting with !, with .',
pattern: ['/**/*', '!./**/*.js'],
expectedFiles: testFiles.slice(0, 3),
},
{
description: 'Mixed filters: absolute filter take files, when absolute negated filter rejects',
pattern: ['/A/**/*.js', '!/A/B/**/*.js'],
expectedFiles: testFiles.slice(1, 3),
},
{
description: 'Mixed filters: relative filter take files, when absolute negated filter rejects',
pattern: ['**/*.js', '!/A/B/C/**/*.js'],
expectedFiles: testFiles.slice(3, 4),
},
{
description: 'Mixed filters: absolute filter take files, when relative negated filter rejects',
pattern: ['/A/**/*.js', '!./C/**/*.js'],
expectedFiles: testFiles.slice(1, 4),
},
{
description: 'Mixed filters: relative filter take files, when relative negated filter rejects',
pattern: ['**/*.js', '!./C/**/*.js'],
expectedFiles: testFiles.slice(3, 4),
},
];
for (const testCase of testCases) {
const stream = filter(testCase.pattern);
const promise = Readable.from(stream).toArray();
for (const testFile of testFiles) {
stream.write(testFile);
}
stream.end();
const files = await promise; // eslint-disable-line no-await-in-loop
t.deepEqual(files.map(file => file.path), testCase.expectedFiles.map(file => file.path));
}
});