1
1
const t = require ( 'tap' )
2
- const { join } = require ( 'path' )
2
+ const { join, parse } = require ( 'path' )
3
+ const { tmpdir } = require ( 'os' )
3
4
const find = require ( '../lib/find.js' )
4
5
5
6
t . test ( 'find the git dir many folders up' , t => {
6
7
const root = t . testdir ( {
7
8
'.git' : { index : 'hello' } ,
8
9
a : { b : { c : { d : { e : { } } } } } ,
9
10
} )
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 )
12
12
} )
13
13
14
14
t . test ( 'stop before root dir' , t => {
15
15
const root = t . testdir ( {
16
16
'.git' : { index : 'hello' } ,
17
17
a : { b : { c : { d : { e : { } } } } } ,
18
18
} )
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 )
21
20
} )
22
21
23
22
t . test ( 'stop at root dir' , t => {
24
23
const root = t . testdir ( {
25
24
'.git' : { index : 'hello' } ,
26
25
a : { b : { c : { d : { e : { } } } } } ,
27
26
} )
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 )
30
28
} )
31
29
32
30
t . test ( 'find the git dir at current level' , t => {
@@ -38,13 +36,40 @@ t.test('find the git dir at current level', t => {
38
36
39
37
t . test ( 'no git dir to find' , t => {
40
38
// 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 )
43
40
} )
44
41
45
42
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 ( ) )
49
46
return t . resolveMatch ( find ( ) , null )
50
47
} )
48
+
49
+ t . test ( 'mock is' , async t => {
50
+ const mockFind = async ( t , opts ) => {
51
+ const seen = [ ]
52
+ const mocked = t . mock ( '../lib/find.js' , {
53
+ '../lib/is.js' : async ( o ) => {
54
+ seen . push ( o . cwd )
55
+ return false
56
+ } ,
57
+ } )
58
+ const cwd = tmpdir ( )
59
+ const root = parse ( cwd ) . root
60
+ return [ await mocked ( { cwd : tmpdir ( ) , ...opts } ) , seen , root ]
61
+ }
62
+
63
+ await t . test ( 'no git dir to find' , async t => {
64
+ const [ res , seen , root ] = await mockFind ( t )
65
+ t . strictSame ( res , null )
66
+ t . strictSame ( seen , [ ...new Set ( seen ) ] , 'no directory checked more than once' )
67
+ t . equal ( seen [ seen . length - 1 ] , root , 'last dir is root' )
68
+ } )
69
+
70
+ await t . test ( 'root is never found' , async t => {
71
+ const [ res , seen , root ] = await mockFind ( t , { root : 1 } )
72
+ t . strictSame ( res , null )
73
+ t . equal ( seen [ seen . length - 1 ] , root , 'last dir is root' )
74
+ } )
75
+ } )
0 commit comments