summaryrefslogtreecommitdiff
path: root/shcmdtest
blob: cf98abecd8eb32550da75bd4fba4acd202e6853b (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh

TESTS=""
ONCE=""

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

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

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

SCRIPTS_RUN=0
TEST_COUNT=0
PASSES=0
FAILS=0

envme () {
    env DATADIR="${DATADIR}" SRCDIR="${SRCDIR}" TESTNAME="${TEST_NAME}" "$@"
}

teardown_once () {
    test -x "$1/teardown-once" && envme "$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" && envme "$1/setup-once" >/dev/null 2>/dev/null
}

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

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


clear_line () {
    SPACES=$((${#LONGEST_NAME} + ${#SCRIPTS_RUN} + ${#SCRIPT_COUNT} + 3))
    while test $SPACES -gt 0; do
	echo -n " "
	SPACES=$(($SPACES - 1))
    done
    echo -n -e '\r'
}

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

LONGEST_NAME=0
runtest () {
    THIS_FAIL=0
    SCRIPTS_RUN=$(($SCRIPTS_RUN + 1))
    echo -n "${SCRIPTS_RUN}/${SCRIPT_COUNT}: ${TEST_NAME}"
    TEST_LEN=${#TEST_NAME}
    if test $TEST_LEN -gt $LONGEST_NAME; then
	LONGEST_NAME=$TEST_LEN
    else
	SPACES=$(($LONGEST_NAME - $TEST_LEN))
	while test $SPACES -gt 0; do
	    echo -n " "
	    SPACES=$(($SPACES - 1))
	done
    fi
    echo -n -e '\r'
    if test -r "$1/$2.stdin"; then
	# Run the test with stdin
	envme "$1/$2.script" < "$1/$2.stdin" \
	    > "$1/$2.stdout-actual" 2> "$1/$2.stderr-actual"
	TEST_EXIT=$?
    else
	envme "$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" "$1/$2.stdout-diff"
    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" "$1/$2.stderr-diff"
    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
}

for T in "$@"; do
    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}"

clear_line

echo "Totals: ${SCRIPTS_RUN} tests run, comprising ${TEST_COUNT} assertions."
echo "Of those, ${PASSES} passed and ${FAILS} failed."