Debian Codegraph @

bash_unit

raw
1 #!/usr/bin/env bash
2 #
3 # bash unit testing enterprise edition framework for professionals
4 # Copyright (C) 2011-2016 Pascal Grange
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 #
17 # https://github.com/bash-unit/bash_unit
18
19 # shellcheck disable=2317 # Ignore unreachable - most function are not called.
20 # shellcheck disable=2155 # Ignore Declare and assign separately
21
22 VERSION=v2.3.3
23
24 ESCAPE=$(printf "\033")
25 NOCOLOR="${ESCAPE}[0m"
26 RED="${ESCAPE}[91m"
27 GREEN="${ESCAPE}[92m"
28 YELLOW="${ESCAPE}[93m"
29 BLUE="${ESCAPE}[94m"
30
31 # Make bash_unit immune to some basic unix commands faking
32 CAT="$(type -P cat)"
33 SED="$(type -P sed)"
34 GREP="$(type -P grep)"
35 RM="$(type -P rm)"
36 SHUF="$(type -P sort) -R"
37
38 # Store the number of tests run in a file so that the value is available
39 # from the parent process. This will become an issue if we start considering
40 # parallel test execution in the future.
41 TEST_COUNT_FILE="$(mktemp)"
42 # shellcheck disable=2064 # Use single quotes, expands now, not when signaled.
43 trap "$RM -f \"$TEST_COUNT_FILE\"" EXIT
44
45 fail() {
46 local message=${1:-}
47 local stdout=${2:-}
48 local stderr=${3:-}
49
50 # shellcheck disable=2154
51 notify_test_failed "$__bash_unit_current_test__"
52 notify_message "$message"
53 [[ -n "$stdout" ]] && [ -s "$stdout" ] && notify_stdout < "$stdout"
54 [[ -n "$stderr" ]] && [ -s "$stderr" ] && notify_stderr < "$stderr"
55
56 stacktrace | notify_stack
57 exit 1
58 }
59
60 assert() {
61 local assertion=$1
62 local message=${2:-}
63
64 _assert_expression \
65 "$assertion" \
66 "[ \$status == 0 ]" \
67 "\"$message\""
68 }
69
70 assert_fails() {
71 local assertion=$1
72 local message=${2:-}
73
74 _assert_expression \
75 "$assertion" \
76 "[ \$status != 0 ]" \
77 "\"$message\""
78 }
79
80 assert_fail() {
81 #deprecated, use assert_fails instead
82 assert_fails "$@"
83 }
84
85 assert_status_code() {
86 local expected_status=$1
87 local assertion="$2"
88 local message="${3:-}"
89
90 _assert_expression \
91 "$assertion" \
92 "[ \$status == $expected_status ]" \
93 "\"$message\" expected status code $expected_status but was \$status"
94 }
95
96 _assert_expression() {
97 local assertion=$1
98 local condition=$2
99 local message=$3
100 (
101 local stdout=$(mktemp)
102 local stderr=$(mktemp)
103 # shellcheck disable=2064 # Use single quotes, expands now, not when signaled.
104 trap "$RM -f \"$stdout\" \"$stderr\"" EXIT
105
106 local status
107 eval "($assertion)" >"$stdout" 2>"$stderr" && status=$? || status=$?
108 if ! eval "$condition"
109 then
110 fail "$(eval echo "$message")" "$stdout" "$stderr"
111 fi
112 ) || exit $?
113 }
114
115 assert_equals() {
116 local expected=$1
117 local actual=$2
118 local message=${3:-}
119 [[ -z $message ]] || message="$message\n"
120
121 if [ "$expected" != "$actual" ]
122 then
123 fail "$message expected [$expected] but was [$actual]"
124 fi
125 }
126
127 assert_not_equals() {
128 local unexpected=$1
129 local actual=$2
130 local message=${3:-}
131 [[ -z $message ]] || message="$message\n"
132
133 [ "$unexpected" != "$actual" ] || \
134 fail "$message expected different value than [$unexpected] but was the same"
135 }
136
137 assert_matches() {
138 local expected=$1
139 local actual=$2
140 local message=${3:-}
141 [[ -z $message ]] || message="$message\n"
142
143 if [[ ! "${actual}" =~ ${expected} ]]; then
144 fail "$message expected regex [$expected] to match [$actual]"
145 fi
146 }
147
148 assert_not_matches() {
149 local unexpected=$1
150 local actual=$2
151 local message=${3:-}
152 [[ -z $message ]] || message="$message\n"
153
154 if [[ "${actual}" =~ ${unexpected} ]]; then
155 fail "$message expected regex [$unexpected] should not match but matched [$actual]"
156 fi
157 }
158
159 assert_within_delta() {
160 function abs() {
161 local value=$1
162 local sign=$(( value < 0 ? -1 : 1 ))
163 echo $((value * sign))
164 }
165 function is_number() {
166 local value=$1
167 test "$value" -eq "$value" 2>/dev/null
168 }
169 local expected=$1
170 local actual=$2
171 local max_delta=$3
172 assert "is_number $expected" "$message expected value [$expected] is not a number"
173 assert "is_number $actual" "$message actual value [$actual] is not a number"
174 assert "is_number $max_delta" "$message max_delta [$max_delta] is not a number"
175 local message=${4:-}
176 [[ -z $message ]] || message="$message\n"
177
178 local actual_delta="$(abs $((expected - actual)))"
179
180 if (( actual_delta > max_delta )); then
181 fail "$message expected value [$expected] to match [$actual] with a maximum delta of [$max_delta]"
182 fi
183 }
184
185 assert_no_diff() {
186 local expected=$1
187 local actual=$2
188 local message=${3:-}
189 [[ -z "$message" ]] || message="$message\n"
190
191 assert "diff '${expected}' '${actual}'" \
192 "$message expected '${actual}' to be identical to '${expected}' but was different"
193 }
194
195 fake() {
196 local command=$1
197 shift
198 if [ $# -gt 0 ]
199 then
200 eval "function $command() { export FAKE_PARAMS=(\"\$@\") ; $* ; }"
201 else
202 eval "function $command() { echo \"$($CAT)\" ; }"
203 fi
204 export -f "${command?}"
205 }
206
207 stacktrace() {
208 local i=1
209 while [ -n "${BASH_SOURCE[$i]:-}" ]
210 do
211 echo "${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}:${FUNCNAME[$i]}()"
212 i=$((i + 1))
213 done | "$GREP" -v "^$BASH_SOURCE"
214 }
215
216 run_test_suite() {
217 local failure=0
218
219 if run_setup_suite
220 then
221 run_tests || failure=$?
222 else
223 failure=$?
224 fi
225 run_teardown_suite
226
227 return $failure
228 }
229
230 run_setup_suite() {
231 if declare -F | "$GREP" ' setup_suite$' >/dev/null
232 then
233 setup_suite
234 fi
235 }
236
237 maybe_shuffle() {
238 # shellcheck disable=2015 # A && B || C. C may run when A is true.
239 ((randomize)) && $SHUF || $CAT
240 }
241
242 run_tests() {
243 local failure=0
244
245 local pending_tests=$(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::')
246 if [[ -n "$skip_pattern" ]]
247 then
248 local skipped_tests=$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -E "$skip_pattern" | "$SED" -e 's: .*::')
249 local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -v -E "$skip_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
250 else
251 local skipped_tests=""
252 local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)"
253 fi
254
255 local test_count=$(cat "${TEST_COUNT_FILE}")
256 test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests")))
257 echo "${test_count}" > "${TEST_COUNT_FILE}"
258
259 for pending_test in $pending_tests
260 do
261 notify_test_starting "$pending_test"
262 notify_test_pending "$pending_test"
263 done
264
265 for skipped_test in $skipped_tests
266 do
267 notify_test_starting "$skipped_test"
268 notify_test_skipped "$skipped_test"
269 done
270
271 for test in $tests_to_run
272 do
273 (
274 local status=0
275 declare -F | "$GREP" ' setup$' >/dev/null && setup
276 (__bash_unit_current_test__="$test" run_test) || status=$?
277 declare -F | "$GREP" ' teardown$' >/dev/null && teardown
278 exit $status
279 )
280 failure=$(( $? || failure))
281 done
282
283 return $failure
284 }
285
286 run_test() {
287 set -e
288 notify_test_starting "$__bash_unit_current_test__"
289 "$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__"
290 }
291
292 run_teardown_suite() {
293 if declare -F | "$GREP" ' teardown_suite$' >/dev/null
294 then
295 teardown_suite
296 fi
297 }
298
299 usage() {
300 echo "$1" >&2
301 echo "$0 [-f <output format>] [-p <pattern1>] [-p <pattern2>] [-s <skip_pattern1>] [-s <skip_pattern2>] [-r] ... <test_file1> <test_file2> ..." >&2
302 echo >&2
303 echo "Runs tests in test files that match <pattern>s" >&2
304 echo "Test that match the skip patterns are skipped and displayed as SKIPPED" >&2
305 echo "<output format> is optional only supported value is tap" >&2
306 echo "-r to execute test cases in random order" >&2
307 echo "-v to get current version information" >&2
308 echo "See https://github.com/bash-unit/bash_unit" >&2
309 exit 1
310 }
311
312 # Formatting
313
314 pretty_success() {
315 pretty_format "$GREEN" "\u2713" "${1:-}"
316 }
317
318 pretty_warning() {
319 pretty_format "$YELLOW" "\u2717" "${1:-}"
320 }
321
322 pretty_failure() {
323 pretty_format "$RED" "\u2717" "${1:-}"
324 }
325
326 pretty_format() {
327 local color="$1"
328 local pretty_symbol="$2"
329 local alt_symbol="${3:-}"
330 local term_utf8=false
331 #env
332 if is_terminal && [[ "${LANG:-}" =~ .*UTF-8.* ]]
333 then
334 term_utf8=true
335 fi
336 (
337 $CAT
338 if $term_utf8
339 then
340 echo -en " $pretty_symbol "
341 else
342 [[ -n "$alt_symbol" ]] && echo -en " $alt_symbol "
343 fi
344 ) | color "$color"
345 }
346
347 color() {
348 _start_color() {
349 if is_terminal ; then echo -en "$color" ; fi
350 }
351 _stop_color() {
352 if is_terminal ; then echo -en "$NOCOLOR" ; fi
353 }
354 local color=$1
355 shift
356 _start_color
357 if [ $# -gt 0 ]
358 then
359 echo "$*"
360 else
361 $CAT
362 fi
363 _stop_color
364 }
365
366 is_terminal() {
367 [ -t 1 ] || [[ "${FORCE_COLOR:-}" == true ]]
368 }
369
370 text_format() {
371 notify_suite_starting() {
372 local test_file="$1"
373 echo "Running tests in $test_file"
374 }
375 notify_test_starting() {
376 local test="$1"
377 echo -e -n "\tRunning $test ... " | color "$BLUE"
378 }
379 notify_test_pending() {
380 echo -n "PENDING" | pretty_warning
381 echo
382 }
383 notify_test_skipped() {
384 echo -n "SKIPPED" | pretty_warning
385 echo
386 }
387 notify_test_succeeded() {
388 echo -n "SUCCESS" | pretty_success
389 echo
390 }
391 notify_test_failed() {
392 echo -n "FAILURE" | pretty_failure
393 echo
394 }
395 notify_message() {
396 local message="$1"
397 # shellcheck disable=SC2059
398 [[ -z "$message" ]] || printf -- "$message\n"
399 }
400
401 notify_stdout() {
402 "$SED" 's:^:out> :' | color "$GREEN"
403 }
404 notify_stderr() {
405 "$SED" 's:^:err> :' | color "$RED"
406 }
407 notify_stack() {
408 color "$YELLOW"
409 }
410 notify_suites_succeded() {
411 echo -n "Overall result: SUCCESS" | pretty_success
412 echo
413 }
414 notify_suites_failed() {
415 echo -n "Overall result: FAILURE" | pretty_failure
416 echo
417 }
418 }
419
420 tap_format() {
421 notify_suite_starting() {
422 local test_file="$1"
423 echo "# Running tests in $test_file"
424 }
425 notify_test_starting() {
426 :
427 }
428 notify_test_pending() {
429 local test="$1"
430 echo -n "ok" | pretty_warning -
431 echo -n "$test" | color "$BLUE"
432 echo " # todo test to be written" | color "$YELLOW"
433 }
434 notify_test_skipped() {
435 local test="$1"
436 echo -n "ok" | pretty_warning -
437 echo -n "$test" | color "$BLUE"
438 echo " # skip test not run" | color "$YELLOW"
439 }
440 notify_test_succeeded() {
441 local test="$1"
442 echo -n "ok" | pretty_success -
443 echo "$test" | color "$BLUE"
444 }
445 notify_test_failed() {
446 local test="$1"
447 echo -n "not ok" | pretty_failure -
448 echo "$test" | color "$BLUE"
449 }
450 notify_message() {
451 local message="$1"
452 [[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /'
453 }
454 notify_stdout() {
455 "$SED" 's:^:# out> :' | color "$GREEN"
456 }
457 notify_stderr() {
458 "$SED" 's:^:# err> :' | color "$RED"
459 }
460 notify_stack() {
461 "$SED" 's:^:# :' | color "$YELLOW"
462 }
463 notify_suites_succeded() {
464 local message="$1"
465 echo "1..$message"
466 }
467 notify_suites_failed() {
468 local message="$1"
469 echo "1..$message"
470 }
471 }
472
473 quiet_mode() {
474 notify_message() {
475 :
476 }
477 notify_stdout() {
478 :
479 }
480 notify_stderr() {
481 :
482 }
483 notify_stack() {
484 :
485 }
486 }
487
488 skip_if() {
489 local condition="$1"
490 local pattern="$2"
491 if eval "$condition" >/dev/null 2>&1
492 then
493 skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}"
494 skip_pattern_separator="|"
495 fi
496 }
497
498 count() {
499 local tests="$1"
500 if [[ -z "$tests" ]]; then
501 echo 0
502 else
503 echo "$tests" | wc -l
504 fi
505 }
506
507 output_format=text
508 verbosity=normal
509 test_pattern=""
510 test_pattern_separator=""
511 skip_pattern=""
512 skip_pattern_separator=""
513 randomize=0
514 while getopts "vp:s:f:rq" option
515 do
516 case "$option" in
517 p)
518 test_pattern="${test_pattern}${test_pattern_separator}${OPTARG}"
519 test_pattern_separator="|"
520 ;;
521 s)
522 skip_pattern="${skip_pattern}${skip_pattern_separator}${OPTARG}"
523 skip_pattern_separator="|"
524 ;;
525 f)
526 output_format="${OPTARG}"
527 ;;
528 r)
529 randomize=1
530 ;;
531 v)
532 echo "bash_unit $VERSION"
533 exit
534 ;;
535 q)
536 verbosity=quiet
537 ;;
538 ?)
539 usage
540 ;;
541 esac
542 done
543 shift $((OPTIND-1))
544
545 for test_file in "$@"
546 do
547 test -e "$test_file" || usage "file does not exist: $test_file"
548 test -r "$test_file" || usage "can not read file: $test_file"
549 done
550
551 case "$output_format" in
552 text)
553 text_format
554 ;;
555 tap)
556 tap_format
557 ;;
558 *)
559 usage "unsupported output format: $output_format"
560 ;;
561 esac
562
563 if [[ "$verbosity" == quiet ]]
564 then
565 quiet_mode
566 fi
567
568 #run tests received as parameters
569 failure=0
570 echo 0 > "${TEST_COUNT_FILE}"
571 for test_file in "$@"
572 do
573 notify_suite_starting "$test_file"
574 (
575 # Ensure bash_unit will exit with failure
576 # in case of syntax error.
577 set -e
578
579 if [[ "${STICK_TO_CWD:-}" != true ]]
580 then
581 cd "$(dirname "$test_file")"
582 # shellcheck disable=1090
583 source "./$(basename "$test_file")"
584 else
585 # shellcheck disable=1090
586 source "$test_file"
587 fi
588 set +e
589 run_test_suite
590 )
591 failure=$(( $? || failure))
592 done
593
594 if ((failure))
595 then
596 notify_suites_failed "$(cat "${TEST_COUNT_FILE}")"
597 else
598 notify_suites_succeded "$(cat "${TEST_COUNT_FILE}")"
599 fi
600
601 exit $failure