Skip to content

Add XMLName back to TestSuite struct #1914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions test/k8s-integration/filter-junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type TestSuites struct {
TestSuite []TestSuite `xml:"testsuite"`
}
type TestSuite struct {
XMLName string `xml:"testsuite"`
TestCases []TestCase `xml:"testcase"`
}

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

re := regexp.MustCompile(testFilter)

var mergeErrors []string
var filesToDelete []string

// Keep only matching testcases. Testcases skipped in all test runs are only stored once.
filtered := map[string]TestCase{}
for _, dir := range sourceDirectories {
files, err := os.ReadDir(dir)
if err != nil {
Expand All @@ -86,7 +89,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
continue
}
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".xml") {
if !strings.HasSuffix(file.Name(), ".xml") || file.Name() == "junit_runner.xml" {
continue
}
fullFilename := filepath.Join(dir, file.Name())
Expand All @@ -95,24 +98,28 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
if err != nil {
return err
}
if err = xml.Unmarshal(data, &junit); err != nil {
return err
var testSuiteData TestSuites
if err = xml.Unmarshal(data, &testSuiteData); err != nil {
return fmt.Errorf("failed to unmarshal XML file %v: %w", fullFilename, err)
}

for _, testsuite := range testSuiteData.TestSuite {
for _, testcase := range testsuite.TestCases {
if !re.MatchString(testcase.Name) {
continue
}
entry, ok := filtered[testcase.Name]
if !ok || // not present yet
entry.Skipped != "" && testcase.Skipped == "" { // replaced skipped test with real test run
filtered[testcase.Name] = testcase
}
}
}
}
}

// Keep only matching testcases. Testcases skipped in all test runs are only stored once.
filtered := map[string]TestCase{}
for _, testcase := range junit.TestCases {
if !re.MatchString(testcase.Name) {
continue
}
entry, ok := filtered[testcase.Name]
if !ok || // not present yet
entry.Skipped != "" && testcase.Skipped == "" { // replaced skipped test with real test run
filtered[testcase.Name] = testcase
}
}
var junit TestSuite
junit.TestCases = nil
for _, testcase := range filtered {
junit.TestCases = append(junit.TestCases, testcase)
Expand All @@ -121,7 +128,7 @@ func MergeJUnit(testFilter string, sourceDirectories []string, destination strin
// Re-encode.
data, err := xml.MarshalIndent(junit, "", " ")
if err != nil {
return err
return fmt.Errorf("failed to marshal junit data: %w", err)
}

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