2
# shfuncs : test suite common shell functions
3
# which was shipped with the TET example code.
5
. "$XDG_TEST_DIR/include/testfuncs.sh"
7
assert_exit() # execute command (saving output) and check exit code
12
# make sure nothing is hanging around from a prev. test.
13
rm -f out.stdout out.stderr
15
# $1 is command, $2 is expected exit code (0 or "N" for non-zero)
16
( "$@" > out.stdout 2> out.stderr )
21
if [ -z "$EXPECT" ]; then
24
if [ "$EXPECT" = N -a "$CODE" -eq 0 ]; then
25
test_fail "Command ($*) gave exit code $CODE, expected nonzero"
26
elif [ "$EXPECT" != N ] && [ "$EXPECT" -ne "$CODE" ]; then
27
test_fail "Command ($*) gave exit code $CODE, expected $EXPECT"
31
assert_interactive_notroot() {
32
if [ `whoami` != 'root' ] ; then
33
assert_interactive "$@"
37
assert_interactive() {
39
# assert_interactive {msg} [y|n|C|s varname]
41
# msg is the text to print.
42
# y -> expect y for [y/n]
43
# n -> expect n for [y/n]
44
# s -> save y or n into varname. Then, (presumably) $varname can be
45
# given in place of y or n in subsequent calls to assert_interactive
46
# C -> cleanup msg. Always print "msg [enter to continue]" despite test failure.
47
# if no argument is given after msg, print "msg [enter to continue]"
51
# It seems valuable to see what happens even if the test has failed.
52
# (eg fail on stdout.)
53
if [ "$TEST_STATUS" = 'FAIL' -a -z "$XDG_UTILS_DEBUG_LEVEL" -a "$expect" != C ] ; then
54
## Don't waste user's time if test has already failed.
55
test_infoline "Test has already failed. Not bothering to ask '$query'"
59
if [ ! -z "$XDG_TEST_NO_INTERACTIVE" ] ; then
60
test_infoline "Assumed '$query' is '$expect'"
64
if [ ! -z "$expect" -a "$expect" != C ] ; then
65
if [ "$expect" != y -a "$expect" != n -a "$expect" != s -a "$expect" != C ] ; then
66
echo "TEST SYNTAX ERROR: interactive assertions require one of (y,n,s,C) as choices. (found '$expect')" >&2
70
while [ "$result" != y -a "$result" != n ] ; do
71
echo -ne "\n\t$query [y/n]: " >&2
75
if [ "$expect" = s ] ; then
77
echo "TEST SYNTAX ERROR: 's' requires a variable name"
81
elif [ "$result" != "$expect" ] ; then
82
test_fail "User indicated '$result' instead of '$expect' in respnonse to '$query'"
85
echo -ne "\n\t$query [enter to continue] " >&2
91
assert_file_in_path() {
92
search_dirs=`echo "$2" | tr ':' ' '`
93
found_files=`find $search_dirs -name "$1" 2>/dev/null`
95
if [ -z "$found_files" ] ; then
96
test_fail "Did not find '$1' in '$2'"
100
assert_file_not_in_path() {
101
search_dirs=`echo "$2" | tr ':' ' '`
102
found_files=`find $search_dirs -name "$1" 2>/dev/null`
104
if [ ! -z "$found_files" ] ; then
105
test_fail "Found '$found_files' in $2"
111
if [ ! -e "$1" ] ; then
112
test_fail "'$1' does not exist"
114
elif [ ! -f "$1" ] ; then
115
test_fail "'$1' is not a regular file"
118
if [ -f "$2" ] ; then
119
compare=`diff -wB "$1" "$2"`
120
if [ ! -z "$compare" ] ; then
121
test_fail "'$1' is different from '$2'. Diff is:\n$compare"
127
if [ -e "$1" ] ; then
128
test_fail "'$1' exists."
133
assert_nostdout() # check that nothing went to stdout
137
test_infoline "Unexpected output from '$LASTCOMMAND' written to stdout, as shown below:"
138
infofile out.stdout stdout:
143
assert_nostderr() # check that nothing went to stderr
145
if [ ! -z "$XDG_UTILS_DEBUG_LEVEL" ] ; then
146
test_infoline "not checking STDERR from '$LASTCOMMAND' because XDG_UTILS_DEBUG_LEVEL=$XDG_UTILS_DEBUG_LEVEL"
147
test_infoline out.stderr stderr:
148
elif [ -s out.stderr ] ; then
149
test_infoline "Unexpected output from '$LASTCOMMAND' written to stderr, as shown below:"
150
infofile out.stderr stderr:
155
assert_stderr() # check that stderr matches expected error
157
# $1 is file containing regexp for expected error
158
# if no argument supplied, just check out.stderr is not empty
160
if [ ! -s out.stderr ]
162
test_infoline "Expected output from '$LASTCOMMAND' to stderr, but none written"
166
if [ ! -z "$1" ] ; then
169
exec 4<&0 0< "$expfile" 3< out.stderr
174
if expr "$line" : "$expline" > /dev/null
188
test_infoline "Incorrect output from '$LASTCOMMAND' written to stderr, as shown below"
189
infofile "$expfile" "expected stderr:"
190
infofile out.stderr "received stderr:"
196
assert_stdout() # check that stderr matches expected error
198
# $1 is file containing regexp for expected error
199
# if no argument supplied, just check out.stderr is not empty
201
if [ ! -s out.stdout ]
203
test_infoline "Expected output from '$LASTCOMMAND' to stdout, but none written"
207
if [ ! -z "$1" ] ; then
210
if [ ! -e "$expfile" ] ; then
211
test_status NORESULT "Could not find file '$expfile' to look up expected pattern!"
215
exec 4<&0 0< "$expfile" 3< out.stdout
220
if expr "$line" : "$expline" > /dev/null
234
test_infoline "Incorrect output from '$LASTCOMMAND' written to stdout, as shown below"
235
infofile "$expfile" "expected stdout:"
236
infofile out.stdout "received stdout:"
243
infofile() # write file to journal using tet_infoline
245
# $1 is file name, $2 is prefix for tet_infoline
250
test_infoline "$prefix$line"
254
require_interactive() {
255
if [ ! -z "$XDG_TEST_NO_INTERACTIVE" ] ; then
256
test_result UNTESTED "XDG_TEST_NO_INTERACTIVE is set, but this test needs interactive"
261
if [ `whoami` != 'root' ] ; then
262
test_result UNTESTED "not running as root, but test requires root privileges"
267
if [ `whoami` = 'root' ] ; then
268
test_result UNTESTED "running as root, but test must be run as a normal user"
277
if [ -z "$DISPLAY" ] ; then
278
test_fail "DISPLAY not set!"
283
if [ "x$XDGUTIL" = x ]; then
284
test_fail "XDGUTIL variable not set"