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 5, 2023
1 parent 1759d1c commit 0b7d3c6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
4 changes: 2 additions & 2 deletions lib/find.js
Original file line number Diff line number Diff line change
@@ -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
49 changes: 37 additions & 12 deletions test/find.js
Original file line number Diff line number Diff line change
@@ -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,40 @@ 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 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 cwd = tmpdir()
const root = parse(cwd).root
return [await mocked({ cwd: tmpdir(), ...opts }), seen, root]
}

await t.test('no git dir to find', async t => {
const [res, seen, root] = await mockFind(t)
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')
})

await t.test('root is never found', async t => {
const [res, seen, root] = await mockFind(t, { root: 1 })
t.strictSame(res, null)
t.equal(seen[seen.length - 1], root, 'last dir is root')
})
})

0 comments on commit 0b7d3c6

Please sign in to comment.