-
Notifications
You must be signed in to change notification settings - Fork 3
/
test
executable file
·96 lines (88 loc) · 2.08 KB
/
test
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
LANG=C
# shellcheck source=./shellib/shellib.sh
. "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/shellib/shellib.sh"
# Run test file $1
function run_test_file() {
if [ -f "$1" ] && [ -r "$1" ]; then
info "running test file $1:" "$symbol_doing"
if bats "$1"; then
info "$1 done" "$symbol_done"
else
err "$1 failed" "$symbol_failed"
return "$status_err"
fi
else
err "Could not run test file $1"
return "$status_err"
fi
}
# Run named test set $1
function run_test_set() {
if [ -f "$1" ] && [ -r "$1" ]; then
info "running test set $1" "$symbol_doing"
test_path="$(dirname "$1")"
mapfile -t set_lines <"$1"
for set_line in "${set_lines[@]}"; do
case "$set_line" in
'') ;; # Ignore empty lines
\#*) ;; # Ignore comments
*\.bats | *\.set)
run_test "$test_path/$set_line"
;;
*)
warn "Unexpected test set $1 line $set_line"
;;
esac
done
else
err "Could not run test set $1"
return "$status_err"
fi
}
# Run test file or test set $1
function run_test() {
case "$1" in
*.bats)
run_test_file "$1"
;;
*.set)
run_test_set "$1"
;;
*)
err "Unknown test type $1"
usage
return "$status_err"
;;
esac
}
# Print usage
function usage() {
echo "Usage: ${TEST_ARGV:-$0} [path [...]]]"
echo
echo ' path Path to test file or test set'
}
# Main
# For zero arguments: prints usage
# One or more arguments: runs test
function main() {
if [ -z "${1+x}" ]; then
usage
return
fi
for arg in "$@"; do
if [ -f "$arg" ]; then
run_test "$arg"
else
err "Unknown test type $1"
usage
return "$status_err"
fi
done
}
# Skip execution under test
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
main "$@"
fi