Skip to content

Commit

Permalink
refactor: read only first 8KB of the xml
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Oct 2, 2024
1 parent 22b065f commit 05dd832
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions cmd/testworkflow-toolkit/artifacts/junit_post_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,15 @@ func (p *JUnitPostProcessor) Add(path string) error {
if ok := isXMLFile(stat); !ok {
return nil
}
xmlData, err := io.ReadAll(file)
if err != nil {
// Read only the first 8KB of data
const BYTE_SIZE_8KB = 8 * 1024
xmlData := make([]byte, BYTE_SIZE_8KB)
n, err := file.Read(xmlData)
if err != nil && err != io.EOF {
return errors.Wrapf(err, "failed to read %s", path)
}
// Trim the slice to the actual number of bytes read
xmlData = xmlData[:n]
ok := isJUnitReport(xmlData)
if !ok {
return nil
Expand Down Expand Up @@ -103,17 +108,11 @@ func isXMLFile(stat fs.FileInfo) bool {

// isJUnitReport checks if the XML data is a JUnit report.
func isJUnitReport(xmlData []byte) bool {
const BYTE_SIZE_8KB = 8 * 1024

tags := []string{
"<testsuite",
"<testsuites",
}

if len(xmlData) > BYTE_SIZE_8KB {
xmlData = xmlData[:BYTE_SIZE_8KB]
}

content := string(xmlData)

for _, tag := range tags {
Expand Down

0 comments on commit 05dd832

Please sign in to comment.