/
test-install.sh
135 lines (103 loc) 路 2.25 KB
/
test-install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Integrations test
# Installs husky in /tmp and makes a few commits
# Exit on error
set -e
# ---
# Variables
# ---
HUSKY_DEBUG=1
projectDir=/tmp/husky-project
hookParamsFile=hook-params
# ---
# Functions
# ---
# Separator function for readability
test() {
echo -e "\e[34m\n---\n$1\n---\n\e[0m"
}
# Commit function
commit() {
touch $1
git add $1
HUSKY_SKIP_HOOKS=$2 git commit -m "$1 msg"
}
# ---
# Setup
# ---
# Reset dir
rm -rf $projectDir && mkdir $projectDir
# Husky needs to be packed to be closer to a real install
# Ensure that install scripts are enabled for the test
npx --no-install pinst --enable
npm run build && npm pack
npx --no-install pinst --disable
# Move husky to project
mv husky-*.tgz $projectDir
# Init a blank git/npm project and install husky
cd $projectDir
git init
git config user.name foo # Needed by AppVeyor
git config user.email foo@example.com # Needed by AppVeyor
npm init -y
# Create .huskyrc with skipCI: false before installing husky
cat > .huskyrc << EOL
{
"skipCI": false,
"hooks": {
"commit-msg": "echo \"commit-msg hook from Husky\" && echo \$HUSKY_GIT_PARAMS > $hookParamsFile"
}
}
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
# ---
# Tests
# ---
test "hook should not run when HUSKY_SKIP_HOOKS=1"
commit first 1
if [ -f $hookParamsFile ]; then
echo "Fail: hooks were not skipped."
exit 1
fi
# ---
test "hook should run and have HUSKY_GIT_PARAMS set"
commit second
if [ ! -f $hookParamsFile ]; then
echo "Fail: hook script didn't run"
exit 1
fi
actual=$(cat $hookParamsFile)
expected=".git/COMMIT_EDITMSG"
if [ "$actual" != "$expected" ]; then
echo "Fail: HUSKY_GIT_PARAMS weren't set correctly"
echo "$actual != $expected"
exit 1
fi
# ---
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"