Skip to content

Commit

Permalink
refactor: husky and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Feb 13, 2024
1 parent 095a4fe commit cd4d65a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
5 changes: 2 additions & 3 deletions husky
@@ -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
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
@@ -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
@@ -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
Expand Up @@ -3,4 +3,5 @@
setup
install

# Test init command
expect 0 "npx --no-install husky init"
1 change: 1 addition & 0 deletions test/functions.sh
Expand Up @@ -39,6 +39,7 @@ expect() {
}

expect_hooksPath_to_be() {
set +e
hooksPath=$(git config core.hooksPath)
if [ "$hooksPath" != "$1" ]; then
error "core.hooksPath should be $1, was $hooksPath"
Expand Down

0 comments on commit cd4d65a

Please sign in to comment.