-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashunit2.sh
More file actions
executable file
·167 lines (139 loc) · 3.7 KB
/
Copy pathbashunit2.sh
File metadata and controls
executable file
·167 lines (139 loc) · 3.7 KB
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
#!/bin/bash
# The naming convention for global variable prefixes in bashunit2 is:
# "underscore + package name + two underscores".
declare -a _bashunit2__tests
declare _bashunit2__test_function_filter=
declare _bashunit2__run_last_exit_status=
declare _bashunit2__run_last_stdout=
declare _bashunit2__run_last_stderr=
bashunit2::_err() {
echo "bashunit2: ${1:-}" >&2
}
bashunit2::_die() {
bashunit2::_err "${1:-Died} at ${BASH_SOURCE[1]} line ${BASH_LINENO[0]}."
exit 1
}
bashunit2::_print_functions() {
local _ f
while read -r _ _ f; do echo "$f"; done < <(declare -F)
}
bashunit2::_print_tests() {
local f
while read -r f; do
if [[ $f =~ ^test_ || $f =~ ::test_ ]]; then
echo "$f"
fi
done < <(bashunit2::_print_functions)
}
bashunit2::_print_filtered_tests() {
local f
while read -r f; do
if [[ $f =~ $_bashunit2__test_function_filter ]]; then
echo "$f"
fi
done < <(bashunit2::_print_tests)
}
bashunit2::_discover_tests() {
local f
_bashunit2__tests=()
while read -r f; do
_bashunit2__tests+=("$f")
done < <(bashunit2::_print_filtered_tests)
if [[ ${#_bashunit2__tests[@]} -eq 0 ]]; then
bashunit2::_err "Could not find any tests."
return 1
fi
return 0
}
bashunit2::_run_tests() {
echo "TAP version 14"
echo "1..${#_bashunit2__tests[@]}"
local ok=y t
for t in "${_bashunit2__tests[@]}"; do
# The test function may execute exit.
# We execute the command in a sub-process to continue processing.
if ( $t ); then
echo "ok - $t"
else
echo "not ok - $t"
ok=
fi
done
[ "$ok" ]
}
bashunit2::_usage() {
while IFS='' read -r line; do
echo "$line" >&2
done <<__USAGE__
Usage: bashunit2::run_tests [OPTION]...
-f PATTERN Filter by regular expression PATTERN for the function name
to be tested.
-h display this help and exit.
__USAGE__
}
bashunit2::run_tests() {
local opt
while getopts f:h opt; do
case "$opt" in
f) _bashunit2__test_function_filter="$OPTARG";;
h) bashunit2::_usage; return 1;;
*) bashunit2::_usage; return 1;;
esac
done
shift $((OPTIND-1))
bashunit2::_discover_tests && bashunit2::_run_tests
}
bashunit2::print_run_last_exit_status() {
echo "$_bashunit2__run_last_exit_status"
}
bashunit2::print_run_last_stdout() {
echo -n "$_bashunit2__run_last_stdout"
}
bashunit2::print_run_last_stderr() {
echo -n "$_bashunit2__run_last_stderr"
}
bashunit2::run() {
# Assign stderr, stdout, and exit status to variables without creating
# temporary files.
# https://stackoverflow.com/questions/13806626/
# shellcheck disable=SC1090
. <(
{
_bashunit2_stderr=$(
{
# shellcheck disable=SC2030
_bashunit2_stdout=$( "$@" )
# shellcheck disable=SC2030
_bashunit2_exit_status=$?
} 2>&1
declare -p _bashunit2_stdout _bashunit2_exit_status >&2
)
declare -p _bashunit2_stderr
} 2>&1
)
# Ignore shellcheck warnings.
# shellcheck disable=SC2031
: "${_bashunit2_exit_status:=}" "${_bashunit2_stdout:=}" \
"${_bashunit2_stderr:=}"
_bashunit2__run_last_exit_status="$_bashunit2_exit_status"
_bashunit2__run_last_stdout="$_bashunit2_stdout"
_bashunit2__run_last_stderr="$_bashunit2_stderr"
return 0
}
bashunit2::assert_eq_str() {
local e="$1" a="$2"
if [[ "$e" != "$a" ]]; then
bashunit2::_err "expected: '$e', actual: '$a'"
return 1
fi
return 0
}
# TODO: Remove deprecated function.
# This function exists for backward compatibility.
bashunit2_run_tests() {
bashunit2::_err '[DEPRECATED] This function will be removed in the future.'
bashunit2::run_tests
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
bashunit2::run_tests "$@"
fi