Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function getGitProjectRoot(directory=process.cwd()) {
if (fs.existsSync(fullPath)) {
if(!fs.lstatSync(fullPath).isDirectory()) {
let content = fs.readFileSync(fullPath, { encoding: 'utf-8' })
let match = /^gitdir: (.*)\s*$/.exec(content)
let match = /^gitdir: (.*\.git)((\/.*)|(\s*))/.exec(content)
if (match) {
return path.normalize(match[1])
}
Expand Down
30 changes: 30 additions & 0 deletions simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const path = require("path")

const { version: packageVersion } = require('./package.json');

// eslint-disable-next-line no-unused-vars
const fsMock = jest.mock('fs')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fsMock needs to be in scope so that mockFsImplementation() can work properly.


// Get project root directory

Expand Down Expand Up @@ -44,6 +46,34 @@ test('get git root works from any file', () => {
})


function mockFsImplementation(gitdir){
const mocks = [
jest.spyOn(fs, 'existsSync').mockImplementation(()=>true),
jest.spyOn(fs, 'lstatSync').mockImplementation(()=>({
isDirectory: jest.fn(()=>false)
})),
jest.spyOn(fs, 'readFileSync').mockImplementation(()=>gitdir)
]
return ()=> mocks.forEach(mock=>mock.mockRestore())
}

const gitRoot = '/home/user/projects/simple-git-hooks/.git';
const workTreePath = '/home/user/projects/worktree1';
const workTreeTestFilePath = '/home/user/projects/worktree1/simple-git-hooks.test.js';

test('get git root works from worktree directory', () => {
const clearMocks = mockFsImplementation('gitdir: /home/user/projects/simple-git-hooks/.git/worktrees/worktree1')
expect(spc.getGitProjectRoot(workTreePath)).toBe(gitRoot);
clearMocks();
})

test('get git root works from any file', () => {
const clearMocks = mockFsImplementation('gitdir: /home/user/projects/simple-git-hooks/.git/worktrees/worktree1')
expect(spc.getGitProjectRoot(workTreeTestFilePath)).toBe(gitRoot);
clearMocks();
})


// Check if simple-pre-commit is in devDependencies or dependencies in package json

const correctPackageJsonProjectPath = path.normalize(path.join(process.cwd(), '_tests', 'project_with_simple_pre_commit_in_deps'))
Expand Down