-
-
Notifications
You must be signed in to change notification settings - Fork 127
/
test.js
140 lines (120 loc) · 3.57 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
import fs from 'fs';
import path from 'path';
import test from 'ava';
import m from '.';
const cwd = process.cwd();
const fixture = [
'a.tmp',
'b.tmp',
'c.tmp',
'd.tmp',
'e.tmp'
];
test.before(() => {
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 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('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('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].opts.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('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']);
});
// Rejected for being an invalid pattern
[
{},
[{}],
true,
[true],
false,
[false],
null,
[null],
undefined,
[undefined],
NaN,
[NaN],
5,
[5],
function () {},
[function () {}]
].forEach(v => {
const valstring = v === undefined ?
'undefined' :
(JSON.stringify(v) || v.toString());
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.throws(m(v), TypeError);
await t.throws(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);
});
});