summaryrefslogtreecommitdiff
path: root/shcmdtest
blob: 77277a8dd4e2b0b7b78ea1548732692ab01b8b60 (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
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
#!/bin/sh

TESTS=""
ONCE=""

for D in "$@"; do
    for TEST in "$D/"*.script; do
	if test "x${ONCE}" = "x"; then
	    ONCE=1
	    set -- "${TEST}"
	else
	    set -- "$@" "${TEST}"
	fi
    done
done

OLD_DIR=""
LOCALTMPDIR=$(mktemp -d)
DATADIR="${LOCALTMPDIR}/data"
SRCDIR=$(pwd)

export DATADIR SRCDIR

mkdir "${DATADIR}"
blow_away () { rm -fr "${LOCALTMPDIR}"; }
trap blow_away 0

TEST_COUNT=0
PASSES=0
FAILS=0

teardown_once () {
    test -x "$1/teardown-once" && "$1/teardown-once" >/dev/null 2>/dev/null
    rm -fr "${DATADIR}"
    mkdir "${DATADIR}"
}

setup_once () {
    test -d "${DATADIR}" || mkdir "${DATADIR}"
    test -x "$1/setup-once" && "$1/setup-once" >/dev/null 2>/dev/null
}

setup_for () {
    test -x "$1/setup" && "$1/setup" >/dev/null 2>/dev/null
    test -x "$1/$2.setup" && "$1/$2.setup" >/dev/null 2>/dev/null
}

teardown_for () {
    test -x "$1/$2.teardown" && "$1/$2.teardown" >/dev/null 2>/dev/null
    test -x "$1/teardown" && "$1/teardown" >/dev/null 2>/dev/null
}

FAILMSGS=""

pass_fail () {
    TEST_COUNT=$(($TEST_COUNT + 1))
    if test "x$1" = "x0"; then
	PASSES=$(($PASSES + 1))
    else
	FAILS=$(($FAILS + 1))
	THIS_FAIL=1
	if test "x$4" != "x"; then
	    FAILMSGS="${FAILMSGS}\n$(cat $4)"
	else
	    FAILMSGS="${FAILMSGS}\n$2: $3\n"
	fi
    fi
}

runtest () {
    THIS_FAIL=0
    TESTNAME="$2"
    export TESTNAME
    if test -r "$1/$2.stdin"; then
	# Run the test with stdin
	"$1/$2.script" < "$1/$2.stdin" > "$1/$2.stdout-actual" 2> "$1/$2.stderr-actual"
	TEST_EXIT=$?
    else
	"$1/$2.script" < /dev/null > "$1/$2.stdout-actual" 2> "$1/$2.stderr-actual"
	TEST_EXIT=$?
    fi
    WANT_EXIT=$(test -r "$1/$2.exit" && cat "$1/$2.exit")
    WANT_EXIT=${WANT_EXIT:-0}
    test "x$TEST_EXIT" = "x$WANT_EXIT"
    pass_fail "$?" "$1/$2" "exit code $TEST_EXIT is not $WANT_EXIT"
    if test -r "$1/$2.stdout"; then
	diff -u "$1/$2.stdout" "$1/$2.stdout-actual" > "$1/$2.stdout-diff"
	pass_fail "$?" "$1/$2" "stdout differs"
    else
	diff -u /dev/null "$1/$2.stdout-actual" > "$1/$2.stdout-diff"
	pass_fail "$?" "$1/$2" "stdout differs"  "$1/$2.stdout-diff"
    fi
    if test -r "$1/$2.stderr"; then
	diff -u "$1/$2.stderr" "$1/$2.stderr-actual" > "$1/$2.stderr-diff"
	pass_fail "$?" "$1/$2" "stderr differs" "$1/$2.stderr-diff"
    else
	diff -u /dev/null "$1/$2.stderr-actual" > "$1/$2.stdout-diff"
	pass_fail "$?" "$1/$2" "stderr differs"
    fi
    if test "x$THIS_FAIL" = "x0"; then
	rm -f "$1/$2.stdout-actual" "$1/$2.stderr-actual"
	rm -f "$1/$2.stdout-diff" "$1/$2.stderr-diff"
    fi
}

ONCE=""
for T in "$@"; do
    if test "x${ONCE}" = "x"; then
	ONCE=1
	set --
    fi
    CUR_DIR=$(dirname "$T")
    if test "x${OLD_DIR}" != "x${CUR_DIR}"; then
	test "x${OLD_DIR}" != "x" && teardown_once "${OLD_DIR}"
	setup_once "${CUR_DIR}"
	OLD_DIR="${CUR_DIR}"
    fi
    TEST_NAME=$(basename "$T" .script)
    setup_for "${CUR_DIR}" "${TEST_NAME}"
    runtest "${CUR_DIR}" "${TEST_NAME}"
    teardown_for "${CUR_DIR}" "${TEST_NAME}"
done

test "x${OLD_DIR}" != "x" && teardown_once "${OLD_DIR}"

echo "${PASSES} / ${TEST_COUNT}"
echo "${FAILMSGS}"

for F in "$@"; do
    echo "$F:"
    cat "$F"
done