-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-tests
210 lines (175 loc) · 3.94 KB
/
bash-tests
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
#!/bin/bash
# bash-tests - bash library for unit and integration tests
#
# Copyright (C) 2019 Christian Garbs <mitch@cgarbs.de>
# Licensed under GNU GPL v3 or later.
#
# This file is part of bash-n.
#
# bash-n is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# bash-n is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with bash-n. If not, see <http://www.gnu.org/licenses/>.
set -e
############################
#
# public test interface
#
# perform an assertion
# $1 SUBJECT to check
# $* PARAMETERS depending on SUBJECT
assert_that()
{
local subject=$1
shift
case $subject in
variable)
_assert_variable "${@}"
;;
stdout)
_assert_stdout "${@}"
;;
*)
fail "unknown test subject <$subject> in assert_that"
;;
esac
}
# fail the test with a message
# $* MESSAGE
fail()
{
echo "${term_error}$*${term_normal}" >&2
exit 1
}
############################
#
# private methods
#
_add_empty_before_test_if_missing()
{
if ! declare -F before_test; then
before_test()
{
:
}
fi
}
_setup_colors()
{
if tput sgr0 >/dev/null 2>&1; then
term_normal=$(tput sgr0)
term_error=$(tput setaf 1)
term_success=$(tput setaf 2)
else
term_normal=
term_error=
term_success=
fi
echo -n $term_normal
}
_setup_test_name()
{
test_script=${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}
test_script=${test_script##/*}
test_script=${test_script:-unknown}
}
_create_tmpdir()
{
tmpdir=$(mktemp -d)
trap "rm -rf '$tmpdir'" EXIT
}
_scan_test_methods()
{
test_method_file=$tmpdir/tests
declare -F | sed 's/^declare -f //' | grep ^test_ | sort -R > $test_method_file
read -r test_count _ <<< $(wc -l "$test_method_file")
if [ "$test_count" -lt 1 ]; then
fail "no tests defined"
fi
}
# $1 ACTUAL value
# $2 EXPECTED value
_assert_equals()
{
local actual=$1 expected=$2
if [ "$actual" != "$expected" ]; then
fail "expected <$expected> but got <$actual>"
fi
}
# $1 VARIABLE to check
# $2 VERB describing the check
# $* PARAMETERS depending on VERB
_assert_variable()
{
local variable=$1 verb=$2 expected=$3
echo -n " check \$$variable: " >&2
case $verb in
is)
_assert_equals "${!variable}" "$expected"
;;
*)
fail "unknown verb <$verb> in <assert_that variable>"
;;
esac
echo "ok" >&2
}
# $1 VERB describing the check
_assert_stdout()
{
local verb=$1
echo -n " check stdout: " >&2
[ "$verb" != 'is' ] && fail "unknown verb <$verb> in <assert_that stdout>"
export tmpdir
(
cd "$tmpdir"
cat > expected
cp stdout actual
if ! cmp -s expected actual; then
diff -Narup expected actual >&2
fail "!! output differs"
fi
) || exit 1
echo "ok" >&2
}
# $1 TEST NAME is the declared bash function to call
_run_test()
{
local test_stdout=$tmpdir/stdout
echo " $test_name" >&2
before_test
$test_name > "$test_stdout" || fail "execution failed"
test_ok_count=$((test_ok_count + 1))
}
############################
#
# test execution
#
_setup_test_name
_setup_colors
_create_tmpdir
_scan_test_methods
_add_empty_before_test_if_missing
test_ok_count=0
while IFS= read -u 9 -r test_name; do
_run_test "$test_name"
done 9<"$test_method_file"
if [ $test_count == $test_ok_count ]; then
tests_missing=false
echo -n $term_success >&2
else
tests_missing=true
echo -n $term_error >&2
fi
printf "%s: %d/%d tests ok%s\n" $test_script $test_ok_count $test_count $term_normal >&2
if $tests_missing; then
fail "some test executions are missing!"
fi
exit 0