-
-
Notifications
You must be signed in to change notification settings - Fork 558
/
createFilter.ts
executable file
·141 lines (119 loc) · 4.39 KB
/
createFilter.ts
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
import { resolve as rawResolve } from 'path';
import test from 'ava';
import { createFilter, normalizePath } from '../';
const resolve = (...parts: string[]) => normalizePath(rawResolve(...parts));
test.beforeEach(() => process.chdir(__dirname));
test('includes by default', (t) => {
const filter = createFilter();
t.truthy(filter(resolve('x')));
});
test('excludes IDs that are not included, if include.length > 0', (t) => {
const filter = createFilter(['y']);
t.falsy(filter(resolve('x')));
t.truthy(filter(resolve('y')));
});
test('excludes IDs explicitly', (t) => {
const filter = createFilter(null, ['y']);
t.truthy(filter(resolve('x')));
t.falsy(filter(resolve('y')));
});
test('handles non-array arguments', (t) => {
const filter = createFilter('foo/*', 'foo/baz');
t.truthy(filter(resolve('foo/bar')));
t.falsy(filter(resolve('foo/baz')));
});
test('negation patterns', (t) => {
const filter = createFilter(['a/!(b)/c']);
t.truthy(filter(resolve('a/d/c')));
t.falsy(filter(resolve('a/b/c')));
});
test('excludes non-string IDs', (t) => {
const filter = createFilter(null, null);
t.falsy(filter({}));
});
test('excludes strings beginning with NUL', (t) => {
const filter = createFilter(null, null);
t.falsy(filter('\0someid'));
});
test('includes with regexp', (t) => {
const filter = createFilter(['a/!(b)/c', /\.js$/]);
t.truthy(filter(resolve('a/d/c')));
t.falsy(filter(resolve('a/b/c')));
t.truthy(filter(resolve('a.js')));
t.truthy(filter(resolve('a/b.js')));
t.falsy(filter(resolve('a/b.jsx')));
});
test('excludes with regexp', (t) => {
const filter = createFilter(['a/!(b)/c', /\.js$/], /\.js$/);
t.truthy(filter(resolve('a/d/c')));
t.falsy(filter(resolve('a/b/c')));
t.falsy(filter(resolve('a.js')));
t.falsy(filter(resolve('a/b.js')));
t.falsy(filter(resolve('a/b.jsx')));
});
test('allows setting an absolute base dir', (t) => {
const baseDir = resolve('C');
const filter = createFilter(['y*'], ['yx'], { resolve: baseDir });
t.falsy(filter(`${baseDir}/x`));
t.truthy(filter(`${baseDir}/ys`));
t.falsy(filter(`${baseDir}/yx`));
t.falsy(filter(resolve('C/d/ys')));
t.falsy(filter(resolve('ys')));
t.falsy(filter('ys'));
});
test('allows setting a relative base dir', (t) => {
const filter = createFilter(['y*'], ['yx'], { resolve: 'C/d' });
t.falsy(filter(resolve('C/d/x')));
t.truthy(filter(resolve('C/d/ys')));
t.falsy(filter(resolve('C/d/yx')));
t.falsy(filter(`${resolve('C')}/ys`));
t.falsy(filter(resolve('ys')));
t.falsy(filter('ys'));
});
test('ignores a falsy resolve value', (t) => {
const filter = createFilter(['y*'], ['yx'], { resolve: null });
t.falsy(filter(resolve('x')));
t.truthy(filter(resolve('ys')));
t.falsy(filter(resolve('yx')));
t.falsy(filter(`${resolve('C')}/ys`));
t.falsy(filter(resolve('C/d/ys')));
t.falsy(filter('ys'));
});
test('allows preventing resolution against process.cwd()', (t) => {
const filter = createFilter(['y*'], ['yx'], { resolve: false });
t.falsy(filter('x'));
t.truthy(filter('ys'));
t.falsy(filter('yx'));
t.falsy(filter(`${resolve('C')}/ys`));
t.falsy(filter(resolve('C/d/ys')));
t.falsy(filter(resolve('ys')));
});
test('includes names starting with a "."', (t) => {
const filter = createFilter(['**/*a']);
t.truthy(filter(resolve('.a')));
t.truthy(filter(resolve('.x/a')));
});
test.serial('includes names containing parenthesis', (t) => {
process.chdir(resolve(__dirname, 'fixtures/folder-with (parens)'));
const filter = createFilter(['*.ts+(|x)', '**/*.ts+(|x)'], ['*.d.ts', '**/*.d.ts']);
t.truthy(filter(resolve('folder (test)/src/main.tsx')));
t.truthy(filter(resolve('.x/(test)a.ts')));
t.falsy(filter(resolve('.x/(test)a.d.ts')));
});
test('handles relative paths', (t) => {
const filter = createFilter(['./index.js', './foo/../a.js']);
t.truthy(filter(resolve('index.js')));
t.truthy(filter(resolve('a.js')));
t.falsy(filter(resolve('foo/a.js')));
});
test('does not add current working directory when pattern is an absolute path', (t) => {
const filter = createFilter([resolve('..', '..', '*')]);
t.truthy(filter(resolve('..', '..', 'a')));
t.truthy(filter(resolve('..', '..', 'b')));
t.falsy(filter(resolve('..', 'c')));
});
test('does not add current working directory when pattern starts with a glob', (t) => {
const filter = createFilter(['**/*']);
t.truthy(filter(resolve('a')));
t.truthy(filter(resolve('..', '..', 'a')));
});