From d6ecc9d0b02374c9be38c6fe8b8e070f6e823089 Mon Sep 17 00:00:00 2001 From: typicode Date: Thu, 9 Jan 2020 05:07:52 +0100 Subject: [PATCH] fix show error message if hook fails --- scripts/test-install.sh | 28 ++++++++++++++++++++++++++++ src/runner/index.ts | 20 ++++++++------------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/scripts/test-install.sh b/scripts/test-install.sh index e08dca46a..a193366ef 100644 --- a/scripts/test-install.sh +++ b/scripts/test-install.sh @@ -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 @@ -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" diff --git a/src/runner/index.ts b/src/runner/index.ts index 532f45f31..43d6ec7d6 100644 --- a/src/runner/index.ts +++ b/src/runner/index.ts @@ -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', @@ -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 } /**