diff --git a/lib/find.js b/lib/find.js index e520ce8..34bd310 100644 --- a/lib/find.js +++ b/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 diff --git a/test/find.js b/test/find.js index 3101912..1951cc4 100644 --- a/test/find.js +++ b/test/find.js @@ -1,5 +1,6 @@ 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 => { @@ -7,8 +8,7 @@ t.test('find the git dir many folders up', t => { '.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 => { @@ -16,8 +16,7 @@ t.test('stop before root dir', t => { '.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 => { @@ -25,8 +24,7 @@ t.test('stop at root dir', t => { '.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 => { @@ -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)) + } +})