-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunittest
executable file
·347 lines (312 loc) · 9.85 KB
/
unittest
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
#!/bin/sh
#
# unittest - unit tests runner for POSIX shell.
#
# SPDX-FileCopyrightText: 2014 Maciej Żok <https://github.com/macie/unittest.sh>
# SPDX-License-Identifier: MIT
set -u
readonly UT_VERSION='local-dev'
UT_CURRENT_LOCATION=''
UT_COLOR_OUTPUT='auto' # 'auto' | 'on' | 'off'
#
# MESSAGES
#
# Write test status message to stdout in color when forced or on capable
# interactive terminal (using UT_COLOR_OUTPUT global variable).
unittest__print_result() (
readonly testcase="${1:-unknown}"
readonly status="${2:-????}"
if [ "$UT_COLOR_OUTPUT" = 'on' ] || { [ -t 1 ] && [ "$UT_COLOR_OUTPUT" = 'auto' ]; }; then
readonly color_normal='\033[0m'
case $status in
PASS)
readonly color_testcase='\033[0m' # default
readonly color_status='\033[0;32m' # green on default
;;
FAIL)
readonly color_testcase='\033[0;31m' # red on default
readonly color_status='\033[97;41m' # white on red
;;
SKIP)
readonly color_testcase='\033[0;37m' # gray on default
readonly color_status='\033[0;37m' # gray on default
;;
*)
readonly color_testcase='\033[0;90m' # dark gray on default
readonly color_status='\033[97;100m' # white on dark gray
;;
esac
fi
printf "${color_status-}%s\t${color_testcase-}%s${color_normal-}\n" "$status" "$testcase"
return 0
)
# Write debug message to stderr in color: when forced or on capable
# interactive terminal (using UT_COLOR_OUTPUT global variable).
# Uses UT_CURRENT_LOCATION global variable for test location.
unittest__print_debug() (
readonly location="${UT_CURRENT_LOCATION##*/}"
readonly header="${1:-'UNKNOWN ERROR'}"
shift 1 # $@ - paragraphs
if [ "$UT_COLOR_OUTPUT" = 'on' ] || { [ -t 2 ] && [ "$UT_COLOR_OUTPUT" = 'auto' ]; }; then
readonly color_normal='\033[0m'
readonly color_header='\033[0;34m' # blue on default
readonly color_quote='\033[0;37m' # gray on default
fi
# align header line to the 80 chars width. Header is usually not longer than 15 chars
printf "\n${color_header-}%-20s%60s${color_normal-}\n\n" "-- ${header} -" "- ${location:--}" |
sed -e 's/ /--/g' -e 's/- -/---/g' >&2
for paragraph in "$@"; do
# shellcheck disable=SC2059 # '%b' is not portable yet
case ${paragraph} in
' '*) printf "${color_quote-}" >&2 ;;
*) ;;
esac
printf "%s${color_normal-}\n\n" "${paragraph}" >&2
done
return 0
)
#
# ASSERTIONS
#
# Drop-in replacement for test(1) command with user-friendly error messages.
# shellcheck disable=SC2317 # it shadows test command called inside test files
test() (
test_args='test'
for arg in "$@"; do
case "${arg}" in
!|-*|=|!=|\<|\>)
test_args="${test_args} ${arg}"
;;
[0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9])
test_args="${test_args} ${arg}"
;;
*)
test_args="${test_args} '${arg}'"
;;
esac
done
error_msg=$(/bin/test "$@" 2>&1)
case $? in
0) ;;
1)
unittest__print_debug 'FAILED TEST' \
'I expected:' \
" ${test_args}" \
'to be true, but the result was false.'
return 1
;;
*)
unittest__print_debug 'WRONG ASSERTION' \
'I tried to check:' \
" ${test_args}" \
'but I got error with message:' \
" ${error_msg}" \
'Did you use proper operator?' \
"Hint: Some operators requires specific type of values. See: 'man test'."
return 1
;;
esac
return 0
)
#
# FUNCTIONS
#
##
# Find files with tests (test_*.sh).
# SYNOPSIS:
# unittest__test_files [directory...]
# OPERANDS:
# directory - A pathname of directory to search in. If no directory is
# given, it will look for 'tests' directory inside current one. If
# a directory is '-', it will use stdin.
# STDIN:
# (optional) List of directories to search in. Used when call with '-' argument.
# STDOUT:
# List of 'test_*.sh' file paths.
# STDERR:
# (optional) Debug/error message.
# EXIT STATUS:
# 0 - Successfully traversed all directories.
# >0 - An error occurred.
# EXAMPLES:
# unittest__test_files ./unit_tests ./integration_tests
# ls ../ | unittest__test_files -
# CAVEATS:
# Calling it inside pipeline without '-' will disregards standard input and
# use defaults instead.
unittest__test_files() (
{
if [ "${1-}" = '-' ]; then
cat -
else
printf '%s\n' "$@"
fi
} |
while read -r testdir; do
if ! find "${testdir:-./}" -path "*${testdir:-tests/}*" -name 'test_*.sh' 2>/dev/null; then
unittest__print_debug 'TESTS NOT FOUND' \
"I was looking for 'test_*.sh' files inside '${testdir:-tests/}' directory using:" \
" $ find \"${testdir:-./}\" -path \"*${testdir:-tests/}*\" -name 'test_*.sh' -print" \
'but instead of files I got an error with message:' \
" $(find "${testdir:-./}" -path "*${testdir:-tests/}*" -name 'test_*.sh' -print 2>&1)"
return 1
fi
done
return $?
)
##
# Run tests from given files.
# STDIN: List of files.
# STDOUT: Test name with status.
# STDERR: (optional) Debug/error message.
# EXIT STATUS:
# 0 - All tests passed.
# >0 - Some tests failed.
##
unittest__run() {
ut_exit_code=0
while read -r ut_testfile; do
(
UT_CURRENT_LOCATION="${ut_testfile#./}"
ut_before_all=$(sed -n -e 's/^[ \t]*\(before_all\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(beforeAll\)[ \t]*(.*/\1/p' "${ut_testfile}")
case "$ut_before_all" in
*beforeAll*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' beforeAll()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'before_all'."
;;
esac
ut_after_all=$(sed -n -e 's/^[ \t]*\(after_all\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(afterAll\)[ \t]*(.*/\1/p' "${ut_testfile}")
case "$ut_after_all" in
*afterAll*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' afterAll()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'after_all'."
;;
esac
ut_before_each=$(sed -n -e 's/^[ \t]*\(before_each\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(beforeEach\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(setUp\)[ \t]*(.*/\1/p' "${ut_testfile}")
case "$ut_before_each" in
*beforeEach*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' beforeEach()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'before_each'."
;;
*setUp*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' setUp()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'before_each'."
;;
esac
ut_after_each=$(sed -n -e 's/^[ \t]*\(after_each\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(afterEach\)[ \t]*(.*/\1/p' -e 's/^[ \t]*\(tearDown\)[ \t]*(.*/\1/p' "${ut_testfile}")
case "$ut_after_each" in
*afterEach*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' afterEach()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'after_each'."
;;
*tearDown*)
unittest__print_debug 'DEPRECATED' \
'Function:' \
' tearDown()' \
'is deprecated and will be unsupported in the future versions.' \
"Hint: Rename it to 'after_each'."
;;
esac
ut_testfunctions=$(awk '/^[^{]*}/ { nesting-- } /^[ \t]*x{0,1}test_[^(=]*\(.*/ { if (nesting == 0) { print $1 } } /{[^}]*$/ { nesting++ }' "${ut_testfile}" | tr -d '(){}')
# shellcheck source=/dev/null
. "${ut_testfile}"
${ut_before_all}
for ut_testcase in ${ut_testfunctions}; do
UT_CURRENT_LOCATION="${ut_testfile#./}:${ut_testcase}"
case ${ut_testcase} in
x*)
unittest__print_result "${UT_CURRENT_LOCATION}" 'SKIP'
;;
*)
${ut_before_each}
# test result is status of last command in test
if ${ut_testcase}; then
unittest__print_result "${UT_CURRENT_LOCATION}" 'PASS'
else
# last command in test failed
ut_exit_code=1
unittest__print_result "${UT_CURRENT_LOCATION}" 'FAIL'
fi
${ut_after_each}
;;
esac
done
${ut_after_all}
unset -v UT_CURRENT_LOCATION ut_testfile ut_before_all ut_after_all ut_before_each ut_after_each ut_testfunctions ut_testcase
exit ${ut_exit_code}
)
ut_exit_code=$?
done
unset -v ut_testfile
return ${ut_exit_code}
}
#
# MAIN ROUTINE
#
{
# Color output by default: on supported terminals when NO_COLOR is not set.
# Supported terminals are recognized based on TERM variable. When TERM is
# not set (for example inside CI environment) we assume that terminal is dumb.
# NO_COLOR has precedence over CLICOLOR_FORCE. See: https://bixense.com/clicolors/
if [ -n "${NO_COLOR:-}" ]; then
UT_COLOR_OUTPUT='off'
elif [ -n "${CLICOLOR_FORCE:-}" ]; then
UT_COLOR_OUTPUT='on'
elif [ "$(TERM=${TERM:-dumb} tput colors)" -lt 8 ]; then
UT_COLOR_OUTPUT='off'
fi
case $# in
0) # discovery mode
unittest__test_files | unittest__run
exit $?
;;
1)
case $1 in
-h|--help)
cat >&2 <<-'EOF'
unittest - unit tests framework for shell scripts.
Usage:
unittest [options] [test_directory | test_file]
Options:
-h, --help Show this help and exit.
-v, --version Show version number and exit.
Without any arguments it will run all tests from 'tests' directory.
EOF
exit 0
;;
-v|--version)
printf 'unittest %s\n' "$UT_VERSION" >&2
exit 0
;;
*) # specified directory/file
if [ -d "$1" ]; then
unittest__test_files "$1" | unittest__run
exit $?
elif [ -f "$1" ]; then
printf '%s\n' "$1" | unittest__run
exit $?
fi
;;
esac
esac
unittest__print_debug 'INVALID USAGE' \
"I cannot understand '$*' option. Did you want to use option or did you misspell file/directory?" \
'Hint: Find valid usage with:' \
' $ unittest -h'
exit 64 # EX_USAGE
}