Skip to content

Commit d5a99e3

Browse files
committed
fix: dont default find root option to anything
This ensures that it will only short circuit if a valid path is passed in. This commit also adds one more mocked test to ensure that the system root path is always checked as the last path and no path is checked twice.
1 parent 53fa3bc commit d5a99e3

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

lib/find.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const is = require('./is.js')
2-
const { dirname, sep } = require('path')
2+
const { dirname } = require('path')
33

4-
module.exports = async ({ cwd = process.cwd(), root = sep } = {}) => {
4+
module.exports = async ({ cwd = process.cwd(), root } = {}) => {
55
while (true) {
66
if (await is({ cwd })) {
77
return cwd

test/find.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
const t = require('tap')
2-
const { join } = require('path')
2+
const { join, parse } = require('path')
3+
const { tmpdir } = require('os')
34
const find = require('../lib/find.js')
45

56
t.test('find the git dir many folders up', t => {
67
const root = t.testdir({
78
'.git': { index: 'hello' },
89
a: { b: { c: { d: { e: {} } } } },
910
})
10-
const path = `${root}/a/b/c/d/e`
11-
return t.resolveMatch(find({ cwd: path }), root)
11+
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e') }), root)
1212
})
1313

1414
t.test('stop before root dir', t => {
1515
const root = t.testdir({
1616
'.git': { index: 'hello' },
1717
a: { b: { c: { d: { e: {} } } } },
1818
})
19-
const path = `${root}/a/b/c/d/e`
20-
return t.resolveMatch(find({ cwd: path, root: join(root, 'a') }), null)
19+
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e'), root: join(root, 'a') }), null)
2120
})
2221

2322
t.test('stop at root dir', t => {
2423
const root = t.testdir({
2524
'.git': { index: 'hello' },
2625
a: { b: { c: { d: { e: {} } } } },
2726
})
28-
const path = `${root}/a/b/c/d/e`
29-
return t.resolveMatch(find({ cwd: path, root }), root)
27+
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e'), root }), root)
3028
})
3129

3230
t.test('find the git dir at current level', t => {
@@ -38,13 +36,35 @@ t.test('find the git dir at current level', t => {
3836

3937
t.test('no git dir to find', t => {
4038
// this will fail if your tmpdir is in a git repo, I suppose
41-
const path = require('os').tmpdir()
42-
return t.resolveMatch(find({ cwd: path }), null)
39+
return t.resolveMatch(find({ cwd: tmpdir() }), null)
4340
})
4441

4542
t.test('default to cwd', t => {
46-
// this will fail if your tmpdir is in a git repo, I suppose
47-
const path = require('os').tmpdir()
48-
process.chdir(path)
43+
const dir = process.cwd()
44+
t.teardown(() => process.chdir(dir))
45+
process.chdir(tmpdir())
4946
return t.resolveMatch(find(), null)
5047
})
48+
49+
t.test('mock is', async t => {
50+
const cwd = tmpdir()
51+
const { root } = parse(cwd)
52+
53+
const mockFind = async (t, opts) => {
54+
const seen = []
55+
const mocked = t.mock('../lib/find.js', {
56+
'../lib/is.js': async (o) => {
57+
seen.push(o.cwd)
58+
return false
59+
},
60+
})
61+
const res = await mocked({ cwd, ...opts })
62+
t.strictSame(res, null)
63+
t.strictSame(seen, [...new Set(seen)], 'no directory checked more than once')
64+
t.equal(seen[seen.length - 1], root, 'last dir is root')
65+
}
66+
67+
for (const tCase of [undefined, { root }, { root: 1 }]) {
68+
await t.test(`root: ${JSON.stringify(tCase)}`, t => mockFind(t, tCase))
69+
}
70+
})

0 commit comments

Comments
 (0)