forked from manolo/shell2junit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsh2ju.sh
executable file
·156 lines (140 loc) · 4.57 KB
/
sh2ju.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
#!/bin/bash
### Copyright 2010 Manuel Carrasco Moñino. (manolo at apache.org)
###
### Licensed under the Apache License, Version 2.0.
### You may obtain a copy of it at
### http://www.apache.org/licenses/LICENSE-2.0
###
### A library for shell scripts which creates reports in jUnit format.
### These reports can be used in Jenkins, or any other CI.
###
### Usage:
### - Include this file in your shell script
### - Use juLog to call your command any time you want to produce a new report
### Usage: juLog <options> command arguments
### options:
### -name="TestName" : the test name which will be shown in the junit report
### -error="RegExp" : a regexp which sets the test as failure when the output matches it
### -ierror="RegExp" : same as -error but case insensitive
### - Junit reports are left in the folder 'result' under the directory where the script is executed.
### - Configure Jenkins to parse junit files from the generated folder
###
tests=0; failures=0; errors=0; totaltime=0; content=""
date=$(which gdate || which date) 2>/dev/null
# create output folder
juDIR="$(pwd)/results"
mkdir -p "$juDIR" || exit
# The name of the suite is calculated based in your script name
suite=$(basename "$0" | sed -e 's/.sh$//' | tr "." "_")
# A wrapper for the eval method which allows catching seg-faults and use tee
errfile=/tmp/evErr.$$.log
eVal() {
eval "\"$1\""
echo $? 2>&1 | tr -d "\n" > $errfile
}
# Method to clean old tests
juLogClean() {
echo "+++ Removing old junit reports from: $juDIR "
rm -f "$juDIR"/TEST-*
# Reset variables
tests=0
failures=0
errors=0
totaltime=0
content=""
}
# Execute a command and record its results
juLog() {
# parse arguments
ya=""; icase=""
while [ -z "$ya" ]; do
case "$1" in
-name=*) name=$(printf "%.2d" $tests)-$(echo "$1" | sed -e 's/-name=//'); shift;;
-ierror=*) ereg=`echo "$1" | sed -e 's/-ierror=//'`; icase="-i"; shift;;
-error=*) ereg=`echo "$1" | sed -e 's/-error=//'`; shift;;
*) ya=1;;
esac
done
# use first arg as name if it was not given
if [ -z "$name" ]; then
name="$(printf "%.2d" $tests)-$1"
shift
fi
# calculate command to eval
[ -z "$1" ] && return
cmd="$1"; shift
while [ -n "$1" ]
do
cmd="$cmd \"$1\""
shift
done
# eval the command sending output to a file
outf=/var/tmp/ju$$.txt
errf=/var/tmp/ju$$-err.txt
>$outf
echo | tee -a $outf
echo "+++ Running case: $name" | tee -a $outf
echo "+++ working dir: $(pwd)" | tee -a $outf
echo "+++ command: $cmd" | tee -a $outf
starttime=$($date +%s.%N)
((eVal "$cmd" | tee -a $outf) 3>&1 1>&2 2>&3 | tee $errf) 3>&1 1>&2 2>&3
exitcode=$([ -e "$errfile" ] && cat "$errfile" || echo "0")
rm -f $errfile
endtime=$($date +%s.%N)
echo "+++ exit code: $exitcode" | tee -a $outf
# set the appropriate error, based in the exit code and the regex
[ $exitcode != 0 ] && err=1 || err=0
out=$(cat $outf | sed -e 's/^\([^+]\)/| \1/g')
if [ $err = 0 -a -n "$ereg" ]; then
H=$(echo "$out" | egrep $icase "$ereg")
[ -n "$H" ] && err=1
fi
echo "+++ error: $err" | tee -a $outf
rm -f $outf
errMsg=$([ -e "$errf" ] && cat "$errf" || echo "")
rm -f $errf
[ $err = 0 -a ! -z "$errMsg" ] && failed=1 || failed=0
# calculate vars
tests=$(($tests + 1))
failures=$(($failures + $failed))
errors=$(($errors + $err))
time=$(echo "$endtime - $starttime" | bc -l)
totaltime=$(echo "$totaltime + $time" | bc -l)
# write the junit xml report
## failure tag
[ $failed = 0 ] && failure="" || failure="
<failure type=\"ScriptError\" message=\"Script Error\"><![CDATA[$errMsg]]></failure>
"
## error tag
[ $err = 0 ] && error="" || error="
<error type=\"ScriptError\" message=\"Script Error\"><![CDATA[$errMsg]]></error>
"
## testcase tag
content="$content
<testcase name=\"$name\" status=\"run\" time=\"$time\" >
$failure
$error
<system-out>
<![CDATA[
$out
]]>
</system-out>
<system-err>
<![CDATA[
$errMsg
]]>
</system-err>
</testcase>
"
local formatted_date="$($date -Iseconds | cut -d+ -f1)"
## testsuite block
cat <<EOF > "$juDIR/TEST-$suite.xml"
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="$tests" failures="$failures" disabled="0" errors="$errors" timestamp="$formatted_date" time="$totaltime" name="AllTests">
<testsuite name="$suite" tests="$tests" failures="$failures" errors="$errors" time="$totaltime" timestamp="$formatted_date">
$content
</testsuite>
</testsuites>
EOF
return $exitcode
}