Skip to content

Instantly share code, notes, and snippets.

@cnlpete
Forked from eschmidscs/sh2ju.sh
Created October 19, 2021 11:58
Show Gist options
  • Save cnlpete/557f0854612a79c9d463760e25229148 to your computer and use it in GitHub Desktop.
Save cnlpete/557f0854612a79c9d463760e25229148 to your computer and use it in GitHub Desktop.
junit bash commands
#!/bin/bash
### Copyright 2010 Manuel Carrasco Moñino. (manolo at apache.org)
### Copyright 2016 Patrick Double (pat at patdouble.com)
###
### 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 Hudson, or any other CI.
###
### Usage:
### - source 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 hudson to parse junit files from the generated folder
###
asserts=0; assertstr="00"; errors=0; total=0; content=""
# create output folder
juDIR=${CIRCLE_TEST_REPORTS:-$(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 $? | tr -d "\n" >$errfile
}
# Method to clean old tests
juLogClean() {
echo "+++ Removing old junit reports from: $juDIR "
rm -f "$juDIR"/TEST-*
}
# Execute a command and record its results
juLog() {
stored_options=$-
set +ex
# parse arguments
local ya="" icase="" name="" ereg="" icase="" cmd=""
while [ -z "$ya" ]; do
case "$1" in
-name=*) name=$assertstr-${1//-name=/}; shift;;
-ierror=*) ereg=${1//-ierror=/}; icase="-i"; shift;;
-error=*) ereg=${1//-error=/}; shift;;
*) ya=1;;
esac
done
# use first arg as name if it was not given
if [ -z "$name" ]; then
name="$assertstr-$1"
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
touch $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
ini=$(date +%s)
[[ $stored_options == *"x"* ]] && set -x
eVal "$cmd" 2>&1 | tee -a $outf
evErr=$(cat $errfile)
set +x
rm -f $errfile
end=$(date +%s)
echo "+++ exit code: $evErr" | tee -a $outf
# set the appropriate error, based in the exit code and the regex
err=1
if [[ "$evErr" == 0 ]]; then
err=0
fi
out=$(sed -e 's/^\([^+]\)/| \1/g' $outf)
if [[ $err == 0 && -n "$ereg" ]]; then
H=$(echo "$out" | grep -E $icase "$ereg")
[ -n "$H" ] && err=1
fi
echo "+++ error: $err" | tee -a $outf
rm -f $outf
# calculate vars
echo "Asserts is ${asserts}"
asserts=$((asserts + 1))
assertstr=$(printf "%.2d" $asserts)
errors=$((errors + err))
time=$((end - ini))
total=$((total + time))
# write the junit xml report
## failure tag
[ $err = 0 ] && failure="" || failure="
<failure type=\"ScriptError\" message=\"Script Error\">
<![CDATA[
$out
]]>
</failure>
"
## testcase tag
content="$content
<testcase assertions=\"1\" name=\"$name\" time=\"$time\">
$failure
<system-out>
<![CDATA[
$out
]]>
</system-out>
</testcase>
"
## testsuite block
cat <<EOF > "$juDIR/TEST-$suite.xml"
<testsuite failures="0" assertions="$assertstr" name="$suite" tests="1" errors="$errors" time="$total">
$content
</testsuite>
EOF
set -$stored_options
return "$evErr"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment