Debian Codegraph @

fast-glob/src/providers/filters/entry.spec.ts

raw
1 import * as assert from 'assert';
2 import * as path from 'path';
3
4 import Settings, { Options } from '../../settings';
5 import * as tests from '../../tests';
6 import { EntryFilterFunction, Pattern, Entry } from '../../types';
7 import EntryFilter from './entry';
8
9 type FilterOptions = {
10 positive: Pattern[];
11 negative?: Pattern[];
12 options?: Options;
13 };
14
15 const FILE_ENTRY = tests.entry.builder().path('root/file.txt').file().build();
16 const DIRECTORY_ENTRY = tests.entry.builder().path('root/directory').directory().build();
17
18 function getEntryFilterInstance(options?: Options): EntryFilter {
19 const settings = new Settings(options);
20
21 return new EntryFilter(settings, {
22 dot: settings.dot
23 });
24 }
25
26 function getFilter(options: FilterOptions): EntryFilterFunction {
27 const negative = options.negative ?? [];
28
29 return getEntryFilterInstance(options.options).getFilter(options.positive, negative);
30 }
31
32 function getResult(entry: Entry, options: FilterOptions): boolean {
33 const filter = getFilter(options);
34
35 return filter(entry);
36 }
37
38 function accept(entry: Entry, options: FilterOptions): void {
39 assert.strictEqual(getResult(entry, options), true);
40 }
41
42 function reject(entry: Entry, options: FilterOptions): void {
43 assert.strictEqual(getResult(entry, options), false);
44 }
45
46 describe('Providers → Filters → Entry', () => {
47 describe('Constructor', () => {
48 it('should create instance of class', () => {
49 const filter = getEntryFilterInstance();
50
51 assert.ok(filter instanceof EntryFilter);
52 });
53 });
54
55 describe('.getFilter', () => {
56 describe('options.unique', () => {
57 it('should do not build the index when an option is disabled', () => {
58 const filterInstance = getEntryFilterInstance({ unique: false });
59
60 const filter = filterInstance.getFilter(['**/*'], []);
61
62 filter(FILE_ENTRY);
63
64 assert.strictEqual(filterInstance.index.size, 0);
65 });
66
67 it('should do not add an entry to the index when an entry does not match to patterns', () => {
68 const filterInstance = getEntryFilterInstance();
69
70 const filter = filterInstance.getFilter(['**/*.unrelated-file-extension'], []);
71
72 filter(FILE_ENTRY);
73
74 assert.strictEqual(filterInstance.index.size, 0);
75 });
76
77 it('should reject a duplicate entry', () => {
78 const filter = getFilter({
79 positive: ['**/*']
80 });
81
82 filter(FILE_ENTRY);
83
84 const actual = filter(FILE_ENTRY);
85
86 assert.ok(!actual);
87 });
88
89 it('should accept a duplicate entry when an option is disabled', () => {
90 const filter = getFilter({
91 positive: ['**/*'],
92 options: { unique: false }
93 });
94
95 filter(FILE_ENTRY);
96
97 const actual = filter(FILE_ENTRY);
98
99 assert.ok(actual);
100 });
101 });
102
103 describe('options.onlyFiles', () => {
104 it('should reject a directory entry', () => {
105 reject(DIRECTORY_ENTRY, {
106 positive: ['**/*'],
107 options: { onlyFiles: true }
108 });
109 });
110
111 it('should accept a directory entry', () => {
112 accept(DIRECTORY_ENTRY, {
113 positive: ['**/*'],
114 options: { onlyFiles: false }
115 });
116 });
117
118 it('should accept a file entry', () => {
119 accept(FILE_ENTRY, {
120 positive: ['**/*'],
121 options: { onlyFiles: true }
122 });
123 });
124 });
125
126 describe('options.onlyDirectories', () => {
127 it('should reject a file entry', () => {
128 reject(FILE_ENTRY, {
129 positive: ['**/*'],
130 options: { onlyDirectories: true }
131 });
132 });
133
134 it('should accept a directory entry', () => {
135 accept(DIRECTORY_ENTRY, {
136 positive: ['**/*'],
137 options: { onlyDirectories: true }
138 });
139 });
140 });
141
142 describe('options.absolute', () => {
143 it('should reject when an entry match to the negative pattern', () => {
144 reject(FILE_ENTRY, {
145 positive: ['**/*'],
146 negative: ['**/*'],
147 options: { absolute: true }
148 });
149 });
150
151 it('should reject when an entry match to the negative pattern with absolute path', () => {
152 const negative = path.posix.join(process.cwd().replace(/\\/g, '/'), '**', '*');
153
154 reject(FILE_ENTRY, {
155 positive: ['**/*'],
156 negative: [negative],
157 options: { absolute: true }
158 });
159 });
160
161 it('should accept when an entry does not match to the negative pattern', () => {
162 accept(FILE_ENTRY, {
163 positive: ['**/*'],
164 negative: ['*'],
165 options: { absolute: true }
166 });
167 });
168
169 it('should accept when an entry does not match to the negative pattern with absolute path', () => {
170 const negative = path.posix.join(process.cwd().replace(/\\/g, '/'), 'non-root', '**', '*');
171
172 accept(FILE_ENTRY, {
173 positive: ['**/*'],
174 negative: [negative],
175 options: { absolute: true }
176 });
177 });
178 });
179
180 describe('options.baseNameMatch', () => {
181 it('should reject an entry', () => {
182 reject(FILE_ENTRY, {
183 positive: ['*'],
184 options: { baseNameMatch: false }
185 });
186 });
187
188 it('should accept an entry', () => {
189 accept(FILE_ENTRY, {
190 positive: ['*'],
191 options: { baseNameMatch: true }
192 });
193 });
194 });
195
196 describe('Pattern', () => {
197 it('should reject when an entry match to the negative pattern', () => {
198 reject(FILE_ENTRY, {
199 positive: ['**/*'],
200 negative: ['**/*']
201 });
202 });
203
204 it('should reject when an entry does not match to the positive pattern', () => {
205 reject(FILE_ENTRY, {
206 positive: ['*']
207 });
208 });
209
210 it('should accept when an entry match to the positive pattern with a leading dot', () => {
211 accept(FILE_ENTRY, {
212 positive: ['./**/*']
213 });
214 });
215
216 it('should accept an entry with a leading dot', () => {
217 const entry = tests.entry.builder().path('./root/file.txt').file().build();
218
219 accept(entry, {
220 positive: ['**/*']
221 });
222 });
223
224 it('should accept when an entry match to the positive pattern', () => {
225 accept(FILE_ENTRY, {
226 positive: ['**/*']
227 });
228 });
229
230 it('should try to apply patterns to the path with the trailing slash for directory entry', () => {
231 accept(DIRECTORY_ENTRY, {
232 positive: ['**/'],
233 options: { onlyFiles: false }
234 });
235 });
236
237 it('should not try to apply patterns to the path with the trailing slash for non-directory entry', () => {
238 reject(FILE_ENTRY, {
239 positive: ['**/'],
240 options: { onlyFiles: false }
241 });
242 });
243 });
244 });
245
246 describe('Immutability', () => {
247 it('should return the data without changes', () => {
248 const filter = getFilter({
249 positive: ['**/*']
250 });
251
252 const reference = tests.entry.builder().path('root/file.txt').file().build();
253 const entry = tests.entry.builder().path('root/file.txt').file().build();
254
255 filter(entry);
256
257 assert.deepStrictEqual(entry, reference);
258 });
259 });
260 });