Skip to content

Commit

Permalink
Add testcase source file attribute to JUnit XML reports (#8218)
Browse files Browse the repository at this point in the history
* Add script to insert source file attributes to XML file testcases.
  • Loading branch information
sarahchen6 authored Jan 21, 2025
1 parent d5592c6 commit 1f71dbd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion .circleci/collect_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# This folder will be saved by circleci and available after test runs.

set -e
#Enable '**' support
# Enable '**' support
shopt -s globstar

TEST_RESULTS_DIR=results
Expand All @@ -19,13 +19,36 @@ if [[ ${#TEST_RESULT_DIRS[@]} -eq 0 ]]; then
exit 0
fi

function get_source_file () {
file_path="${RESULT_XML_FILE%%"/build"*}"
file_path="${file_path/#"$WORKSPACE_DIR"\//}/src"
if ! [[ $RESULT_XML_FILE == *"#"* ]]; then
class="${RESULT_XML_FILE%.xml}"
class="${class##*"TEST-"}"
class="${class##*"."}"
common_root=$(grep -rl "class $class" "$file_path" | head -n 1)
while IFS= read -r line; do
while [[ $line != "$common_root"* ]]; do
common_root=$(dirname "$common_root")
if [[ "$common_root" == "$common_root/.." ]]; then
break
fi
done
done < <(grep -rl "class $class" "$file_path")
file_path="$common_root"
fi
}

echo "Saving test results:"
while IFS= read -r -d '' RESULT_XML_FILE
do
echo -n "- $RESULT_XML_FILE"
AGGREGATED_FILE_NAME=$(echo "$RESULT_XML_FILE" | rev | cut -d "/" -f 1,2,5 | rev | tr "/" "_")
echo -n " as $AGGREGATED_FILE_NAME"
cp "$RESULT_XML_FILE" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Insert file attribute to testcase XML nodes
get_source_file
sed -i "/<testcase/ s|\(time=\"[^\"]*\"\)|\1 file=\"$file_path\"|g" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Replace Java Object hashCode by marker in testcase XML nodes to get stable test names
sed -i '/<testcase/ s/@[0-9a-f]\{5,\}/@HASHCODE/g' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
# Replace random port numbers by marker in testcase XML nodes to get stable test names
Expand Down

0 comments on commit 1f71dbd

Please sign in to comment.