-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tests.sh
executable file
·435 lines (299 loc) · 10.5 KB
/
run-tests.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env sh
#################################################
# shellcheck shell=sh enable=all disable=SC2230 #
#################################################
[ -z "${SHDEBUG}" ] || set -vx
runquiet() {
"${@}" >/dev/null 2>&1
}
safecd() {
runquiet cd -- "${1}" || {
printf -- "Fatal: unable to change directory to %s\n" "${1}" >&2
exit 1
}
}
[ -z "${0%"${0##*/}"}" ] || safecd "${0%"${0##*/}"}"
###############################
# shellcheck source=/dev/null #
###############################
. ../src/which.extended.sh
for command in [ cat chmod cp curl ed envsubst git grep ln mkdir printf pwd rm tar umask uname xz; do
which -s "${command}" || notlocatedlist="${notlocatedlist} ${command}\n"
done
if [ -n "${notlocatedlist}" ]; then
printf "Tests runner requires %s command(s) and/or builtin(s) to operate properly\n" next >&2
printf "%b\n" "${notlocatedlist}" >&2
printf "Please install %s command(s) and relaunch.\n" required >&2
exit 1
fi
scriptDir="$(pwd -P 2>/dev/null)"
umask 022
[ -d bin ] || runquiet mkdir -m755 bin
safecd bin
if [ ! -x shellspec ]; then
printf "%s command is required to run tests.\n" shellspec >&2
printf -- "Fetching and installing %s command to %s…\n" shellspec "${scriptDir}/bin" >&2
if runquiet git clone https://github.com/shellspec/shellspec.git shellspec.command; then
runquiet ln -s shellspec.command/shellspec shellspec
runquiet ed shellspec.command/lib/general.sh <<EOF
3i
[ -z "\${YASH_VERSION:-}" ] || { set +o | grep "+o posix" >/dev/null 2>&1 || unset YASH_VERSION; }
.
w
q
EOF
printf -- "%s command has been installed to %s.\n" shellspec "${scriptDir}/bin" >&2
else
safecd ..
runquiet rm -rf bin
printf -- "Fatal: unable to fetch and install %s command to %s\n" shellspec "${scriptDir}/bin" >&2
exit 1
fi
fi
os="$(uname -s)"
if [ ! -x shellcheck ]; then
if [ "${os}" = Darwin ] || [ "${os}" = Linux ]; then
[ "${os}" = Darwin ] && oslo=darwin || oslo=linux
arch="$(uname -m)"
if [ "${arch}" = x86_64 ] || [ "${arch}" = aarch64 ]; then
printf "%s command is required to run tests.\n" shellcheck >&2
printf -- "Fetching and installing %s command to %s…\n" shellcheck "${scriptDir}/bin" >&2
shellcheckRemote="https://github.com/koalaman/shellcheck/releases/download/stable/shellcheck-stable.${oslo}.${arch}.tar.xz"
runquiet mkdir -m755 shellcheck.command
safecd shellcheck.command
if curl -fsSL "${shellcheckRemote}" | xz -d - | tar --strip-components=1 -xf -; then
safecd ..
runquiet ln -s shellcheck.command/shellcheck shellcheck
printf -- "%s command has been installed to %s.\n" shellcheck "${scriptDir}/bin" >&2
else
safecd ..
runquiet rm -rf shellcheck.command
printf -- "Unable to fetch and install %s command to %s\n" shellcheck "${scriptDir}/bin" >&2
fi
fi
fi
if [ ! -x shellcheck ]; then
if which -is shellcheck; then
shellcheck="$(which -i shellcheck)"
runquiet ln -s "${shellcheck}" shellcheck
printf -- "Utilizing %s command installed locally at %s.\n" shellcheck "${shellcheck}" >&2
else
cat <<EOF >shellcheck
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
EOF
runquiet chmod 755 shellcheck
fi
fi
fi
sh=busybox
shell="$(which -i "${sh}")"
if [ -n "${shell}" ]; then
[ -x "${sh}" ] || {
cat <<EOF >"${sh}"
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
[ "\${1}" = ash ] || [ "\${1}" = sh ] || set -- sh "\${@}"
exec ${shell} "\${@}"
EOF
runquiet chmod 755 "${sh}"
}
[ -d "sh.${sh}" ] || runquiet mkdir -m755 "sh.${sh}"
safecd "sh.${sh}"
[ -x sh ] || runquiet ln -s "${shell}" sh
safecd ..
fi
shells="ash bash dash gwsh ksh mksh loksh oksh pdksh posh yash zsh"
for sh in ${shells}; do
shell="$(which -i "${sh}")"
if [ -n "${shell}" ]; then
[ "${sh}" = loksh ] || [ "${sh}" = pdksh ] || {
test "${sh}" = posh -a "${os}" = Darwin || test "${sh}" = oksh -a "${os}" != Darwin || {
[ -x "${sh}" ] || runquiet ln -s "${shell}" "${sh}"
[ -d "sh.${sh}" ] || runquiet mkdir -m755 "sh.${sh}"
[ -x "sh.${sh}/sh" ] || {
safecd "sh.${sh}"
runquiet ln -s "../${sh}" sh
safecd ..
}
}
}
fi
done
if [ ! -x ash ] && [ -x dash ]; then
runquiet ln -s dash ash
runquiet mkdir -m755 sh.ash
safecd sh.ash
runquiet ln -s ../ash sh
safecd ..
fi
#############################
# shellcheck disable=SC2209 #
#############################
command=sh
[ -x "${command}" ] || {
binary="$(which -i "${command}")"
runquiet ln -s "${binary}" "${command}"
}
command=zsh
if [ -x "${command}" ]; then
shell="$(which -i "${command}")"
safecd "sh.${command}"
runquiet rm -f sh
runquiet ln -s "${shell}" sh
safecd ..
runquiet rm -f "${command}"
cat <<EOF >"${command}"
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
exec ${shell} --emulate ksh "\${@}"
EOF
runquiet chmod 755 "${command}"
fi
command=which
[ -x "${command}" ] || {
cat <<EOF >"${command}"
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
[ -z "\${0%"\${0##*/}"}" ] || {
cd "\${0%"\${0##*/}"}" || exit
}
###############################
# shellcheck source=/dev/null #
###############################
. ../../src/which.extended.sh
#############################
# shellcheck disable=SC2230 #
#############################
which "\${@}"
EOF
runquiet chmod 755 "${command}"
}
#############################
# shellcheck disable=SC2209 #
#############################
command=printf
[ -x "${command}" ] || {
binary="$(which -i "${command}")"
[ -z "${binary}" ] || runquiet ln -s "${binary}" "${command}"
[ -x "${command}" ] || {
cat <<EOF >"${command}"
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
#############################
# shellcheck disable=SC2059 #
#############################
printf -- "\${@}"
EOF
runquiet chmod 755 "${command}"
}
}
command=[
[ -x "${command}" ] || {
binary="$(which -i "${command}")"
[ -n "${binary}" ] || binary="$(which -i test)"
[ -z "${binary}" ] || runquiet ln -s "${binary}" "${command}"
[ -x "${command}" ] || {
cat <<EOF >"${command}"
#!/usr/bin/env sh
##################################
# shellcheck shell=sh enable=all #
##################################
[ -z "\${SHDEBUG}" ] || set -vx
for arg; do
test "\${arg}" = ] || args="\${args} \${arg}"
done
#############################
# shellcheck disable=SC2086 #
#############################
test \${args}
EOF
runquiet chmod 755 "${command}"
}
}
safecd ..
printf "Preparing %s tests…\n" shellspec >&2
[ -d tests/shellspec ] || runquiet mkdir -m755 tests/shellspec
[ -r tests/shellspec/001-shellcheck.sh ] || runquiet cp tests/templates/shellcheck.sh tests/shellspec/001-shellcheck.sh
testsbin="${scriptDir}/bin"
export testsbin shellbintitle shellbin shebang extrapath shellbinary shellfulltitle
substenv() {
#############################
# shellcheck disable=SC2016 #
#############################
envsubst '$testsbin $shellbintitle $shellbin $shebang $extrapath $shellbinary $shellfulltitle' <"tests/templates/which.${1}.sh"
}
shellbintitle=busybox
shellbin="bin/sh.${shellbintitle}/sh"
shebang="${scriptDir}/${shellbin}"
extrapath="${shebang%/*}"
shellbinary="${shebang##*/}"
shellfulltitle="${shellbintitle} ${shellbinary}"
[ -r "tests/shellspec/002-which-classic-${shellbintitle}-sh.sh" ] ||
substenv classic >"tests/shellspec/002-which-classic-${shellbintitle}-sh.sh"
[ -r "tests/shellspec/003-which-extended-${shellbintitle}-sh.sh" ] ||
substenv extended >"tests/shellspec/003-which-extended-${shellbintitle}-sh.sh"
testid=4
for sh in ${shells}; do
shellbintitle="${sh}"
shellbin="bin/${shellbintitle}"
shebang="${scriptDir}/${shellbin}"
extrapath="${shebang%/*}"
shellbinary="${shebang##*/}"
shellfulltitle="${shellbinary}"
testScript="tests/shellspec/$(printf "%03d" "${testid}")-which-classic-${shellbintitle}-regular.sh"
[ -r "${testScript}" ] || substenv classic >"${testScript}"
testid="$((testid + 1))"
testScript="tests/shellspec/$(printf "%03d" "${testid}")-which-extended-${shellbintitle}-regular.sh"
[ -r "${testScript}" ] || substenv extended >"${testScript}"
testid="$((testid + 1))"
shellbin="bin/sh.${shellbintitle}/sh"
shebang="${scriptDir}/${shellbin}"
extrapath="${shebang%/*}"
shellbinary="${shebang##*/}"
shellfulltitle="${shellbintitle} ➤ POSIX ${shellbinary}"
testScript="tests/shellspec/$(printf "%03d" "${testid}")-which-classic-${shellbintitle}-sh.sh"
[ -r "${testScript}" ] || substenv classic >"${testScript}"
testid="$((testid + 1))"
testScript="tests/shellspec/$(printf "%03d" "${testid}")-which-extended-${shellbintitle}-sh.sh"
[ -r "${testScript}" ] || substenv extended >"${testScript}"
testid="$((testid + 1))"
done
shell=bin/sh
bin/shellspec --shell "${shell}" tests/shellspec/001-shellcheck.sh
[ -x bin/sh.busybox/sh ] && shell=bin/sh.busybox/sh
bin/shellspec --shell "${shell}" tests/shellspec/002-which-classic-busybox-sh.sh
bin/shellspec --shell "${shell}" tests/shellspec/003-which-extended-busybox-sh.sh
testid=4
shellspec() {
bin/shellspec --shell "${shell}" "tests/shellspec/$(printf "%03d" "${testid}")-which-${1}-${sh}-${2}.sh"
}
for sh in ${shells}; do
shell=bin/sh
[ -x "bin/${sh}" ] && shell="bin/${sh}"
shellspec classic regular
testid="$((testid + 1))"
shellspec extended regular
testid="$((testid + 1))"
shell=bin/sh
[ -x "bin/sh.${sh}/sh" ] && shell="bin/sh.${sh}/sh"
shellspec classic sh
testid="$((testid + 1))"
shellspec extended sh
testid="$((testid + 1))"
done