Skip to content

Commit 10f3fdb

Browse files
authored
Merge pull request #1914 from pwschuurman/fix-junit-xml
Add XMLName back to TestSuite struct
2 parents 27bb056 + b4d5b06 commit 10f3fdb

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

test/k8s-integration/filter-junit.go

+23-16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type TestSuites struct {
3838
TestSuite []TestSuite `xml:"testsuite"`
3939
}
4040
type TestSuite struct {
41+
XMLName string `xml:"testsuite"`
4142
TestCases []TestCase `xml:"testcase"`
4243
}
4344

@@ -71,13 +72,15 @@ func (s SkipReason) MarshalText() ([]byte, error) {
7172
// MergeJUnit merges all junit xml files found in sourceDirectories into a single xml file at destination, using the filter.
7273
// The merging removes duplicate skipped tests. The original files are deleted.
7374
func MergeJUnit(testFilter string, sourceDirectories []string, destination string) error {
74-
var junit TestSuite
7575
var data []byte
7676

7777
re := regexp.MustCompile(testFilter)
7878

7979
var mergeErrors []string
8080
var filesToDelete []string
81+
82+
// Keep only matching testcases. Testcases skipped in all test runs are only stored once.
83+
filtered := map[string]TestCase{}
8184
for _, dir := range sourceDirectories {
8285
files, err := os.ReadDir(dir)
8386
if err != nil {
@@ -86,7 +89,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
8689
continue
8790
}
8891
for _, file := range files {
89-
if !strings.HasSuffix(file.Name(), ".xml") {
92+
if !strings.HasSuffix(file.Name(), ".xml") || file.Name() == "junit_runner.xml" {
9093
continue
9194
}
9295
fullFilename := filepath.Join(dir, file.Name())
@@ -95,24 +98,28 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
9598
if err != nil {
9699
return err
97100
}
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+
}
100117
}
101118
}
102119
}
103120

104121
// 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
116123
junit.TestCases = nil
117124
for _, testcase := range filtered {
118125
junit.TestCases = append(junit.TestCases, testcase)
@@ -121,7 +128,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
121128
// Re-encode.
122129
data, err := xml.MarshalIndent(junit, "", " ")
123130
if err != nil {
124-
return err
131+
return fmt.Errorf("failed to marshal junit data: %w", err)
125132
}
126133

127134
if err = os.WriteFile(destination, data, 0644); err != nil {

0 commit comments

Comments
 (0)