@@ -4,6 +4,8 @@ const path = require("path")
4
4
5
5
const { version : packageVersion } = require ( './package.json' ) ;
6
6
7
+ // eslint-disable-next-line no-unused-vars
8
+ const fsMock = jest . mock ( 'fs' )
7
9
8
10
// Get project root directory
9
11
@@ -44,6 +46,34 @@ test('get git root works from any file', () => {
44
46
} )
45
47
46
48
49
+ function mockFsImplementation ( gitdir ) {
50
+ const mocks = [
51
+ jest . spyOn ( fs , 'existsSync' ) . mockImplementation ( ( ) => true ) ,
52
+ jest . spyOn ( fs , 'lstatSync' ) . mockImplementation ( ( ) => ( {
53
+ isDirectory : jest . fn ( ( ) => false )
54
+ } ) ) ,
55
+ jest . spyOn ( fs , 'readFileSync' ) . mockImplementation ( ( ) => gitdir )
56
+ ]
57
+ return ( ) => mocks . forEach ( mock => mock . mockRestore ( ) )
58
+ }
59
+
60
+ const gitRoot = '/home/user/projects/simple-git-hooks/.git' ;
61
+ const workTreePath = '/home/user/projects/worktree1' ;
62
+ const workTreeTestFilePath = '/home/user/projects/worktree1/simple-git-hooks.test.js' ;
63
+
64
+ test ( 'get git root works from worktree directory' , ( ) => {
65
+ const clearMocks = mockFsImplementation ( 'gitdir: /home/user/projects/simple-git-hooks/.git/worktrees/worktree1' )
66
+ expect ( spc . getGitProjectRoot ( workTreePath ) ) . toBe ( gitRoot ) ;
67
+ clearMocks ( ) ;
68
+ } )
69
+
70
+ test ( 'get git root works from any file' , ( ) => {
71
+ const clearMocks = mockFsImplementation ( 'gitdir: /home/user/projects/simple-git-hooks/.git/worktrees/worktree1' )
72
+ expect ( spc . getGitProjectRoot ( workTreeTestFilePath ) ) . toBe ( gitRoot ) ;
73
+ clearMocks ( ) ;
74
+ } )
75
+
76
+
47
77
// Check if simple-pre-commit is in devDependencies or dependencies in package json
48
78
49
79
const correctPackageJsonProjectPath = path . normalize ( path . join ( process . cwd ( ) , '_tests' , 'project_with_simple_pre_commit_in_deps' ) )
0 commit comments