parseJUnitFile reads and parses a single JUnit XML file
(filePath string)
| 18 | |
| 19 | // parseJUnitFile reads and parses a single JUnit XML file |
| 20 | func parseJUnitFile(filePath string) (*junit.Testsuites, error) { |
| 21 | file, err := os.Open(filePath) |
| 22 | if err != nil { |
| 23 | return nil, fmt.Errorf("failed to open file %s: %w", filePath, err) |
| 24 | } |
| 25 | defer func() { |
| 26 | if err := file.Close(); err != nil { |
| 27 | fmt.Printf("Warning: Failed to close file %s: %v\n", filePath, err) |
| 28 | } |
| 29 | }() |
| 30 | |
| 31 | var testsuites junit.Testsuites |
| 32 | decoder := xml.NewDecoder(file) |
| 33 | if err := decoder.Decode(&testsuites); err != nil { |
| 34 | // Try parsing as a single testsuite |
| 35 | if _, seekErr := file.Seek(0, 0); seekErr != nil { |
| 36 | return nil, fmt.Errorf("failed to seek file %s: %w", filePath, seekErr) |
| 37 | } |
| 38 | var testsuite junit.Testsuite |
| 39 | decoder = xml.NewDecoder(file) |
| 40 | if err := decoder.Decode(&testsuite); err != nil { |
| 41 | return nil, fmt.Errorf("failed to parse JUnit XML %s: %w", filePath, err) |
| 42 | } |
| 43 | testsuites.Suites = []junit.Testsuite{testsuite} |
| 44 | } |
| 45 | |
| 46 | return &testsuites, nil |
| 47 | } |
| 48 | |
| 49 | // topLevelTestName extracts the suite/top-level test name from a test name. |
| 50 | // For "TestSuiteV0/TestMethod" returns "TestSuiteV0". |
no test coverage detected