Skip to content

Commit

Permalink
fix: husky=0 in init (#1395)
Browse files Browse the repository at this point in the history
* refactor: husky and add tests

* style: indent
  • Loading branch information
typicode authored Feb 13, 2024
1 parent 095a4fe commit 29056db
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 40 deletions.
5 changes: 2 additions & 3 deletions husky
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env sh
H="$HUSKY"
[ "$H" = "2" ] && set -x
[ "$HUSKY" = "2" ] && set -x
h="${0##*/}"
s="${0%/*/*}/$h"

Expand All @@ -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=$?
Expand Down
6 changes: 4 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes.
15 changes: 15 additions & 0 deletions test/7_set_u.sh
Original file line number Diff line number Diff line change
@@ -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"
22 changes: 22 additions & 0 deletions test/8_husky_0.sh
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions test/7_init.sh → test/9_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
setup
install

# Test init command
expect 0 "npx --no-install husky init"
71 changes: 36 additions & 35 deletions test/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 29056db

Please sign in to comment.