Skip to content

Instantly share code, notes, and snippets.

@spedepekka
Created July 9, 2015 12:59
Show Gist options
  • Save spedepekka/2ca20691eafbc62ad28c to your computer and use it in GitHub Desktop.
Save spedepekka/2ca20691eafbc62ad28c to your computer and use it in GitHub Desktop.
Combine multiple JUnit XML files from folder into single XML file
#!/bin/bash
#
#
# Script combines given JUnit XML files into one.
#
# Script assumes that file begins with XML header.
# Script will copy content between <testsuite> tags
# to new file with XML header and <testsuites> tags.
# Script will not affect original files, but it will
# overwrite output file contents. Script searches all
# *.xml files from input directory.
#
# -h - Help
# -i [DIR] - Input directory
# -o [FILE] - Output file
#
# Author: Jarno Tuovinen <[email protected]>
#
# License: MIT
#
INPUTDIR=""
OUTPUTFILE=""
# arg 1 = input file
function striphead() {
sed 1,$(expr $(grep -m1 -n "<testsuite" $1 | cut -f1 -d:) - 1)d $1
}
function error() {
echo "ERROR: $*"
exit -1
}
function help() {
echo "$0"
echo "HELP:"
echo -e "\t-h\tHelp"
echo -e "\t-i\tInput dir [mandatory]"
echo -e "\t-o\tOutput file [mandatory]"
exit 0
}
# Handle parameters
while getopts "hi:o:" opt; do
case $opt in
h) help ;;
i) INPUTDIR="$OPTARG" ;;
o) OUTPUTFILE="$OPTARG" ;;
\?) error ;;
esac
done
# Input dir must be given
if [[ -z ${INPUTDIR} ]]; then
error "Input dir not given"
fi
# Output file must be given
if [[ -z ${OUTPUTFILE} ]]; then
error "Output file not given"
fi
echo '<?xml version="1.0" encoding="UTF-8"?>' > ${OUTPUTFILE}
echo '<testsuites>' >> ${OUTPUTFILE}
for f in ${INPUTDIR}/*.xml
do
echo $(striphead ${f}) >> ${OUTPUTFILE}
done
echo '</testsuites>' >> ${OUTPUTFILE}
@kickthemooon
Copy link

I'm getting

sed: -e expression #1, char 3: unexpected `,'

@dosas
Copy link

dosas commented Dec 11, 2023

Very nice! You probably do not even need the ouputfile argument. You could make use of redirect:

./combine-junit-xml.sh xml_dir > output.xml 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment