Merge: doc: fixed some typos and other misc. corrections
[nit.git] / misc / jenkins / unitrun.sh
1 #!/bin/bash
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # This shell script executes a shell command and generates a JUnit compatible XML file
17
18 if test $# -lt 2; then
19 echo "Usage: unitrun.sh name command"
20 echo "Runs the command and generates a JUnit XML file name.xml"
21 exit
22 fi
23
24 name=$1
25 shift
26
27 # Detect a working time command
28 if command time --quiet -f%e true 2>/dev/null; then
29 TIME="command time --quiet -f%e -o ${name}.t.out"
30 elif command time -f%e true 2>/dev/null; then
31 TIME="command time -f%e -o ${name}.t.out"
32 elif command gtime -f%e true 2>/dev/null; then
33 TIME="command gtime -f%e -o ${name}.t.out"
34 else
35 TIME=
36 fi
37
38 # Detect a working date command
39 if date -Iseconds >/dev/null 2>&1; then
40 TIMESTAMP="timestamp='`date -Iseconds`'"
41 else
42 TIMESTAMP=
43 fi
44
45 # Magic here! This tee and save both stdout and stderr in distinct files without messing with them
46 # Time just get the user time
47 $TIME "$@" > >(tee "${name}.out") 2> >(tee "${name}.2.out" >&2)
48 res=$?
49
50 c=`echo "${name%-*}" | tr "-" "."`
51 n=${name##*-}
52
53 # Do we have a time result?
54 if test -f "${name}.t.out"; then
55 T="time='`cat "${name}.t.out"`'"
56 else
57 T=
58 fi
59
60 cat > "${name}.xml"<<END
61 <testsuites><testsuite>
62 <testcase classname='$c' name='$n' $T $TIMESTAMP>
63 END
64 if test "$res" != "0"; then
65 echo >> "${name}.xml" "<error message='Command returned $res'/>"
66 echo "+ Command returned $res" >&2
67 fi
68 if test -s "${name}.out"; then
69 cat >> "${name}.xml"<<END
70 <system-out><![CDATA[
71 `cat -v ${name}.out`
72 ]]></system-out>
73 END
74 fi
75 if test -s "${name}.2.out"; then
76 cat >> "${name}.xml"<<END
77 <system-err><![CDATA[
78 `cat -v ${name}.2.out`
79 ]]></system-err>
80 END
81 fi
82 cat >> "${name}.xml"<<END
83 </testcase>
84 </testsuite></testsuites>
85 END
86
87 rm "${name}.out" "${name}.2.out" "${name}.t.out" 2> /dev/null || true