-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshunit2-xunit
executable file
·186 lines (161 loc) · 3.13 KB
/
shunit2-xunit
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
174
175
176
177
178
179
180
181
182
183
184
185
186
#! /bin/bash
# shunit2-xunit [-OPTIONS] [DIRs]
# -a ACTDIR run all suites that are children of ACTDIR
# -d debug
# -h print help message
# -o XMLFILE put xml results in XMLFILE
# -v verbose
# run shunit2 tests under all the given DIRs,
# and report results in xunit xml format.
# for reporting purposes, each DIR is considered a "test suite".
# all -test files under it are considered "test cases"
#
testcases()
{
declare d="$1"
find $d -type f -name "*_test.sh" -o -name "shunit2_test_*.sh"
}
runtest()
{
declare tf="$1" outf="$2"
declare tmpf="$LOGDIR/s2-out.tmp"
declare xstat
$DFLAG && echo "+ $t"
((SECONDS=0))
d=$(dirname $t)
t=$(basename $t)
(cd $d && ./$t) > "$tmpf" 2>&1
xstat=$?
cat "$tmpf"
if $OFLAG; then
{
printf '<testcase classname="%s" name="%s" time="%d">\n' \
"shunit2" "$t" "$SECONDS"
if [ $xstat -ne 0 ]; then
printf '</failure>\n'
cat "$tmpf"
printf '</failure>\n'
fi
printf '</testcase>\n'
} >> $outf
fi
return $xstat
}
runsuite()
{
declare name="$1"
declare cases=$(testcases "$name" | sort)
declare tests=0 errors=0 failures=0
declare outf="$LOGDIR/xml.tmp"
declare xstat
if [ -z "$cases" ]; then
# a directory with no tests will be skipped, not counted
$DFLAG && echo "===== no shunit2 tests found under $name, skipping"
return 0
fi
((NSUITES++))
$VFLAG && echo "===== running suite $name"
mkdir -p $LOGDIR
for t in $cases; do
if ! runtest "$t" "$outf"; then
FAILED=1
((failures++))
fi
((tests++))
done
if $OFLAG; then
{
printf '<testsuite name="%s" tests="%d" errors="%d" failures="%d">\n' \
"$name" "$tests" "$errors" "$failures"
cat "$outf"
printf '</testsuite>\n'
} >> $XMLFILE
fi
/bin/rm -rf $LOGDIR
[ $failures -eq 0 ]
}
usage()
{
echo 'usage: shunit2-xunit [-OPTIONS] [DIRs]'
echo ' -a ACTDIR run all suites children of ACTDIR'
echo ' -d debug'
echo ' -h print help message'
echo ' -o XMLFILE output xml to XMLFILE'
echo ' -v verbose'
echo ' DIRs directories containing shunit2 tests'
}
get_suites()
{
declare d="$1"
find "$d"/* -maxdepth 0 -type d | sed -e 's,^\./,,'
}
prog_error()
{
echo "$PROGNAME: $*" 1>&2
}
main()
{
while getopts ':a:dho:p:v' flag; do
case "$flag" in
a)
AFLAG=true
ACTDIR="$OPTARG"
;;
d)
DFLAG=true
;;
h)
usage
exit 0
;;
v)
VFLAG=true
;;
o)
XMLFILE="$OPTARG"
OFLAG=true
;;
*)
prog_error "unknown flag -$flag"
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ $# -eq 0 ]; then
AFLAG=true
ACTDIR=.
fi
if $AFLAG; then
set -- $(get_suites $ACTDIR) "$@"
fi
if $OFLAG; then
{
echo '<?xml version="1.0" encoding="UTF-8"?>'
} >> $XMLFILE
fi
for d in "$@"; do
runsuite $d || ((SUITES_FAILED++))
done
}
cleanup()
{
/bin/rm -rf $LOGDIR
}
# ----- start of mainline code
FAILED=0
PROGNAME=${0##*/}
PROGDIR=$(cd $(dirname $0) && /bin/pwd -P)
XMLFILE=
OFLAG=false
AFLAG=false
ACTDIR=
DFLAG=false
VFLAG=false
LOGDIR=/tmp/shunit2-xunit-$$
NSUITES=0
SUITES_FAILED=0
trap cleanup exit
main "$@"
printf "===== Ran $NSUITES suites, $SUITES_FAILED failed\n"
[ $SUITES_FAILED -eq 0 ]