-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathassert.sh
More file actions
120 lines (108 loc) · 3.21 KB
/
Copy pathassert.sh
File metadata and controls
120 lines (108 loc) · 3.21 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
#
# assert.sh: minimal test assertions for posix shell
#
# Assertions are NON-FATAL: a failure is recorded and the file keeps going,
# so one bad assertion does not hide every test after it. Totals are printed
# by an EXIT trap, which also sets the exit status, so a test file cannot
# forget to report.
#
# Set ASSERT_VERBOSE=1 to also print passing assertions.
#
_assert_pass=0
_assert_fail=0
_assert_skip=0
_assert_file=${0##*/}
_assert_errfile=${TMPDIR:-/tmp}/shlib-assert.$$
# Run an assertion's command, stashing its stderr.
#
# A negative assertion is EXPECTED to make things complain: `assertFalse
# "hash_sha256 NONEXISTANT"` really does run sha256sum on a missing file.
# Printing that on a passing run buried the real output -- 25 lines of stderr
# against 14 of stdout -- and made green runs look broken. So stderr is held
# back and only shown when the assertion fails, where it is the useful part.
#
# eval runs in the CURRENT shell, not a subshell, so any side effects behave
# exactly as they did before.
_assert_run() {
eval "$1" 2>"$_assert_errfile"
}
_assert_show_stderr() {
test -s "$_assert_errfile" || return 0
sed 's/^/ | /' "$_assert_errfile" >&2
}
_assert_ok() {
_assert_pass=$((_assert_pass + 1))
test -z "${ASSERT_VERBOSE-}" || echo "ok ${_assert_file}: $1"
}
_assert_no() {
_assert_fail=$((_assert_fail + 1))
echo "FAIL ${_assert_file}: $1" >&2
}
# assert_skip records a test that could not run (missing dependency).
# It is not a failure.
assert_skip() {
_assert_skip=$((_assert_skip + 1))
echo "skip ${_assert_file}: $1" >&2
}
assertTrue() {
if _assert_run "$1"; then
_assert_ok "$2"
else
_assert_no "assertTrue [$1] $2"
_assert_show_stderr
fi
}
assertFalse() {
if _assert_run "$1"; then
_assert_no "assertFalse [$1] $2"
_assert_show_stderr
else
_assert_ok "$2"
fi
}
assertEquals() {
if [ "$1" = "$2" ]; then
_assert_ok "$3"
else
_assert_no "assertEquals want='$1' got='$2' $3"
fi
}
assertNotEquals() {
if [ "$1" != "$2" ]; then
_assert_ok "$3"
else
_assert_no "assertNotEquals want!='$1' got='$2' $3"
fi
}
# assert_summary prints totals and exits with the right status. It is wired
# to EXIT below; calling it directly is harmless but unnecessary.
assert_summary() {
test -z "$_assert_done" || return 0
_assert_done=1
rm -f "$_assert_errfile"
_assert_total=$((_assert_pass + _assert_fail))
if [ "$_assert_fail" -gt 0 ]; then
echo "FAIL ${_assert_file}: ${_assert_fail}/${_assert_total} assertions failed" >&2
exit 1
fi
if [ "$_assert_skip" -gt 0 ]; then
echo "ok ${_assert_file}: ${_assert_total} assertions, ${_assert_skip} skipped"
exit 0
fi
echo "ok ${_assert_file}: ${_assert_total} assertions"
exit 0
}
# A test file that calls an undefined function would otherwise just print
# "command not found" and still report ok, because nothing asserted. Treat a
# file that ran zero assertions as a failure.
assert_summary_strict() {
test -n "$_assert_done" && return 0
if [ "$((_assert_pass + _assert_fail + _assert_skip))" -eq 0 ]; then
_assert_done=1
rm -f "$_assert_errfile"
echo "FAIL ${_assert_file}: no assertions ran" >&2
exit 1
fi
assert_summary
}
trap assert_summary_strict EXIT