fork sh2ju to accommendate existing testing tools

This commit is contained in:
Minhan Xia 2016-06-22 16:40:23 -07:00
parent ca1e808c2f
commit ec3f0e9f07

View File

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/bash
### Copyright 2010 Manuel Carrasco Moñino. (manolo at apache.org) ### Copyright 2010 Manuel Carrasco Moñino. (manolo at apache.org)
### ###
### Licensed under the Apache License, Version 2.0. ### Licensed under the Apache License, Version 2.0.
@ -18,6 +18,7 @@
### -name="TestName" : the test name which will be shown in the junit report ### -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 ### -error="RegExp" : a regexp which sets the test as failure when the output matches it
### -ierror="RegExp" : same as -error but case insensitive ### -ierror="RegExp" : same as -error but case insensitive
### -output="Path" : path to output directory, defaults to "./results"
### - Junit reports are left in the folder 'result' under the directory where the script is executed. ### - 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 ### - Configure Jenkins to parse junit files from the generated folder
### ###
@ -25,29 +26,28 @@
asserts=00; errors=0; total=0; content="" asserts=00; errors=0; total=0; content=""
date=`which gdate 2>/dev/null || which date` date=`which gdate 2>/dev/null || which date`
# create output folder # default output folder
juDIR=`pwd`/results juDIR=`pwd`/results
mkdir -p "$juDIR" || exit
# The name of the suite is calculated based in your script name # The name of the suite is calculated based in your script name
suite="" suite=""
# A wrapper for the eval method witch allows catching seg-faults and use tee # A wrapper for the eval method witch allows catching seg-faults and use tee
errfile=/tmp/evErr.$$.log errfile=/tmp/evErr.$$.log
eVal() { function eVal() {
eval "$1" (eval "$1")
# stdout and stderr may currently be inverted (see below) so echo may write to stderr # stdout and stderr may currently be inverted (see below) so echo may write to stderr
echo $? 2>&1 | tr -d "\n" > $errfile echo $? 2>&1 | tr -d "\n" > $errfile
} }
# Method to clean old tests # Method to clean old tests
juLogClean() { function juLogClean() {
echo "+++ Removing old junit reports from: $juDIR " echo "+++ Removing old junit reports from: $juDIR "
rm -f "$juDIR"/TEST-* rm -f "$juDIR"/TEST-*
} }
# Execute a command and record its results # Execute a command and record its results
juLog() { function juLog() {
suite=""; suite="";
errfile=/tmp/evErr.$$.log errfile=/tmp/evErr.$$.log
date=`which gdate || which date` date=`which gdate || which date`
@ -61,10 +61,13 @@ juLog() {
-class=*) class=`echo "$1" | sed -e 's/-class=//'`; shift;; -class=*) class=`echo "$1" | sed -e 's/-class=//'`; shift;;
-ierror=*) ereg=`echo "$1" | sed -e 's/-ierror=//'`; icase="-i"; shift;; -ierror=*) ereg=`echo "$1" | sed -e 's/-ierror=//'`; icase="-i"; shift;;
-error=*) ereg=`echo "$1" | sed -e 's/-error=//'`; shift;; -error=*) ereg=`echo "$1" | sed -e 's/-error=//'`; shift;;
-output=*) juDIR=`echo "$1" | sed -e 's/-output=//'`; shift;;
*) ya=1;; *) ya=1;;
esac esac
done done
# create output directory
mkdir -p "$juDIR" || exit
# use first arg as name if it was not given # use first arg as name if it was not given
if [ -z "$name" ]; then if [ -z "$name" ]; then
name="$asserts-$1" name="$asserts-$1"
@ -75,9 +78,6 @@ juLog() {
class="default" class="default"
fi fi
echo "name is: $name"
echo "class is: $class"
suite=$class suite=$class
# calculate command to eval # calculate command to eval
@ -100,7 +100,7 @@ juLog() {
ini=`$date +%s.%N` ini=`$date +%s.%N`
# execute the command, temporarily swapping stderr and stdout so they can be tee'd to separate files, # execute the command, temporarily swapping stderr and stdout so they can be tee'd to separate files,
# then swapping them back again so that the streams are written correctly for the invoking process # then swapping them back again so that the streams are written correctly for the invoking process
((eVal "$cmd" | tee -a $outf) 3>&1 1>&2 2>&3 | tee $errf) 3>&1 1>&2 2>&3 ( (eVal "$cmd" | tee -a $outf) 3>&1 1>&2 2>&3 | tee $errf) 3>&1 1>&2 2>&3
evErr=`cat $errfile` evErr=`cat $errfile`
rm -f $errfile rm -f $errfile
end=`$date +%s.%N` end=`$date +%s.%N`
@ -119,19 +119,23 @@ juLog() {
errMsg=`cat $errf` errMsg=`cat $errf`
rm -f $errf rm -f $errf
# calculate vars # calculate vars
asserts=`expr $asserts + 1` asserts=$(($asserts+1))
errors=`expr $errors + $err` errors=$(($errors+$err))
time=`echo "$end - $ini" | bc -l` time=`echo "$end - $ini" | bc -l`
total=`echo "$total + $time" | bc -l` total=`echo "$total + $time" | bc -l`
# write the junit xml report # write the junit xml report
## failure tag ## failure tag
[ $err = 0 ] && failure="" || failure=" [ $err = 0 ] && failure="" || failure="
<failure type=\"ScriptError\" message=\"Script Error\"><![CDATA[$errMsg]]></failure> <failure type=\"ScriptError\" message=\"Script Error\">
<![CDATA[
$errMsg
]]>
</failure>
" "
## testcase tag ## testcase tag
content="$content content="$content
<testcase assertions=\"1\" name=\"$name\" time=\"$time\"> <testcase assertions=\"1\" name=\"$name\" time=\"$time\" classname=\"$class\">
$failure $failure
<system-out> <system-out>
<![CDATA[ <![CDATA[
@ -148,6 +152,11 @@ $errMsg
## testsuite block ## testsuite block
if [[ -e "$juDIR/TEST-$suite.xml" ]]; then if [[ -e "$juDIR/TEST-$suite.xml" ]]; then
# file exists. first update the failures count
failCount=`sed -n "s/.*testsuite.*failures=\"\([0-9]*\)\".*/\1/p" "$juDIR/TEST-$suite.xml"`
errors=$(($failCount+$errors))
sed -i "0,/failures=\"$failCount\"/ s/failures=\"$failCount\"/failures=\"$errors\"/" "$juDIR/TEST-$suite.xml"
sed -i "0,/errors=\"$failCount\"/ s/errors=\"$failCount\"/errors=\"$errors\"/" "$juDIR/TEST-$suite.xml"
# file exists. Need to append to it. If we remove the testsuite end tag, we can just add it in after. # file exists. Need to append to it. If we remove the testsuite end tag, we can just add it in after.
sed -i "s^</testsuite>^^g" $juDIR/TEST-$suite.xml ## remove testSuite so we can add it later sed -i "s^</testsuite>^^g" $juDIR/TEST-$suite.xml ## remove testSuite so we can add it later
@ -159,11 +168,10 @@ EOF
else else
# no file exists. Adding a new file # no file exists. Adding a new file
cat <<EOF > "$juDIR/TEST-$suite.xml" cat <<EOF > "$juDIR/TEST-$suite.xml"
<testsuite failures="0" assertions="$assertions" name="$suite" tests="1" errors="$errors" time="$total"> <testsuite failures="$errors" assertions="$assertions" name="$suite" tests="1" errors="$errors" time="$total">
$content $content
</testsuite> </testsuite>
EOF EOF
fi fi
} }