Skip to content

Commit 1f71dbd

Browse files
authored
Add testcase source file attribute to JUnit XML reports (#8218)
* Add script to insert source file attributes to XML file testcases.
1 parent d5592c6 commit 1f71dbd

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

.circleci/collect_results.sh

+24-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# This folder will be saved by circleci and available after test runs.
55

66
set -e
7-
#Enable '**' support
7+
# Enable '**' support
88
shopt -s globstar
99

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

22+
function get_source_file () {
23+
file_path="${RESULT_XML_FILE%%"/build"*}"
24+
file_path="${file_path/#"$WORKSPACE_DIR"\//}/src"
25+
if ! [[ $RESULT_XML_FILE == *"#"* ]]; then
26+
class="${RESULT_XML_FILE%.xml}"
27+
class="${class##*"TEST-"}"
28+
class="${class##*"."}"
29+
common_root=$(grep -rl "class $class" "$file_path" | head -n 1)
30+
while IFS= read -r line; do
31+
while [[ $line != "$common_root"* ]]; do
32+
common_root=$(dirname "$common_root")
33+
if [[ "$common_root" == "$common_root/.." ]]; then
34+
break
35+
fi
36+
done
37+
done < <(grep -rl "class $class" "$file_path")
38+
file_path="$common_root"
39+
fi
40+
}
41+
2242
echo "Saving test results:"
2343
while IFS= read -r -d '' RESULT_XML_FILE
2444
do
2545
echo -n "- $RESULT_XML_FILE"
2646
AGGREGATED_FILE_NAME=$(echo "$RESULT_XML_FILE" | rev | cut -d "/" -f 1,2,5 | rev | tr "/" "_")
2747
echo -n " as $AGGREGATED_FILE_NAME"
2848
cp "$RESULT_XML_FILE" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
49+
# Insert file attribute to testcase XML nodes
50+
get_source_file
51+
sed -i "/<testcase/ s|\(time=\"[^\"]*\"\)|\1 file=\"$file_path\"|g" "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
2952
# Replace Java Object hashCode by marker in testcase XML nodes to get stable test names
3053
sed -i '/<testcase/ s/@[0-9a-f]\{5,\}/@HASHCODE/g' "$TEST_RESULTS_DIR/$AGGREGATED_FILE_NAME"
3154
# Replace random port numbers by marker in testcase XML nodes to get stable test names

0 commit comments

Comments
 (0)