Skip to content

Commit

Permalink
Introduce merge_junit script (#103)
Browse files Browse the repository at this point in the history
* Introduce `merge_junit` script

To improve performance of Android unit tests collection. Originated it woocommerce/woocommerce-android#12064

* Add changelog entry

* Address SC2129

Consider using { cmd1; cmd2; } >> file instead of individual redirects. https://www.shellcheck.net/wiki/SC2129

* Remove `sh` suffix

To align to other commands

* Create output file before saving data there

* Rename script to `merge_junit_reports`

* Remove `<testsuites>` tag if it's present in any report that is about to be merged

* Update CHANGELOG.md

Co-authored-by: Olivier Halligon <[email protected]>

---------

Co-authored-by: Olivier Halligon <[email protected]>
  • Loading branch information
wzieba and AliSoftware authored Aug 5, 2024
1 parent abdf292 commit f7a0940
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ _None._

### New Features

_None._
- Introduce `merge_junit_reports` script [#103]

### Bug Fixes

Expand Down
43 changes: 43 additions & 0 deletions bin/merge_junit_reports
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Initialize variables
reports_dir=""
output_file=""

# Function to show usage
usage() {
echo "Usage: $0 -d <reports_dir> -o <output_file>"
exit 1
}

# Parse command-line options
while getopts "d:o:" opt; do
case $opt in
d) reports_dir=$OPTARG ;;
o) output_file=$OPTARG ;;
?) usage ;;
esac
done

# Check if both arguments were provided
if [ -z "$reports_dir" ] || [ -z "$output_file" ]; then
usage
fi

touch "$output_file"
{
echo '<?xml version="1.0" encoding="UTF-8"?>'
# Write XML header to the output file
echo '<testsuites>'

# Merge the content of all input JUnit files in the directory.
# Ignore <testsuites> tag in reports that are being merged,
# as this tag should be a top-level, root tag for any report
sed '/<\?xml .*\?>/d;/<testsuites.*>/d; /<\/testsuites>/d' "$reports_dir"/*.xml

# Close the testsuites tag
echo '</testsuites>'
} >> "$output_file"

# Print the result
echo "Merged XML reports into $output_file"

0 comments on commit f7a0940

Please sign in to comment.