Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix wrong error message #709

Merged
merged 1 commit into from Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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