Skip to content

Commit 8576bda

Browse files
committed
Print gotestsum output for failed reruns
1 parent 951da90 commit 8576bda

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,16 @@ var RunTestsCmd = &cobra.Command{
178178
})
179179

180180
if len(failedAfterRerun) > 0 {
181-
fmt.Println("\nTests That Failed All Reruns:")
181+
fmt.Printf("\nTests That Failed All %d Reruns:\n", rerunFailedCount)
182182
reports.PrintTestResultsTable(os.Stdout, failedAfterRerun, false, false)
183183
fmt.Println()
184+
185+
fmt.Printf("\nLogs From All Reruns:\n")
186+
err := rerunReport.PrintGotestsumOutput("pkgname")
187+
if err != nil {
188+
log.Error().Err(err).Msg("Error printing gotestsum output")
189+
}
190+
184191
log.Error().
185192
Int("noSuccessTests", len(failedAfterRerun)).
186193
Int("reruns", rerunFailedCount).

tools/flakeguard/reports/data.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package reports
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
67
"os"
8+
"os/exec"
79
"sort"
810
"strings"
911
"time"
@@ -25,6 +27,7 @@ type TestReport struct {
2527
SelectedTests []string `json:"selected_tests,omitempty"`
2628
Results []TestResult `json:"results,omitempty"`
2729
FailedLogsURL string `json:"failed_logs_url,omitempty"`
30+
JSONOutputPaths []string `json:"-"` // go test -json outputs from runs
2831
// MaxPassRatio is the maximum flakiness ratio allowed for a test to be considered not flaky
2932
MaxPassRatio float64 `json:"max_pass_ratio,omitempty"`
3033
}
@@ -44,6 +47,25 @@ func (testReport *TestReport) SaveToFile(outputPath string) error {
4447
return nil
4548
}
4649

50+
func (tr *TestReport) PrintGotestsumOutput(format string) error {
51+
for _, path := range tr.JSONOutputPaths {
52+
cmdStr := fmt.Sprintf("cat %q | gotestsum --raw-command --format %q -- cat", path, format)
53+
cmd := exec.Command("bash", "-c", cmdStr)
54+
55+
var outBuf bytes.Buffer
56+
cmd.Stdout = &outBuf
57+
cmd.Stderr = &outBuf
58+
59+
if err := cmd.Run(); err != nil {
60+
return fmt.Errorf("gotestsum command failed for file %s: %w\nOutput: %s", path, err, outBuf.String())
61+
}
62+
63+
fmt.Print(outBuf.String())
64+
fmt.Println("---------------------")
65+
}
66+
return nil
67+
}
68+
4769
// GenerateSummaryData generates a summary of a report's test results
4870
func (testReport *TestReport) GenerateSummaryData() {
4971
var runs, testRunCount, passes, fails, skips, panickedTests, racedTests, flakyTests int

tools/flakeguard/runner/runner.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,9 @@ func (r *Runner) parseTestResults(filePaths []string, runPrefix string, runCount
543543
if err = file.Close(); err != nil {
544544
log.Warn().Err(err).Str("file", filePath).Msg("failed to close file")
545545
}
546-
if err = os.Remove(filePath); err != nil {
547-
log.Warn().Err(err).Str("file", filePath).Msg("failed to delete file")
548-
}
546+
// if err = os.Remove(filePath); err != nil {
547+
// log.Warn().Err(err).Str("file", filePath).Msg("failed to delete file")
548+
// }
549549
}
550550

551551
var results []reports.TestResult
@@ -778,12 +778,13 @@ func (r *Runner) RerunFailedTests(failedTests []reports.TestResult) (*reports.Te
778778
}
779779

780780
report := &reports.TestReport{
781-
GoProject: r.prettyProjectPath,
782-
RaceDetection: r.UseRace,
783-
ExcludedTests: r.SkipTests,
784-
SelectedTests: r.SelectTests,
785-
Results: rerunResults,
786-
MaxPassRatio: r.MaxPassRatio,
781+
GoProject: r.prettyProjectPath,
782+
RaceDetection: r.UseRace,
783+
ExcludedTests: r.SkipTests,
784+
SelectedTests: r.SelectTests,
785+
Results: rerunResults,
786+
MaxPassRatio: r.MaxPassRatio,
787+
JSONOutputPaths: rerunJsonFilePaths,
787788
}
788789
report.GenerateSummaryData()
789790

0 commit comments

Comments
 (0)