-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp-runtests.sh
executable file
·173 lines (148 loc) · 4.37 KB
/
p-runtests.sh
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
SCRIPTNAME=${0##*/}
function showHelp {
echo "Command: $SCRIPTNAME [OPTIONS]... [FILES]..."
echo "Runs test-cases of given [FILES], compares the output of a method with an expected-output file."
echo "Returns SUCCEEDED for outputs that are equal to the expected file content."
echo "Needed: A input file (<something>.txt) and an expected output file (<something>_expected.txt),"
echo " where <something> must be the same string."
echo
echo "OPTIONS:"
echo " -h, --help Show this help message"
echo " -v, --verbose Prints additional information, if a test fails"
echo " -i, --ignore <pattern> Ignore lines with a certain <pattern>"
echo " -c, --command <command> Executes a command and compares its result with a given expected result file"
echo " -s, --silent Prints only minimal info, i.e., pass or fail"
echo " -k, --keepinfo Keep all results, and accumulate them (not implemented yet)"
}
# Each short option character in shortopts may be followed by one colon to indicate it has a required
# argument, and by two colons to indicate it has an optional argument.
TEMP=$(getopt -o hvski:c: --long help,verbose,silent,ignore:,command:,keepinfo -n $SCRIPTNAME -- "$@")
if [ $? != 0 ] ; then echo "$SCRIPTNAME: Parameter parsing failed (getopt). Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
KEEP=false
SILENT=false
VERBOSE=false
DEBUGFILE=
CMD=
while true; do
case "$1" in
-h | --help )
showHelp
exit 0
;;
-v | --verbose )
$SILENT && { echo "$SCRIPTNAME: Verbose and silent can not be given at the same time! Terminating..." >&2; exit 1; }
VERBOSE=true
shift
;;
-i | --ignore )
IGNORESTRING="$2"
shift 2
;;
-c | --command )
CMD="$2"
shift 2
;;
-s | --silent )
$VERBOSE && { echo "$SCRIPTNAME: Verbose and silent can not be given at the same time! Terminating..." >&2; exit 1; }
SILENT=true
shift
;;
-k | --keepinfo )
KEEP=true
shift
;;
--debugfile )
DEBUGFILE="$2"
shift 2
;;
-- )
shift
break
;;
* )
echo "'$SCRIPTNAME -h' gives you more information."
exit 1
;;
esac
done
function outVerbose {
$VERBOSE && echo "$1" >&2
}
ERRCOUNT=0
TESTCOUNT=0
TMPFILE1="/tmp/$SCRIPTNAME-stdout.$$.tmp"
TMPFILE2="/tmp/$SCRIPTNAME-stderr.$$.tmp"
function diffCmd {
DIFFCMD="diff -Bbc --suppress-common-lines "
if test -n "$IGNORESTRING"; then
outVerbose "Ignoring '$IGNORESTRING' while comparing..."
echo "IGNORING does not work currently: Not implemented!"
exit 1
DIFFCMD="$DIFFCMD -I $IGNORESTRING"
fi
DIFFOUT=$($SILENT && echo /dev/null || echo /dev/stdout)
if test -n "$CMD"; then
$CMD $2 > $TMPFILE1 2> $TMPFILE2
OUT=$?
if test $OUT -ne 0; then
echo "ERROR: UNABLE TO EXECUTE TESTS..."
printf "Command '$CMD' failed with error-code '$OUT' and the following output:\n"
printf "STDOUT output-----------------------------------------\n"
cat $TMPFILE1
printf "\n------------------------------------------------------\n"
printf "STDERR output-----------------------------------------\n"
cat $TMPFILE2
printf "\n------------------------------------------------------\n"
exit 1
fi
cat $TMPFILE1 | $DIFFCMD $1 - > $DIFFOUT
return $?
fi
$DIFFCMD $1 $2 > $DIFFOUT
return $?
}
$SILENT || {
echo "Starting tests..."
echo
}
for ARG in "$@"
do
if [[ ! "$ARG" =~ "_expected.txt" ]]; then
outVerbose "-----------------------------------------------------------------------------"
if [ -f "${ARG%.*}_expected.txt" ]; then
let TESTCOUNT+=1
outVerbose "TESTING: $ARG"
diffCmd ${ARG%.*}_expected.txt "$ARG" >&2
PASSED=$?
if [ $PASSED -eq 0 ]; then
echo "TEST SUCCEEDED: $ARG"
else
echo "TEST FAILED : $ARG"
let ERRCOUNT+=1
fi
else
outVerbose "TEST SKIPPED: '$ARG' (no EXPECTED RESULT file)"
fi
fi
done
if test $TESTCOUNT -eq 0; then
echo "Error: No test files given."
showHelp
exit 0
fi
outVerbose "-----------------------------------------------------------------------------"
$SILENT || {
echo
echo "...done!"
echo
test $ERRCOUNT -eq 0 && printf "ALL TESTS PASSED!\n\n" || printf "ERRORS REPORTED!\n\n"
echo "Details:"
printf " $TESTCOUNT tests executed\n"
printf " $ERRCOUNT tests failed\n"
printf " $[TESTCOUNT-ERRCOUNT] tests passed\n"
echo
}
exit 0