54d875c8e3e21a6104d3e7771d188bc231a65859
[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 # Magic here! This tee and save both stdout and stderr in distinct files without messing with them
28 # Time just get the user time
29 /usr/bin/time -f%U --quiet -o "${name}.t.out" "$@" > >(tee "${name}.out") 2> >(tee "${name}.2.out" >&2)
30 res=$?
31
32 c=`echo "${name%-*}" | tr "-" "."`
33 n=${name##*-}
34
35 cat > "${name}.xml"<<END
36 <testsuites><testsuite>
37 <testcase classname='$c' name='$n' time='`cat "${name}.t.out"`' timestamp='`date -Iseconds`'>
38 END
39 if test "$res" != "0"; then
40 echo >> "${name}.xml" "<error message='Command returned $res'/>"
41 echo "+ Command returned $res" >&2
42 fi
43 cat >> "${name}.xml"<<END
44 <system-out><![CDATA[
45 `cat -v ${name}.out`
46 ]]></system-out>
47 <system-err><![CDATA[
48 `cat -v ${name}.2.out`
49 ]]></system-err>
50 </testcase>
51 </testsuite></testsuites>
52 END
53
54 rm "${name}.out" "${name}.2.out" "${name}.t.out"