From 29056db8469522d9d67f1b6cb333c7262069f4ee Mon Sep 17 00:00:00 2001 From: typicode Date: Tue, 13 Feb 2024 23:58:09 +0100 Subject: [PATCH] fix: husky=0 in init (#1395) * refactor: husky and add tests * style: indent --- husky | 5 +-- test.sh | 6 ++- test/{8_time.sh => 10_time.sh} | 0 test/7_set_u.sh | 15 +++++++ test/8_husky_0.sh | 22 +++++++++++ test/{7_init.sh => 9_init.sh} | 1 + test/functions.sh | 71 +++++++++++++++++----------------- 7 files changed, 80 insertions(+), 40 deletions(-) rename test/{8_time.sh => 10_time.sh} (100%) create mode 100644 test/7_set_u.sh create mode 100644 test/8_husky_0.sh rename test/{7_init.sh => 9_init.sh} (80%) diff --git a/husky b/husky index 8d2aa3f85..86bd0ba5f 100644 --- a/husky +++ b/husky @@ -1,6 +1,5 @@ #!/usr/bin/env sh -H="$HUSKY" -[ "$H" = "2" ] && set -x +[ "$HUSKY" = "2" ] && set -x h="${0##*/}" s="${0%/*/*}/$h" @@ -11,7 +10,7 @@ for f in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc"; do [ -f "$f" ] && . "$f" done -[ "$H" = "0" ] && exit 0 +[ "${HUSKY-}" = "0" ] && exit 0 sh -e "$s" "$@" c=$? diff --git a/test.sh b/test.sh index 6f6d9d406..334b428f9 100755 --- a/test.sh +++ b/test.sh @@ -8,5 +8,7 @@ sh test/3_from-sub-dir.sh sh test/4_not-git-dir.sh sh test/5_git_command_not_found.sh sh test/6_command_not_found.sh -sh test/7_init.sh -# sh test/8_time.sh +sh test/7_set_u.sh +sh test/8_husky_0.sh +sh test/9_init.sh +sh test/10_time.sh diff --git a/test/8_time.sh b/test/10_time.sh similarity index 100% rename from test/8_time.sh rename to test/10_time.sh diff --git a/test/7_set_u.sh b/test/7_set_u.sh new file mode 100644 index 000000000..6767e7a1a --- /dev/null +++ b/test/7_set_u.sh @@ -0,0 +1,15 @@ +#!/bin/sh +. test/functions.sh +setup +install + +npx --no-install husky +expect_hooksPath_to_be ".husky/_" + +git add package.json +echo "echo \"pre-commit\"" >.husky/pre-commit + +# Should not fail if set -u is used +mkdir -p config/husky +echo "set -u" > config/husky/init.sh +XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo" \ No newline at end of file diff --git a/test/8_husky_0.sh b/test/8_husky_0.sh new file mode 100644 index 000000000..03c9fde1c --- /dev/null +++ b/test/8_husky_0.sh @@ -0,0 +1,22 @@ +#!/bin/sh +. test/functions.sh +setup +install + +# Should not setup hooks when HUSKY=0 +HUSKY=0 npx --no-install husky +expect_hooksPath_to_be "" + +# Should setup hooks +npx --no-install husky +expect_hooksPath_to_be ".husky/_" + +# Should not commit +git add package.json +echo "echo \"pre-commit\" && exit 1" >.husky/pre-commit +expect 1 "git commit -m foo" + +# Should commit when HUSKY=0 +mkdir -p config/husky +echo "export HUSKY=0" > config/husky/init.sh +XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo" \ No newline at end of file diff --git a/test/7_init.sh b/test/9_init.sh similarity index 80% rename from test/7_init.sh rename to test/9_init.sh index b01908ca3..8079e733a 100644 --- a/test/7_init.sh +++ b/test/9_init.sh @@ -3,4 +3,5 @@ setup install +# Test init command expect 0 "npx --no-install husky init" diff --git a/test/functions.sh b/test/functions.sh index 41dccb5dc..07052a046 100755 --- a/test/functions.sh +++ b/test/functions.sh @@ -2,54 +2,55 @@ set -eu setup() { - name="$(basename -- "$0")" - testDir="/tmp/husky-test-$name" - echo - echo "-------------------" - echo "+ $name" - echo "-------------------" - echo - - # Create test directory - rm -rf "$testDir" - mkdir -p "$testDir" - cd "$testDir" - - # Init git - git init --quiet - git config user.email "test@test" - git config user.name "test" - - # Init package.json - npm_config_loglevel="error" npm init -y 1>/dev/null + name="$(basename -- "$0")" + testDir="/tmp/husky-test-$name" + echo + echo "-------------------" + echo "+ $name" + echo "-------------------" + echo + + # Create test directory + rm -rf "$testDir" + mkdir -p "$testDir" + cd "$testDir" + + # Init git + git init --quiet + git config user.email "test@test" + git config user.name "test" + + # Init package.json + npm_config_loglevel="error" npm init -y 1>/dev/null } install() { - npm install ../husky.tgz + npm install ../husky.tgz } expect() { - set +e - sh -c "$2" - exitCode="$?" - set -e - if [ $exitCode != "$1" ]; then - error "expect command \"$2\" to exit with code $1 (got $exitCode)" - fi + set +e + sh -c "$2" + exitCode="$?" + set -e + if [ $exitCode != "$1" ]; then + error "expect command \"$2\" to exit with code $1 (got $exitCode)" + fi } expect_hooksPath_to_be() { - hooksPath=$(git config core.hooksPath) - if [ "$hooksPath" != "$1" ]; then - error "core.hooksPath should be $1, was $hooksPath" - fi + set +e + hooksPath=$(git config core.hooksPath) + if [ "$hooksPath" != "$1" ]; then + error "core.hooksPath should be $1, was $hooksPath" + fi } error() { - echo -e "\e[0;31mERROR:\e[m $1" - exit 1 + echo -e "\e[0;31mERROR:\e[m $1" + exit 1 } ok() { - echo -e "\e[0;32mOK\e[m" + echo -e "\e[0;32mOK\e[m" }