Skip to content

Commit

Permalink
fix wrong error message (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Apr 9, 2020
1 parent 17c012d commit a403be4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
21 changes: 21 additions & 0 deletions scripts/test-install.sh
Expand Up @@ -131,5 +131,26 @@ if [ "$exitCode" -eq 0 ]; then
exit 1
fi

# ---
test "hook should fail if command not found"
cat > .huskyrc << EOL
{
"skipCI": false,
"hooks": {
"pre-commit": "cmdfoo"
}
}
EOL

set +e
commit fifth
exitCode=$?
set -e

if [ "$exitCode" -eq 0 ]; then
echo "Fail: pre-commit hook should have failed"
exit 1
fi

echo
echo "Success: all tests passed"
22 changes: 21 additions & 1 deletion src/runner/__tests__/index.ts
Expand Up @@ -47,7 +47,7 @@ describe('run', (): void => {
expect(status).toBe(0)
})

it('should return 0 status if the command is undefined', async (): Promise<
it('should return 0 status if no hooks are defined', async (): Promise<
void
> => {
const dir = tempy.directory()
Expand All @@ -64,6 +64,26 @@ describe('run', (): void => {
expect(status).toBe(0)
})

it('should return 1 status if the command is not found in PATH', async (): Promise<
void
> => {
const dir = tempy.directory()

fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-commit': 'cmdfoo'
}
}
})
)

const status = await index(['', '', 'pre-commit'], { cwd: dir })
expect(status).toBe(1)
})

it('should run failing command and return 1 status', async (): Promise<
void
> => {
Expand Down
7 changes: 7 additions & 0 deletions src/runner/index.ts
Expand Up @@ -58,6 +58,13 @@ function runCommand(
console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`)
}

// If shell exits with 127 it means that some command was not found.
// However, if husky has been deleted from node_modules, it'll be a 127 too.
// To be able to distinguish between both cases, 127 is changed to 1.
if (status === 127) {
return 1
}

return status || 0
}

Expand Down

0 comments on commit a403be4

Please sign in to comment.