/
test.js
238 lines (203 loc) · 6.37 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
import fs from 'fs';
import util from 'util';
import path from 'path';
import test from 'ava';
import m from '.';
const cwd = process.cwd();
const tmp = 'tmp';
const fixture = [
'a.tmp',
'b.tmp',
'c.tmp',
'd.tmp',
'e.tmp'
];
test.before(() => {
if (!fs.existsSync(tmp)) {
fs.mkdirSync(tmp);
}
fixture.forEach(fs.writeFileSync.bind(fs));
fixture.forEach(x => fs.writeFileSync(path.join(__dirname, tmp, x)));
});
test.after(() => {
fixture.forEach(fs.unlinkSync.bind(fs));
fixture.forEach(x => fs.unlinkSync(path.join(__dirname, tmp, x)));
fs.rmdirSync(tmp);
});
test('glob - async', async t => {
t.deepEqual(await m('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
});
test('glob - async - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});
test('glob with multiple patterns - async', async t => {
t.deepEqual(await m(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
});
test('respect patterns order - async', async t => {
t.deepEqual(await m(['!*.tmp', 'a.tmp']), ['a.tmp']);
});
test('respect patterns order - sync', t => {
t.deepEqual(m.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});
test('glob - sync', t => {
t.deepEqual(m.sync('*.tmp'), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp']), ['a.tmp', 'b.tmp']);
t.deepEqual(m.sync(['!*.tmp', 'a.tmp']), ['a.tmp']);
});
test('glob - sync - multiple file paths', t => {
t.deepEqual(m.sync(['a.tmp', 'b.tmp']), ['a.tmp', 'b.tmp']);
});
test('return [] for all negative patterns - sync', t => {
t.deepEqual(m.sync(['!a.tmp', '!b.tmp']), []);
});
test('return [] for all negative patterns - async', async t => {
t.deepEqual(await m(['!a.tmp', '!b.tmp']), []);
});
test('cwd option', t => {
process.chdir(tmp);
t.deepEqual(m.sync('*.tmp', {cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(['a.tmp', '*.tmp', '!{c,d,e}.tmp'], {cwd}), ['a.tmp', 'b.tmp']);
process.chdir(cwd);
});
test('don\'t mutate the options object - async', async t => {
await m(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
t.pass();
});
test('don\'t mutate the options object - sync', t => {
m.sync(['*.tmp', '!b.tmp'], Object.freeze({ignore: Object.freeze([])}));
t.pass();
});
test('expose generateGlobTasks', t => {
const tasks = m.generateGlobTasks(['*.tmp', '!b.tmp'], {ignore: ['c.tmp']});
t.is(tasks.length, 1);
t.is(tasks[0].pattern, '*.tmp');
t.deepEqual(tasks[0].options.ignore, ['c.tmp', 'b.tmp']);
});
test('expose hasMagic', t => {
t.true(m.hasMagic('**'));
t.true(m.hasMagic(['**', 'path1', 'path2']));
t.false(m.hasMagic(['path1', 'path2']));
});
test('expandDirectories option', t => {
t.deepEqual(m.sync(tmp), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
t.deepEqual(m.sync('**', {cwd: tmp}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);
t.deepEqual(m.sync(tmp, {expandDirectories: ['a*', 'b*']}), ['tmp/a.tmp', 'tmp/b.tmp']);
t.deepEqual(m.sync(tmp, {
expandDirectories: {
files: ['a', 'b'],
extensions: ['tmp']
}
}), ['tmp/a.tmp', 'tmp/b.tmp']);
t.deepEqual(m.sync(tmp, {
expandDirectories: {
files: ['a', 'b'],
extensions: ['tmp']
},
ignore: ['**/b.tmp']
}), ['tmp/a.tmp']);
});
test('expandDirectories:true and onlyFiles:true option', t => {
t.deepEqual(m.sync(tmp, {onlyFiles: true}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});
test.failing('expandDirectories:true and onlyFiles:false option', t => {
// Node-glob('tmp/**') => ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// Fast-glob('tmp/**') => ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']
// See https://github.com/mrmlnc/fast-glob/issues/47
t.deepEqual(m.sync(tmp, {onlyFiles: false}), ['tmp', 'tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});
test('expandDirectories and ignores option', t => {
t.deepEqual(m.sync('tmp', {
ignore: ['tmp']
}), []);
t.deepEqual(m.sync('tmp/**', {
expandDirectories: false,
ignore: ['tmp']
}), ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']);
});
test.failing('relative paths and ignores option', t => {
process.chdir(tmp);
t.deepEqual(m.sync('../tmp', {
cwd: process.cwd(),
ignore: ['tmp']
}), []);
process.chdir(cwd);
});
// Rejected for being an invalid pattern
[
{},
[{}],
true,
[true],
false,
[false],
null,
[null],
undefined,
[undefined],
NaN,
[NaN],
5,
[5],
function () {},
[function () {}]
].forEach(v => {
const valstring = util.format(v);
const msg = 'Patterns must be a string or an array of strings';
test(`rejects the promise for invalid patterns input: ${valstring} - async`, async t => {
await t.throwsAsync(m(v), TypeError);
await t.throwsAsync(m(v), msg);
});
test(`throws for invalid patterns input: ${valstring}`, t => {
t.throws(() => m.sync(v), TypeError);
t.throws(() => m.sync(v), msg);
});
test(`generateGlobTasks throws for invalid patterns input: ${valstring}`, t => {
t.throws(() => m.generateGlobTasks(v), TypeError);
t.throws(() => m.generateGlobTasks(v), msg);
});
});
test('gitignore option defaults to false', async t => {
const actual = await m('*', {onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});
test('gitignore option defaults to false - sync', t => {
const actual = m.sync('*', {onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});
test('respects gitignore option true', async t => {
const actual = await m('*', {gitignore: true, onlyFiles: false});
t.false(actual.indexOf('node_modules') > -1);
});
test('respects gitignore option true - sync', t => {
const actual = m.sync('*', {gitignore: true, onlyFiles: false});
t.false(actual.indexOf('node_modules') > -1);
});
test('respects gitignore option false', async t => {
const actual = await m('*', {gitignore: false, onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});
test('respects gitignore option false - sync', t => {
const actual = m.sync('*', {gitignore: false, onlyFiles: false});
t.true(actual.indexOf('node_modules') > -1);
});
// https://github.com/sindresorhus/globby/issues/97
test.failing('`{extension: false}` and `expandDirectories.extensions` option', t => {
t.deepEqual(
m.sync(tmp, {
extension: false,
expandDirectories: {
extensions: [
'md',
'tmp'
]
}
}),
[
'a.tmp',
'b.tmp',
'c.tmp',
'd.tmp',
'e.tmp'
]
);
});