-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
test.js
338 lines (261 loc) · 6.79 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import fs from 'fs';
import path from 'path';
import test from 'ava';
import isPathInside from 'is-path-inside';
import pify from 'pify';
import tempy from 'tempy';
import m from '.';
const name = {
pkgDir: 'find-up',
pkg: 'package.json',
fixtureDir: 'fixture',
baz: 'baz.js',
qux: 'qux.js'
};
// These paths are relative to the project root
const rel = {
fixtureDir: name.fixtureDir
};
rel.baz = path.join(rel.fixtureDir, name.baz);
rel.qux = path.join(rel.fixtureDir, name.qux);
rel.barDir = path.join(rel.fixtureDir, 'foo', 'bar');
const abs = {
pkgDir: __dirname
};
abs.pkg = path.join(abs.pkgDir, name.pkg);
abs.fixtureDir = path.join(abs.pkgDir, name.fixtureDir);
abs.baz = path.join(abs.fixtureDir, name.baz);
abs.qux = path.join(abs.fixtureDir, name.qux);
abs.barDir = path.join(abs.fixtureDir, 'foo', 'bar');
// Create a disjoint directory, used for the not-found tests
test.beforeEach(t => {
const tmpDir = tempy.directory();
t.context.disjoint = tmpDir;
});
test.afterEach(t => {
fs.rmdirSync(t.context.disjoint);
});
test('async (child file)', async t => {
const filePath = await m(name.pkg);
t.is(filePath, abs.pkg);
});
test('sync (child file)', t => {
const filePath = m.sync(name.pkg);
t.is(filePath, abs.pkg);
});
test('async (child dir)', async t => {
const filePath = await m(name.fixtureDir);
t.is(filePath, abs.fixtureDir);
});
test('sync (child dir)', t => {
const filePath = m.sync(name.fixtureDir);
t.is(filePath, abs.fixtureDir);
});
test('async (child file, custom cwd)', async t => {
const filePath = await m(name.baz, {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('sync (child file, custom cwd)', t => {
const filePath = m.sync(name.baz, {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('async (child file, array, custom cwd)', async t => {
const filePath = await m([name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('sync (child file, array, custom cwd)', t => {
const filePath = m.sync([name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('async (first child file, array, custom cwd)', async t => {
const filePath = await m([name.qux, name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.qux);
});
test('sync (first child file, array, custom cwd)', t => {
const filePath = m.sync([name.qux, name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.qux);
});
test('async (second child file, array, custom cwd)', async t => {
const filePath = await m(['fake', name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('sync (second child file, array, custom cwd)', t => {
const filePath = m.sync(['fake', name.baz], {
cwd: rel.fixtureDir
});
t.is(filePath, abs.baz);
});
test('async (cwd)', async t => {
const filePath = await m(name.pkgDir, {
cwd: abs.pkgDir
});
t.is(filePath, abs.pkgDir);
});
test('sync (cwd)', t => {
const filePath = m.sync(name.pkgDir, {
cwd: abs.pkgDir
});
t.is(filePath, abs.pkgDir);
});
test('async (cousin file, custom cwd)', async t => {
const filePath = await m(name.baz, {
cwd: rel.barDir
});
t.is(filePath, abs.baz);
});
test('sync (cousin file, custom cwd)', t => {
const filePath = m.sync(name.baz, {
cwd: rel.barDir
});
t.is(filePath, abs.baz);
});
test('async (nested descendant file)', async t => {
const filePath = await m(rel.baz);
t.is(filePath, abs.baz);
});
test('sync (nested descendant file)', t => {
const filePath = m.sync(rel.baz);
t.is(filePath, abs.baz);
});
test('async (nested descendant dir)', async t => {
const filePath = await m(rel.barDir);
t.is(filePath, abs.barDir);
});
test('sync (nested descendant dir)', t => {
const filePath = m.sync(rel.barDir);
t.is(filePath, abs.barDir);
});
test('async (nested cousin dir, custom cwd)', async t => {
const filePath = await m(rel.barDir, {
cwd: rel.fixtureDir
});
t.is(filePath, abs.barDir);
});
test('sync (nested cousin dir, custom cwd)', t => {
const filePath = m.sync(rel.barDir, {
cwd: rel.fixtureDir
});
t.is(filePath, abs.barDir);
});
test('async (ancestor dir, custom cwd)', async t => {
const filePath = await m(name.fixtureDir, {
cwd: rel.barDir
});
t.is(filePath, abs.fixtureDir);
});
test('sync (ancestor dir, custom cwd)', t => {
const filePath = m.sync(name.fixtureDir, {
cwd: rel.barDir
});
t.is(filePath, abs.fixtureDir);
});
test('async (not found)', async t => {
const filePath = await m('somenonexistentfile.js');
t.is(filePath, null);
});
test('sync (not found)', t => {
const filePath = m.sync('somenonexistentfile.js');
t.is(filePath, null);
});
// Both tests start in a disjoint directory. `package.json` should not be found
// and `null` should be returned.
test('async (not found, custom cwd)', async t => {
const filePath = await m(name.pkg, {
cwd: t.context.disjoint
});
t.is(filePath, null);
});
test('sync (not found, custom cwd)', t => {
const filePath = m.sync(name.pkg, {
cwd: t.context.disjoint
});
t.is(filePath, null);
});
test('async (matcher function)', async t => {
const cwd = process.cwd();
t.is(await m(dir => {
t.is(dir, cwd);
return dir;
}), cwd);
t.is(await m(() => {
return '.';
}), cwd);
t.is(await m(async () => {
return 'foo.txt';
}), path.join(cwd, 'foo.txt'));
t.is(await m(() => {
return '..';
}), path.join(cwd, '..'));
t.is(await m(dir => {
return (dir !== cwd) && dir;
}), path.join(cwd, '..'));
t.is(await m(dir => {
return (dir !== cwd) && 'foo.txt';
}), path.join(cwd, '..', 'foo.txt'));
});
test('async (not found, matcher function)', async t => {
const cwd = process.cwd();
const {root} = path.parse(cwd);
const visited = new Set();
t.is(await m(async dir => {
t.is(typeof dir, 'string');
const stat = await pify(fs.stat)(dir);
t.true(stat.isDirectory());
t.true((dir === cwd) || isPathInside(cwd, dir));
t.false(visited.has(dir));
visited.add(dir);
}), null);
t.true(visited.has(cwd));
t.true(visited.has(root));
});
test('sync (matcher function)', t => {
const cwd = process.cwd();
t.is(m.sync(dir => {
t.is(dir, cwd);
return dir;
}), cwd);
t.is(m.sync(() => {
return '.';
}), cwd);
t.is(m.sync(() => {
return 'foo.txt';
}), path.join(cwd, 'foo.txt'));
t.is(m.sync(() => {
return '..';
}), path.join(cwd, '..'));
t.is(m.sync(dir => {
return (dir !== cwd) && dir;
}), path.join(cwd, '..'));
t.is(m.sync(dir => {
return (dir !== cwd) && 'foo.txt';
}), path.join(cwd, '..', 'foo.txt'));
});
test('sync (not found, matcher function)', t => {
const cwd = process.cwd();
const {root} = path.parse(cwd);
const visited = new Set();
t.is(m.sync(dir => {
t.is(typeof dir, 'string');
const stat = fs.statSync(dir);
t.true(stat.isDirectory());
t.true((dir === cwd) || isPathInside(cwd, dir));
t.false(visited.has(dir));
visited.add(dir);
}), null);
t.true(visited.has(cwd));
t.true(visited.has(root));
});