@@ -4,6 +4,8 @@ const path = require("path")
44
55const { version : packageVersion } = require ( './package.json' ) ;
66
7+ // eslint-disable-next-line no-unused-vars
8+ const fsMock = jest . mock ( 'fs' )
79
810// Get project root directory
911
@@ -44,6 +46,34 @@ test('get git root works from any file', () => {
4446} )
4547
4648
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+
4777// Check if simple-pre-commit is in devDependencies or dependencies in package json
4878
4979const correctPackageJsonProjectPath = path . normalize ( path . join ( process . cwd ( ) , '_tests' , 'project_with_simple_pre_commit_in_deps' ) )
0 commit comments