@@ -38,6 +38,7 @@ type TestSuites struct {
38
38
TestSuite []TestSuite `xml:"testsuite"`
39
39
}
40
40
type TestSuite struct {
41
+ XMLName string `xml:"testsuite"`
41
42
TestCases []TestCase `xml:"testcase"`
42
43
}
43
44
@@ -71,13 +72,15 @@ func (s SkipReason) MarshalText() ([]byte, error) {
71
72
// MergeJUnit merges all junit xml files found in sourceDirectories into a single xml file at destination, using the filter.
72
73
// The merging removes duplicate skipped tests. The original files are deleted.
73
74
func MergeJUnit (testFilter string , sourceDirectories []string , destination string ) error {
74
- var junit TestSuite
75
75
var data []byte
76
76
77
77
re := regexp .MustCompile (testFilter )
78
78
79
79
var mergeErrors []string
80
80
var filesToDelete []string
81
+
82
+ // Keep only matching testcases. Testcases skipped in all test runs are only stored once.
83
+ filtered := map [string ]TestCase {}
81
84
for _ , dir := range sourceDirectories {
82
85
files , err := os .ReadDir (dir )
83
86
if err != nil {
@@ -86,7 +89,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
86
89
continue
87
90
}
88
91
for _ , file := range files {
89
- if ! strings .HasSuffix (file .Name (), ".xml" ) {
92
+ if ! strings .HasSuffix (file .Name (), ".xml" ) || file . Name () == "junit_runner.xml" {
90
93
continue
91
94
}
92
95
fullFilename := filepath .Join (dir , file .Name ())
@@ -95,24 +98,28 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
95
98
if err != nil {
96
99
return err
97
100
}
98
- if err = xml .Unmarshal (data , & junit ); err != nil {
99
- return err
101
+ var testSuiteData TestSuites
102
+ if err = xml .Unmarshal (data , & testSuiteData ); err != nil {
103
+ return fmt .Errorf ("failed to unmarshal XML file %v: %w" , fullFilename , err )
104
+ }
105
+
106
+ for _ , testsuite := range testSuiteData .TestSuite {
107
+ for _ , testcase := range testsuite .TestCases {
108
+ if ! re .MatchString (testcase .Name ) {
109
+ continue
110
+ }
111
+ entry , ok := filtered [testcase .Name ]
112
+ if ! ok || // not present yet
113
+ entry .Skipped != "" && testcase .Skipped == "" { // replaced skipped test with real test run
114
+ filtered [testcase .Name ] = testcase
115
+ }
116
+ }
100
117
}
101
118
}
102
119
}
103
120
104
121
// Keep only matching testcases. Testcases skipped in all test runs are only stored once.
105
- filtered := map [string ]TestCase {}
106
- for _ , testcase := range junit .TestCases {
107
- if ! re .MatchString (testcase .Name ) {
108
- continue
109
- }
110
- entry , ok := filtered [testcase .Name ]
111
- if ! ok || // not present yet
112
- entry .Skipped != "" && testcase .Skipped == "" { // replaced skipped test with real test run
113
- filtered [testcase .Name ] = testcase
114
- }
115
- }
122
+ var junit TestSuite
116
123
junit .TestCases = nil
117
124
for _ , testcase := range filtered {
118
125
junit .TestCases = append (junit .TestCases , testcase )
@@ -121,7 +128,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
121
128
// Re-encode.
122
129
data , err := xml .MarshalIndent (junit , "" , " " )
123
130
if err != nil {
124
- return err
131
+ return fmt . Errorf ( "failed to marshal junit data: %w" , err )
125
132
}
126
133
127
134
if err = os .WriteFile (destination , data , 0644 ); err != nil {
0 commit comments