@@ -72,13 +72,15 @@ func (s SkipReason) MarshalText() ([]byte, error) {
72
72
// MergeJUnit merges all junit xml files found in sourceDirectories into a single xml file at destination, using the filter.
73
73
// The merging removes duplicate skipped tests. The original files are deleted.
74
74
func MergeJUnit (testFilter string , sourceDirectories []string , destination string ) error {
75
- var junit TestSuite
76
75
var data []byte
77
76
78
77
re := regexp .MustCompile (testFilter )
79
78
80
79
var mergeErrors []string
81
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 {}
82
84
for _ , dir := range sourceDirectories {
83
85
files , err := os .ReadDir (dir )
84
86
if err != nil {
@@ -87,7 +89,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
87
89
continue
88
90
}
89
91
for _ , file := range files {
90
- if ! strings .HasSuffix (file .Name (), ".xml" ) {
92
+ if ! strings .HasSuffix (file .Name (), ".xml" ) || file . Name () == "junit_runner.xml" {
91
93
continue
92
94
}
93
95
fullFilename := filepath .Join (dir , file .Name ())
@@ -96,24 +98,28 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
96
98
if err != nil {
97
99
return err
98
100
}
99
- if err = xml .Unmarshal (data , & junit ); err != nil {
100
- 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
+ }
101
117
}
102
118
}
103
119
}
104
120
105
121
// Keep only matching testcases. Testcases skipped in all test runs are only stored once.
106
- filtered := map [string ]TestCase {}
107
- for _ , testcase := range junit .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
- }
122
+ var junit TestSuite
117
123
junit .TestCases = nil
118
124
for _ , testcase := range filtered {
119
125
junit .TestCases = append (junit .TestCases , testcase )
@@ -122,7 +128,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
122
128
// Re-encode.
123
129
data , err := xml .MarshalIndent (junit , "" , " " )
124
130
if err != nil {
125
- return err
131
+ return fmt . Errorf ( "failed to marshal junit data: %w" , err )
126
132
}
127
133
128
134
if err = os .WriteFile (destination , data , 0644 ); err != nil {
0 commit comments