Skip to content

Commit 29056db

Browse files
authored
fix: husky=0 in init (#1395)
* refactor: husky and add tests * style: indent
1 parent 095a4fe commit 29056db

File tree

7 files changed

+80
-40
lines changed

7 files changed

+80
-40
lines changed

husky

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env sh
2-
H="$HUSKY"
3-
[ "$H" = "2" ] && set -x
2+
[ "$HUSKY" = "2" ] && set -x
43
h="${0##*/}"
54
s="${0%/*/*}/$h"
65

@@ -11,7 +10,7 @@ for f in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc"; do
1110
[ -f "$f" ] && . "$f"
1211
done
1312

14-
[ "$H" = "0" ] && exit 0
13+
[ "${HUSKY-}" = "0" ] && exit 0
1514

1615
sh -e "$s" "$@"
1716
c=$?

test.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ sh test/3_from-sub-dir.sh
88
sh test/4_not-git-dir.sh
99
sh test/5_git_command_not_found.sh
1010
sh test/6_command_not_found.sh
11-
sh test/7_init.sh
12-
# sh test/8_time.sh
11+
sh test/7_set_u.sh
12+
sh test/8_husky_0.sh
13+
sh test/9_init.sh
14+
sh test/10_time.sh
File renamed without changes.

test/7_set_u.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
. test/functions.sh
3+
setup
4+
install
5+
6+
npx --no-install husky
7+
expect_hooksPath_to_be ".husky/_"
8+
9+
git add package.json
10+
echo "echo \"pre-commit\"" >.husky/pre-commit
11+
12+
# Should not fail if set -u is used
13+
mkdir -p config/husky
14+
echo "set -u" > config/husky/init.sh
15+
XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo"

test/8_husky_0.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
. test/functions.sh
3+
setup
4+
install
5+
6+
# Should not setup hooks when HUSKY=0
7+
HUSKY=0 npx --no-install husky
8+
expect_hooksPath_to_be ""
9+
10+
# Should setup hooks
11+
npx --no-install husky
12+
expect_hooksPath_to_be ".husky/_"
13+
14+
# Should not commit
15+
git add package.json
16+
echo "echo \"pre-commit\" && exit 1" >.husky/pre-commit
17+
expect 1 "git commit -m foo"
18+
19+
# Should commit when HUSKY=0
20+
mkdir -p config/husky
21+
echo "export HUSKY=0" > config/husky/init.sh
22+
XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo"

test/7_init.sh renamed to test/9_init.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
setup
44
install
55

6+
# Test init command
67
expect 0 "npx --no-install husky init"

test/functions.sh

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,55 @@
22
set -eu
33

44
setup() {
5-
name="$(basename -- "$0")"
6-
testDir="/tmp/husky-test-$name"
7-
echo
8-
echo "-------------------"
9-
echo "+ $name"
10-
echo "-------------------"
11-
echo
12-
13-
# Create test directory
14-
rm -rf "$testDir"
15-
mkdir -p "$testDir"
16-
cd "$testDir"
17-
18-
# Init git
19-
git init --quiet
20-
git config user.email "test@test"
21-
git config user.name "test"
22-
23-
# Init package.json
24-
npm_config_loglevel="error" npm init -y 1>/dev/null
5+
name="$(basename -- "$0")"
6+
testDir="/tmp/husky-test-$name"
7+
echo
8+
echo "-------------------"
9+
echo "+ $name"
10+
echo "-------------------"
11+
echo
12+
13+
# Create test directory
14+
rm -rf "$testDir"
15+
mkdir -p "$testDir"
16+
cd "$testDir"
17+
18+
# Init git
19+
git init --quiet
20+
git config user.email "test@test"
21+
git config user.name "test"
22+
23+
# Init package.json
24+
npm_config_loglevel="error" npm init -y 1>/dev/null
2525
}
2626

2727
install() {
28-
npm install ../husky.tgz
28+
npm install ../husky.tgz
2929
}
3030

3131
expect() {
32-
set +e
33-
sh -c "$2"
34-
exitCode="$?"
35-
set -e
36-
if [ $exitCode != "$1" ]; then
37-
error "expect command \"$2\" to exit with code $1 (got $exitCode)"
38-
fi
32+
set +e
33+
sh -c "$2"
34+
exitCode="$?"
35+
set -e
36+
if [ $exitCode != "$1" ]; then
37+
error "expect command \"$2\" to exit with code $1 (got $exitCode)"
38+
fi
3939
}
4040

4141
expect_hooksPath_to_be() {
42-
hooksPath=$(git config core.hooksPath)
43-
if [ "$hooksPath" != "$1" ]; then
44-
error "core.hooksPath should be $1, was $hooksPath"
45-
fi
42+
set +e
43+
hooksPath=$(git config core.hooksPath)
44+
if [ "$hooksPath" != "$1" ]; then
45+
error "core.hooksPath should be $1, was $hooksPath"
46+
fi
4647
}
4748

4849
error() {
49-
echo -e "\e[0;31mERROR:\e[m $1"
50-
exit 1
50+
echo -e "\e[0;31mERROR:\e[m $1"
51+
exit 1
5152
}
5253

5354
ok() {
54-
echo -e "\e[0;32mOK\e[m"
55+
echo -e "\e[0;32mOK\e[m"
5556
}

0 commit comments

Comments
 (0)