summaryrefslogtreecommitdiffstats
path: root/tests/ipcalc-testrunner.sh
blob: d5bc2fd38fc36fcd02468d1c3b7b6e24bb9d783f (plain)
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
#!/bin/sh
#
# This shell script is a simple test runner for ipcalc tests.
#
# Adapted from: Matej Susta <msusta@redhat.com>
#
#   Copyright (c) 2009 Red Hat, Inc. All rights reserved.
#
#   This copyrighted material is made available to anyone wishing
#   to use, modify, copy, or redistribute it subject to the terms
#   and conditions of the GNU General Public License version 2.
#
#   This program 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 this program; if not, write to the Free
#   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301, USA.

exitcode=0

ok() {
	echo "ok."
}

fail() {
	echo "FAILED!"
	exitcode=$((exitcode+1))
	echo -e "Output was:\n$1"
}

TestSuccess() {
	echo -n "Checking $@... "
	output=$(sh -c "$1" 2>&1)
	rc=$?
	[ $rc -eq 0 ] && ok || fail $output
}

TestFailure() {
	echo -n "Checking $@... "
	output=$(sh -c "$1" 2>&1)
	rc=$?
	[ $rc -eq 0 ] && fail $output || ok
}

TestOutput() {
	echo -n "Checking $1... "
	output=$(sh -c "$1" 2>&1)
	rc=$?
	[ "$output" = "$2" ] && ok || fail $output
}

TestOutputFile() {
	echo -n "Reading $2... "
	[ -e "$2" ] || { fail "missing file $2"; return; }
	contents="$(cat "$2" 2>/dev/null)"
	[ -n "$contents" ] && ok || { fail "failed to read $2"; return; }
	TestOutput "$1" "$contents"
}

TestEqual() {
	TestSuccess "$1"
	[ -n "$output" ] && output1="$output" || { fail "no output from $1"; return; }
	TestSuccess "$2"
	[ -n "$output" ] && output2="$output" || { fail "no output from $2"; return; }
	echo -n "Comparing output... "
	[ "$output1" = "$output2" ] && ok || fail "$output1 <> $output2"
}

while [ $# -gt 0 ]; do
	case $1 in
		--test-success) TestSuccess "$2"; shift ;;
		--test-failure) TestFailure "$2"; shift ;;
		--test-output)  TestOutput "$2" "$3"; shift; shift ;;
		--test-outfile) TestOutputFile "$2" "$3"; shift; shift ;;
		--test-equal)   TestEqual "$2" "$3"; shift; shift ;;
		*) fail "invalid argument: $1" ;;
	esac
	shift
done

echo "$exitcode test(s) failed."
exit $exitcode