forked from avajs/ava
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobs.js
182 lines (152 loc) · 4.72 KB
/
globs.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
'use strict';
const path = require('path');
const tap = require('tap');
const globs = require('../lib/globs');
const {test} = tap;
tap.afterEach(done => {
// We changed the CWD in some of the tests
process.chdir(path.resolve(__dirname, '..'));
done();
});
function fixture(...args) {
args.unshift(__dirname, 'fixture', 'globs');
return path.join(...args);
}
test('ignores relativeness in patterns', t => {
const {testPatterns} = globs.normalizeGlobs(['./foo.js', '!./bar'], undefined, ['js']);
t.deepEqual(testPatterns, ['foo.js', '!bar']);
t.end();
});
test('isTest', t => {
const options = globs.normalizeGlobs(
['**/foo*.js', '**/foo*/**/*.js', '!**/fixtures', '!**/helpers'],
undefined,
['js']
);
function isTest(file) {
t.true(globs.isTest(fixture(file), options), `${file} should be a test`);
}
function notTest(file) {
t.false(globs.isTest(fixture(file), options), `${file} should not be a test`);
}
isTest('foo-bar.js');
isTest('foo.js');
isTest('foo/blah.js');
isTest('bar/foo.js');
isTest('bar/foo-bar/baz/buz.js');
notTest('bar/baz/buz.js');
notTest('bar.js');
notTest('bar/bar.js');
notTest('_foo-bar.js');
notTest('foo/_foo-bar.js');
notTest('foo-bar.txt');
notTest('node_modules/foo.js');
notTest('fixtures/foo.js');
notTest('helpers/foo.js');
t.end();
});
test('isSource with defaults', t => {
const options = globs.normalizeGlobs(undefined, undefined, ['js']);
function isSource(file) {
t.true(globs.isSource(file, options), `${file} should be a source`);
}
function notSource(file) {
t.false(globs.isSource(file, options), `${file} should not be a source`);
}
isSource('foo-bar.js');
isSource('foo.js');
isSource('foo/blah.js');
isSource('bar/foo.js');
isSource('_foo-bar.js');
isSource('foo/_foo-bar.js');
isSource('fixtures/foo.js');
isSource('helpers/foo.js');
isSource('snapshots/foo.js.snap');
isSource('snapshots/bar.js.snap');
// TODO: Watcher should probably track any required file that matches the source pattern and has a require extension installed for the given extension.
notSource('foo-bar.json');
notSource('foo-bar.coffee');
// These seem OK
isSource('bar.js');
isSource('bar/bar.js');
notSource('node_modules/foo.js');
t.end();
});
test('isSource with negation negation patterns', t => {
const options = globs.normalizeGlobs(
['**/foo*'],
['!**/bar*'],
['js']
);
t.false(globs.isSource('node_modules/foo/foo.js', options));
t.false(globs.isSource('bar.js', options));
t.false(globs.isSource('foo/bar.js', options));
t.end();
});
test('findHelpersAndTests finds tests (just .js)', async t => {
const fixtureDir = fixture('default-patterns');
process.chdir(fixtureDir);
const expected = [
'sub/directory/__tests__/foo.js',
'sub/directory/bar.spec.js',
'sub/directory/bar.test.js',
'test-foo.js',
'test.js',
'test/baz.js',
'test/deep/deep.js'
].map(file => path.join(fixtureDir, file)).sort();
const {tests: actual} = await globs.findHelpersAndTests({
cwd: fixtureDir,
...globs.normalizeGlobs(['!**/fixtures/*.*', '!**/helpers/*.*'], undefined, ['js'])
});
actual.sort();
t.deepEqual(actual, expected);
});
test('findHelpersAndTests finds tests (.js, .jsx)', async t => {
const fixtureDir = fixture('custom-extension');
process.chdir(fixtureDir);
const expected = [
'test/do-not-compile.js',
'test/foo.jsx',
'test/sub/bar.jsx'
].sort().map(file => path.join(fixtureDir, file));
const {tests: actual} = await globs.findHelpersAndTests({
cwd: fixtureDir,
...globs.normalizeGlobs(['!**/fixtures/*.*', '!**/helpers/*.*'], undefined, ['js', 'jsx'])
});
actual.sort();
t.deepEqual(actual, expected);
});
test('findHelpersAndTests finds helpers (just .js)', async t => {
const fixtureDir = fixture('default-patterns');
process.chdir(fixtureDir);
// TODO: Support pattern to match helpers directories.
const expected = [
// 'sub/directory/__tests__/helpers/foo.js',
'sub/directory/__tests__/_foo.js',
// 'test/helpers/test.js',
'test/_foo-help.js'
].sort().map(file => path.join(fixtureDir, file));
const {helpers: actual} = await globs.findHelpersAndTests({
cwd: fixtureDir,
...globs.normalizeGlobs(undefined, undefined, ['js'])
});
actual.sort();
t.deepEqual(actual, expected);
});
test('findHelpersAndTests finds helpers (.js and .jsx)', async t => {
const fixtureDir = fixture('custom-extension');
process.chdir(fixtureDir);
// TODO: Support pattern to match helpers directories.
const expected = [
'test/sub/_helper.jsx'
// 'test/helpers/a.jsx',
// 'test/helpers/b.js'
].sort().map(file => path.join(fixtureDir, file));
const {helpers: actual} = await globs.findHelpersAndTests({
cwd: fixtureDir,
...globs.normalizeGlobs(undefined, undefined, ['js', 'jsx'])
});
actual.sort();
t.deepEqual(actual, expected);
});