Skip to content

Commit 69556c8

Browse files
committed
Remove logs from flakeguard all test results
1 parent 8ab91a5 commit 69556c8

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

tools/flakeguard/cmd/aggregate_results.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,6 @@ var AggregateResultsCmd = &cobra.Command{
8787
fmt.Println("Test results mapped to code owners successfully.")
8888
}
8989

90-
// Save the aggregated report to the output directory
91-
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
92-
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
93-
return fmt.Errorf("error saving aggregated test report: %w", err)
94-
}
95-
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)
96-
97-
// Filter failed tests (PassRatio < maxPassRatio and not skipped)
98-
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)
99-
s.Suffix = " Filtering failed tests..."
100-
s.Start()
101-
10290
failedTests := reports.FilterTests(aggregatedReport.Results, func(tr reports.TestResult) bool {
10391
return !tr.Skipped && tr.PassRatio < maxPassRatio
10492
})
@@ -141,6 +129,19 @@ var AggregateResultsCmd = &cobra.Command{
141129
fmt.Println("No failed tests found. Skipping generation of failed tests reports.")
142130
}
143131

132+
// Remove logs from test results for the aggregated report
133+
for i := range aggregatedReport.Results {
134+
aggregatedReport.Results[i].Outputs = nil
135+
aggregatedReport.Results[i].PackageOutputs = nil
136+
}
137+
138+
// Save the aggregated report to the output directory
139+
aggregatedReportPath := filepath.Join(outputDir, "all-test-results.json")
140+
if err := reports.SaveReport(fs, aggregatedReportPath, *aggregatedReport); err != nil {
141+
return fmt.Errorf("error saving aggregated test report: %w", err)
142+
}
143+
fmt.Printf("Aggregated test report saved to %s\n", aggregatedReportPath)
144+
144145
// Generate all-tests-summary.json
145146
if summaryFileName != "" {
146147
s = spinner.New(spinner.CharSets[11], 100*time.Millisecond)

tools/flakeguard/reports/io.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package reports
22

33
import (
4+
"bufio"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -101,9 +102,33 @@ func SaveReportNoLogs(fs FileSystem, filePath string, report TestReport) error {
101102
}
102103

103104
func SaveReport(fs FileSystem, filePath string, report TestReport) error {
104-
data, err := json.MarshalIndent(report, "", " ")
105+
// Open the file with truncation mode
106+
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
105107
if err != nil {
106-
return fmt.Errorf("error marshaling outputs: %v", err)
108+
return fmt.Errorf("error opening file: %v", err)
107109
}
108-
return fs.WriteFile(filePath, data, 0644)
110+
defer func() {
111+
if cerr := file.Close(); cerr != nil {
112+
err = fmt.Errorf("error closing file: %v", cerr)
113+
}
114+
}()
115+
116+
// Use a buffered writer for better performance
117+
bufferedWriter := bufio.NewWriter(file)
118+
defer func() {
119+
if err := bufferedWriter.Flush(); err != nil {
120+
fmt.Printf("error flushing buffer: %v\n", err)
121+
}
122+
}()
123+
124+
// Create a JSON encoder with the buffered writer
125+
encoder := json.NewEncoder(bufferedWriter)
126+
encoder.SetIndent("", " ")
127+
128+
// Encode the report
129+
if err := encoder.Encode(report); err != nil {
130+
return fmt.Errorf("error encoding JSON: %v", err)
131+
}
132+
133+
return nil
109134
}

0 commit comments

Comments
 (0)