Skip to content

Commit

Permalink
馃悰 Add try/catch to getAbsoluteHooksPath (#590)
Browse files Browse the repository at this point in the history
This fixes #589
  • Loading branch information
carloscuesta committed Apr 26, 2021
1 parent a065b4c commit 4be3313
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 63 deletions.
26 changes: 14 additions & 12 deletions src/utils/getAbsoluteHooksPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import execa from 'execa'
import path from 'path'

const getAbsoluteHooksPath = async (hookName: string): Promise<string> => {
const { stdout: coreHooksPath } = await execa('git', [
'config',
'--get',
'core.hooksPath'
])
try {
const { stdout: coreHooksPath } = await execa('git', [
'config',
'--get',
'core.hooksPath'
])

const { stdout: gitDirPath } = await execa('git', [
'rev-parse',
'--absolute-git-dir'
])
return path.resolve(coreHooksPath, hookName)
} catch (err) {
const { stdout: gitDirPath } = await execa('git', [
'rev-parse',
'--absolute-git-dir'
])

const hooksPath = coreHooksPath || gitDirPath + '/hooks'

return path.resolve(hooksPath, hookName)
return path.resolve(gitDirPath + '/hooks', hookName)
}
}

export default getAbsoluteHooksPath
64 changes: 13 additions & 51 deletions test/utils/getAbsoluteHooksPath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,77 +6,39 @@ import * as stubs from './stubs'
describe('getAbsoluteHooksPath', () => {
const hookName = 'pre-commit'

describe('when the core path is absolute', () => {
beforeEach(() => {
execa.mockReturnValueOnce({ stdout: stubs.absoluteCoreHooksPath })
execa.mockReturnValueOnce({ stdout: stubs.gitAbsoluteDir })
describe('when core.hooksPath is defined', () => {
beforeAll(() => {
execa.mockReturnValue({ stdout: stubs.absoluteCoreHooksPath })
})

it('returns an absolute path inside it', async () => {
const hookFile = await getAbsoluteHooksPath(hookName)
it('should return core.hooksPath', async () => {
const path = await getAbsoluteHooksPath(hookName)

expect(execa).toHaveBeenCalledWith('git', [
'config',
'--get',
'core.hooksPath'
])
expect(execa).toHaveBeenCalledWith('git', [
'rev-parse',
'--absolute-git-dir'
])
expect(hookFile).toEqual(
expect.stringContaining(stubs.absoluteCoreHooksPath)
)
expect(hookFile).toEqual(expect.stringContaining(hookName))
})
})

describe('when the core path is relative', () => {
beforeEach(() => {
execa.mockReturnValueOnce({ stdout: stubs.relativeCoreHooksPath })
execa.mockReturnValueOnce({ stdout: stubs.gitAbsoluteDir })
})

it('returns an absolute path inside the git directory', async () => {
const hookFile = await getAbsoluteHooksPath(hookName)

expect(execa).toHaveBeenCalledWith('git', [
'config',
'--get',
'core.hooksPath'
])
expect(execa).toHaveBeenCalledWith('git', [
'rev-parse',
'--absolute-git-dir'
])
expect(hookFile).toEqual(
expect.stringContaining(stubs.relativeCoreHooksPath)
)
expect(hookFile).toEqual(expect.stringContaining(hookName))
expect(path).toEqual(`${stubs.absoluteCoreHooksPath}/${hookName}`)
})
})

describe('when the core path is not set in the config', () => {
beforeEach(() => {
execa.mockReturnValueOnce({ stdout: undefined })
describe('when core.hooksPath is not defined', () => {
beforeAll(() => {
execa.mockReturnValueOnce(new Error('core.hooksPath is not defined'))
execa.mockReturnValueOnce({ stdout: stubs.gitAbsoluteDir })
})

it('returns an absolute path inside the git directory', async () => {
const hookFile = await getAbsoluteHooksPath(hookName)
it('should return the default hook path', async () => {
const path = await getAbsoluteHooksPath(hookName)

expect(execa).toHaveBeenCalledWith('git', [
'config',
'--get',
'core.hooksPath'
])
expect(execa).toHaveBeenCalledWith('git', [
'rev-parse',
'--absolute-git-dir'
])
expect(hookFile).toEqual(expect.stringContaining(stubs.gitAbsoluteDir))
expect(hookFile).toEqual(expect.stringContaining('.git/hooks'))
expect(hookFile).toEqual(expect.stringContaining(hookName))

expect(path).toEqual(`${stubs.gitAbsoluteDir}/hooks/${hookName}`)
})
})
})

0 comments on commit 4be3313

Please sign in to comment.