-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.sh
executable file
·244 lines (180 loc) · 4.54 KB
/
test.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env sh
set -ue
# Unset in case we are running from `npm test`
unset INIT_CWD
# shellcheck disable=SC1090
package_dir="$( cd "$( dirname "$( readlink "$0" || echo "$0" )" )"; pwd -P )"
install="$package_dir/install.sh"
if [ -n "${DEBUG-}" ]
then
test_dir="$PWD/test-env"
rm -rf "$test_dir"
mkdir -p "$test_dir"
else
test_dir="$( mktemp -d )"
trap 'echo "Cleanup"; rm -rf "$test_dir"' EXIT HUP INT QUIT TERM
fi
test_repo_dir="$test_dir/repo"
test_remote_dir="$test_dir/remote"
cd "$package_dir"
CLEAN ()
{
cd "$package_dir"
rm -rf "$test_dir"
}
TEST ()
{
name="$1"
pattern="${2:-.*}"
printf "\n## %s\n\n" "$name"
CLEAN
mkdir -p "$test_repo_dir"
cd "$test_repo_dir"
git init --quiet
git symbolic-ref HEAD refs/heads/main
git config --local user.name "Tester McTestface"
git config --local user.email "[email protected]"
git commit -m 'Root' --allow-empty --quiet
git config --local hooks.verbosity 0
git config --local color.ui false
git config --local hooks.pattern "$pattern"
npm init --yes >/dev/null
mkdir -p "$test_remote_dir"
cd "$test_remote_dir"
git clone --quiet --bare "file://$test_repo_dir/.git" .
cd "$test_repo_dir"
git remote add origin "file://$test_remote_dir"
git fetch origin --quiet
mkdir -p node_modules/.bin/
$install > /dev/null
set -x
}
OK ()
{
set +x
printf "✅\n"
}
ALL_DONE ()
{
printf "🎉\n"
}
commit ()
{
git add "$@"
git commit --quiet -m A "$@" 2>&1
}
push ()
{
git add "$@"
git commit --no-verify --quiet -m A "$@" 2>&1
git push --quiet -u origin main 2>&1
}
is_windows ()
{
uname -sr | grep -Ei '^(cygwin|mingw|msys)' > /dev/null
}
tee_stderr ()
{
if is_windows
then
# on windows we can check for /dev/stderr and it exists, but running
# the following command will fail with:
# tee: /dev/stderr: No such file or directory
# so `cat` is here as a fallback
cat
else
tee -a /dev/stderr
fi
}
assert_fail ()
{
# shellcheck disable=SC1111
tee_stderr | grep -q "“$1” hook failed"
}
set_npm_test ()
{
cmd="$1"
printf '{"scripts": {"test": "%s"}}' "$cmd" > package.json
git add package.json
git commit --no-verify -m package.json
}
TEST "Install"
test -x .git/hooks/pre-commit
test -x .git/hooks/pre-push
OK
TEST "Re–install"
$install
test -x .git/hooks/pre-commit
test -x .git/hooks/pre-push
OK
TEST "pre-commit: git-check passing" "git-check"
printf 'This has no white space at the end of the line:' > foo
commit foo
OK
TEST "pre-commit: git-check failing" "git-check"
printf 'This has white space at the end of the line: ' > foo
commit foo | assert_fail 'git-check'
OK
TEST "pre-commit: git-check temporary disable command" "git-check"
printf 'This has white space at the end of the line: ' > foo
commit foo | tee_stderr | grep -qE ' -c hooks.git-check=false '
OK
TEST "pre-commit: non-ascii-filenames passing" "non-ascii-filenames"
: > foo
commit foo
OK
TEST "pre-commit: non-ascii-filenames failing" "non-ascii-filenames"
: > 'żółć'
commit 'żółć' | assert_fail 'non-ascii-filenames'
OK
TEST "pre-push: wip-code" "wip-code"
printf "// WIP\n" > index.js
push index.js | assert_fail 'wip-code'
OK
TEST "pre-push: wip-code, possible false–positive" "wip-code"
printf "// THWIP\n" > index.js
push index.js
OK
TEST "pre-push: npm-test" "npm-test"
set_npm_test 'true'
: > foo.js
push foo.js
OK
TEST "Run commands from node_modules" "lint-eslint"
printf '#!/usr/bin/env sh\necho OK > ./eslint-called\n' > node_modules/.bin/eslint
chmod +x node_modules/.bin/eslint
: > foo.js
commit foo.js
test -e ./eslint-called
OK
TEST "pre-push: npm-test: jest" "npm-test"
(
mkdir -p "$test_dir/jest"
cd "$test_dir/jest/"
npm init --yes >/dev/null
printf '#!/usr/bin/env sh\necho "$@" > ./jest-args\n' > ./jest
chmod +x ./jest
sed -i -e 's~{~{"bin": "jest",~' package.json
)
npm install --save-dev "$test_dir/jest"
set_npm_test "jest"
: > foo.js
push foo.js
grep ' --findRelatedTests.*foo.js' ./jest-args
OK
TEST "pre-push: npm-test: worktree" "npm-test"
set_npm_test 'true'
git config --local hooks.tmpWorkspace true
git status --porcelain > "$test_dir/status"
: > foo.js
push foo.js
# Check for any unexpected artifacts
git status --porcelain | diff --report-identical-files "$test_dir/status" -
OK
TEST "Disablig all hooks" "git-check"
git config hooks.enabled false
printf 'This has white space at the end of the line: ' > foo
commit foo
OK
CLEAN
ALL_DONE