Skip to content

Commit

Permalink
fix show error message if hook fails
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jan 9, 2020
1 parent 3ca7ff4 commit d6ecc9d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
28 changes: 28 additions & 0 deletions scripts/test-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ cat > .huskyrc << EOL
}
}
EOL

# Install husky with npm or yarn
HUSKY_DEBUG=1 npm install husky-*.tgz
# HUSKY_DEBUG=1 yarn add ./husky-*.tgz

# Show hook content
cat .git/hooks/commit-msg
Expand Down Expand Up @@ -105,3 +108,28 @@ test "hook should not fail if husky is not found"

mv node_modules _node_modules
commit third
mv _node_modules node_modules

# ---
test "hook should fail"
cat > .huskyrc << EOL
{
"skipCI": false,
"hooks": {
"pre-commit": "echo \"failing pre-commit\" && exit 1"
}
}
EOL

set +e
commit fourth
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"
20 changes: 8 additions & 12 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ function runCommand(
console.log(`husky > ${hookName} (node ${process.version})`)

const SHELL = process.env.SHELL || 'sh'
try {
const { status } = spawnSync(SHELL, ['-c', cmd], {
cwd,
env: { ...process.env, ...env },
stdio: 'inherit'
})
const { status } = spawnSync(SHELL, ['-c', cmd], {
cwd,
env: { ...process.env, ...env },
stdio: 'inherit'
})

return status || 0
} catch (err) {
if (status !== 0) {
const noVerifyMessage = [
'commit-msg',
'pre-commit',
Expand All @@ -60,11 +58,9 @@ function runCommand(
: '(cannot be bypassed with --no-verify due to Git specs)'

console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`)

console.log({ err })

return 1
}

return status || 0
}

/**
Expand Down

0 comments on commit d6ecc9d

Please sign in to comment.