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
#!/bin/bash --
usage="tester.sh --main-cmd '<cmd>' --tester-cmd '<cmd>' <test_name> [--log-dir '<dir>']"
POSITIONAL=()
while [ $# -gt 0 ]; do
key="$1"; shift
case $key in
--main-cmd)
main_cmd="$1"; shift
;;
--tester-cmd)
tester_cmd="$1"; shift
;;
--log-dir)
log_dir="$1"; shift
;;
--err-code)
err_code="$1"; shift
;;
-h|--help)
echo $usage
exit
;;
*)
POSITIONAL+=("$key")
;;
esac
done
set -- "${POSITIONAL[@]}"
test_name=$1
# Ignoring everything that is left
if [ -z "$main_cmd" ] || [ -z "$tester_cmd" ]; then
echo "Error: --main-cmd and --tester-cmd are required." 1>&2
echo $usage 1>&2
exit 1
fi
if [ -z "$test_name" ]; then
echo "Error: at least one positional argument is required." 1>&2
echo $usage 1>&2
exit 1
fi
if [ -z "$log_dir" ]; then
log_out=/dev/stdout
log_err=/dev/stderr
else
mkdir -p "$log_dir"
log_out="$log_dir/${test_name}_out.log"
log_err="$log_dir/${test_name}_err.log"
fi
echo "********************************************************************************"
echo "$test_name: Started"
$main_cmd >"$log_out" 2>"$log_err"
rc=$?
if [ "$rc" -ne "$err_code" ]; then
echo
echo "Failed at creating output. Command: $main_cmd"
if [ ! -z "$log_dir" ]; then
[ -s "$log_out" ] && echo "*** stdout log: $log_out ***" && cat "$log_out"
[ -s "$log_err" ] && echo "*** stderr log: $log_err ***" && cat "$log_err"
fi
echo
echo "$test_name: Failed with exit code $rc"
echo "********************************************************************************"
exit $rc
fi
if [ "$rc" -eq "0" ]; then
$tester_cmd >$log_out 2>$log_err
rc=$?
if [ "$rc" -ne "0" ]; then
echo
echo "Actual and expected results differ. Command: $tester_cmd"
if [ ! -z "$log_dir" ]; then
test -s "$log_out" && echo "*** stdout log: $log_out ***" && cat $log_out
test -s "$log_err" && echo "*** stderr log: $log_err ***" && cat $log_err
fi
echo
echo "$test_name: Failed with exit code $rc"
echo "********************************************************************************"
exit $rc
fi
fi
echo "$test_name: Finished successfully"
echo "********************************************************************************"