Merge: share/libgc: option to use a local version of the source pkgs
[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 env time --quiet -f%U true 2>/dev/null; then
29 TIME="env time --quiet -f%U -o ${name}.t.out"
30 elif env time -f%U true 2>/dev/null; then
31 TIME="env time -f%U -o ${name}.t.out"
32 else
33 TIME=
34 fi
35
36 # Detect a working date command
37 if date -Iseconds >/dev/null 2>&1; then
38 TIMESTAMP="timestamp='`date -Iseconds`'"
39 else
40 TIMESTAMP=
41 fi
42
43 # Magic here! This tee and save both stdout and stderr in distinct files without messing with them
44 # Time just get the user time
45 $TIME "$@" > >(tee "${name}.out") 2> >(tee "${name}.2.out" >&2)
46 res=$?
47
48 c=`echo "${name%-*}" | tr "-" "."`
49 n=${name##*-}
50
51 # Do we have a time result?
52 if test -f "${name}.t.out"; then
53 T="time='`cat "${name}.t.out"`'"
54 else
55 T=
56 fi
57
58 cat > "${name}.xml"<<END
59 <testsuites><testsuite>
60 <testcase classname='$c' name='$n' $T $TIMESTAMP>
61 END
62 if test "$res" != "0"; then
63 echo >> "${name}.xml" "<error message='Command returned $res'/>"
64 echo "+ Command returned $res" >&2
65 fi
66 cat >> "${name}.xml"<<END
67 <system-out><![CDATA[
68 `cat -v ${name}.out`
69 ]]></system-out>
70 <system-err><![CDATA[
71 `cat -v ${name}.2.out`
72 ]]></system-err>
73 </testcase>
74 </testsuite></testsuites>
75 END
76
77 rm "${name}.out" "${name}.2.out" "${name}.t.out" 2> /dev/null || true