Skip to content

Commit c07b8cc

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

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,15 @@ 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+
err := rerunReport.PrintGotestsumOutput("pkgname")
186+
if err != nil {
187+
log.Error().Err(err).Msg("Error printing gotestsum output")
188+
}
189+
184190
log.Error().
185191
Int("noSuccessTests", len(failedAfterRerun)).
186192
Int("reruns", rerunFailedCount).

tools/flakeguard/reports/data.go

Lines changed: 24 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,27 @@ func (testReport *TestReport) SaveToFile(outputPath string) error {
4447
return nil
4548
}
4649

50+
func (tr *TestReport) PrintGotestsumOutput(format string) error {
51+
for i, 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+
fmt.Printf("Run %d logs:\n", i+1)
60+
fmt.Println("--------------------\n")
61+
if err := cmd.Run(); err != nil {
62+
return fmt.Errorf("gotestsum command failed for file %s: %w\nOutput: %s", path, err, outBuf.String())
63+
}
64+
65+
fmt.Print(outBuf.String())
66+
fmt.Println("\n--------------------\n")
67+
}
68+
return nil
69+
}
70+
4771
// GenerateSummaryData generates a summary of a report's test results
4872
func (testReport *TestReport) GenerateSummaryData() {
4973
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)