Skip to content

Commit

Permalink
fix: dont default find root option to anything
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukekarrys committed Jun 6, 2023
1 parent 53fa3bc commit d5a99e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/find.js
@@ -1,7 +1,7 @@
const is = require('./is.js')
const { dirname, sep } = require('path')
const { dirname } = require('path')

module.exports = async ({ cwd = process.cwd(), root = sep } = {}) => {
module.exports = async ({ cwd = process.cwd(), root } = {}) => {
while (true) {
if (await is({ cwd })) {
return cwd
Expand Down
44 changes: 32 additions & 12 deletions test/find.js
@@ -1,32 +1,30 @@
const t = require('tap')
const { join } = require('path')
const { join, parse } = require('path')
const { tmpdir } = require('os')
const find = require('../lib/find.js')

t.test('find the git dir many folders up', t => {
const root = t.testdir({
'.git': { index: 'hello' },
a: { b: { c: { d: { e: {} } } } },
})
const path = `${root}/a/b/c/d/e`
return t.resolveMatch(find({ cwd: path }), root)
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e') }), root)
})

t.test('stop before root dir', t => {
const root = t.testdir({
'.git': { index: 'hello' },
a: { b: { c: { d: { e: {} } } } },
})
const path = `${root}/a/b/c/d/e`
return t.resolveMatch(find({ cwd: path, root: join(root, 'a') }), null)
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e'), root: join(root, 'a') }), null)
})

t.test('stop at root dir', t => {
const root = t.testdir({
'.git': { index: 'hello' },
a: { b: { c: { d: { e: {} } } } },
})
const path = `${root}/a/b/c/d/e`
return t.resolveMatch(find({ cwd: path, root }), root)
return t.resolveMatch(find({ cwd: join(root, 'a/b/c/d/e'), root }), root)
})

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

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

t.test('default to cwd', t => {
// this will fail if your tmpdir is in a git repo, I suppose
const path = require('os').tmpdir()
process.chdir(path)
const dir = process.cwd()
t.teardown(() => process.chdir(dir))
process.chdir(tmpdir())
return t.resolveMatch(find(), null)
})

t.test('mock is', async t => {
const cwd = tmpdir()
const { root } = parse(cwd)

const mockFind = async (t, opts) => {
const seen = []
const mocked = t.mock('../lib/find.js', {
'../lib/is.js': async (o) => {
seen.push(o.cwd)
return false
},
})
const res = await mocked({ cwd, ...opts })
t.strictSame(res, null)
t.strictSame(seen, [...new Set(seen)], 'no directory checked more than once')
t.equal(seen[seen.length - 1], root, 'last dir is root')
}

for (const tCase of [undefined, { root }, { root: 1 }]) {
await t.test(`root: ${JSON.stringify(tCase)}`, t => mockFind(t, tCase))
}
})

0 comments on commit d5a99e3

Please sign in to comment.