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