-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.sh
executable file
·67 lines (51 loc) · 1.86 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
#!/usr/bin/env sh
# test.sh - minimal script testing 'framework'
# Copyright (C) 2020-2021 Peter Willis
[ "${DEBUG:-0}" = "1" ] && set -x
set -u
### How this thing works:
### 1. Create some shell scripts in tests/ directory, filename ending with '.t'
### 2. Define some functions ('_t_NAME') in that shell script
### a. Put the space-separated NAMEs in $ext_tests
### b. Register test pass/fail with 'return 0' / 'return 1'
### 3. Run `./test.sh tests/*.t`
# Define some common tests here
#_t_something () {
#}
# Don't modify anything after here
### _main() - run the tests
### Arguments:
### TESTPATH - A file path ending with '.t'
_main () {
_fail=0 _pass=0 _failedtests=""
testsh_pwd="$(pwd)"
for i in "$@" ; do
cd "$testsh_pwd"
# The following variables should be used by *.t scripts
base_name="$(basename "$i" .t)" ### So you don't need ${BASH_SOURCE[0]}
tmp="$(mktemp -d)" ### Copy your test files into here
. "$i" # load the test script into this shell
# Now we should have a variable $ext_tests set by the test script.
# The value is a string of space-separated names of '_t_SOMETHING' functions to run.
fail=0 pass=0
for t in $ext_tests ; do
if ! _t_$t ; then
echo "$0: $base_name: Test $t failed"
fail=$(($fail+1))
_failedtests="$_failedtests $base_name:$t"
else
echo "$0: $base_name: Test $t succeeded"
pass=$(($pass+1))
fi
done
rm -rf "$tmp"
[ $fail -gt 0 ] && echo "$0: $base_name: Failed $fail tests" && _fail="$(($_fail+$fail))"
_pass=$(($_pass+$pass))
done
}
_main "$@"
echo "$0: Passed $_pass tests"
if [ $_fail -gt 0 ] ; then
echo "$0: Failed $_fail tests: $_failedtests"
exit 1
fi