Skip to content

Commit 69c8081

Browse files
committed
Improve outputs formatting in the console
1 parent 99a4ea2 commit 69c8081

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

tools/flakeguard/cmd/run.go

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ var RunTestsCmd = &cobra.Command{
2424
Use: "run",
2525
Short: "Run tests to check if they are flaky",
2626
Run: func(cmd *cobra.Command, args []string) {
27+
// Create a buffer to accumulate all summary output.
28+
var summaryBuffer bytes.Buffer
29+
30+
// Helper function to flush the summary buffer and exit.
31+
flushSummaryAndExit := func(code int) {
32+
fmt.Print(summaryBuffer.String())
33+
os.Exit(code)
34+
}
35+
2736
// Retrieve flags
2837
projectPath, _ := cmd.Flags().GetString("project-path")
2938
testPackagesJson, _ := cmd.Flags().GetString("test-packages-json")
@@ -59,13 +68,13 @@ var RunTestsCmd = &cobra.Command{
5968
// Validate pass ratio
6069
if passRatioThreshold < 0 || passRatioThreshold > 1 {
6170
log.Error().Float64("pass ratio", passRatioThreshold).Msg("Error: pass ratio must be between 0 and 1")
62-
os.Exit(ErrorExitCode)
71+
flushSummaryAndExit(ErrorExitCode)
6372
}
6473

6574
// Check if project dependencies are correctly set up
6675
if err := checkDependencies(projectPath); err != nil {
6776
log.Error().Err(err).Msg("Error checking project dependencies")
68-
os.Exit(ErrorExitCode)
77+
flushSummaryAndExit(ErrorExitCode)
6978
}
7079

7180
// Determine test packages
@@ -74,13 +83,13 @@ var RunTestsCmd = &cobra.Command{
7483
if testPackagesJson != "" {
7584
if err := json.Unmarshal([]byte(testPackagesJson), &testPackages); err != nil {
7685
log.Error().Err(err).Msg("Error decoding test packages JSON")
77-
os.Exit(ErrorExitCode)
86+
flushSummaryAndExit(ErrorExitCode)
7887
}
7988
} else if len(testPackagesArg) > 0 {
8089
testPackages = testPackagesArg
8190
} else {
8291
log.Error().Msg("Error: must specify either --test-packages-json or --test-packages")
83-
os.Exit(ErrorExitCode)
92+
flushSummaryAndExit(ErrorExitCode)
8493
}
8594
}
8695

@@ -114,33 +123,35 @@ var RunTestsCmd = &cobra.Command{
114123
mainReport, err = testRunner.RunTestCmd(testCmdStrings)
115124
if err != nil {
116125
log.Fatal().Err(err).Msg("Error running custom test command")
117-
os.Exit(ErrorExitCode)
126+
flushSummaryAndExit(ErrorExitCode)
118127
}
119128
} else {
120129
mainReport, err = testRunner.RunTestPackages(testPackages)
121130
if err != nil {
122131
log.Fatal().Err(err).Msg("Error running test packages")
123-
os.Exit(ErrorExitCode)
132+
flushSummaryAndExit(ErrorExitCode)
124133
}
125134
}
126135

127136
// Save the main test report to file
128137
if mainReportPath != "" && len(mainReport.Results) > 0 {
129138
if err := mainReport.SaveToFile(mainReportPath); err != nil {
130139
log.Error().Err(err).Msg("Error saving test results to file")
131-
os.Exit(ErrorExitCode)
140+
flushSummaryAndExit(ErrorExitCode)
132141
}
133142
log.Info().Str("path", mainReportPath).Msg("Main test report saved")
134143
}
135144

136145
if len(mainReport.Results) == 0 {
137146
log.Warn().Msg("No tests were run for the specified packages")
138-
return
147+
flushSummaryAndExit(0)
139148
}
140149

141-
fmt.Printf("\nFlakeguard Main Summary:\n")
142-
reports.RenderResults(os.Stdout, *mainReport, false, false)
143-
fmt.Println()
150+
// Accumulate main summary into summaryBuffer
151+
fmt.Fprint(&summaryBuffer, "\nFlakeguard Main Summary:\n")
152+
fmt.Fprintf(&summaryBuffer, "-------------------------\n")
153+
reports.RenderResults(&summaryBuffer, *mainReport, false, false)
154+
fmt.Fprintln(&summaryBuffer)
144155

145156
// Rerun failed tests
146157
if rerunFailedCount > 0 {
@@ -150,24 +161,25 @@ var RunTestsCmd = &cobra.Command{
150161

151162
if len(failedTests) == 0 {
152163
log.Info().Msg("No tests to rerun. All tests passed")
153-
os.Exit(0)
164+
flushSummaryAndExit(0)
154165
}
155166

156167
rerunReport, err = testRunner.RerunFailedTests(failedTests)
157168
if err != nil {
158169
log.Fatal().Err(err).Msg("Error rerunning failed tests")
159-
os.Exit(ErrorExitCode)
170+
flushSummaryAndExit(ErrorExitCode)
160171
}
161172

162-
fmt.Printf("\nAll Tests That Were Rerun:\n")
163-
reports.PrintTestResultsTable(os.Stdout, rerunReport.Results, false, false)
164-
fmt.Println()
173+
fmt.Fprint(&summaryBuffer, "\nAll Tests That Were Rerun:\n")
174+
fmt.Fprintf(&summaryBuffer, "---------------------------\n")
175+
reports.PrintTestResultsTable(&summaryBuffer, rerunReport.Results, false, false)
176+
fmt.Fprintln(&summaryBuffer)
165177

166178
// Save the rerun test report to file
167179
if rerunReportPath != "" && len(rerunReport.Results) > 0 {
168180
if err := rerunReport.SaveToFile(rerunReportPath); err != nil {
169181
log.Error().Err(err).Msg("Error saving test results to file")
170-
os.Exit(ErrorExitCode)
182+
flushSummaryAndExit(ErrorExitCode)
171183
}
172184
log.Info().Str("path", rerunReportPath).Msg("Rerun test report saved")
173185
}
@@ -178,12 +190,14 @@ var RunTestsCmd = &cobra.Command{
178190
})
179191

180192
if len(failedAfterRerun) > 0 {
181-
fmt.Printf("\nTests That Have 0 Success Runs:\n")
182-
reports.PrintTestResultsTable(os.Stdout, failedAfterRerun, false, false)
183-
fmt.Println()
184-
185-
fmt.Printf("\nLogs From All Reruns:\n")
186-
err := rerunReport.PrintGotestsumOutput("pkgname")
193+
fmt.Fprint(&summaryBuffer, "\nTests That Have 0 Success Runs:\n")
194+
fmt.Fprintf(&summaryBuffer, "--------------------------------\n")
195+
reports.PrintTestResultsTable(&summaryBuffer, failedAfterRerun, false, false)
196+
fmt.Fprintln(&summaryBuffer)
197+
198+
fmt.Fprint(&summaryBuffer, "\nLogs From All Reruns:\n")
199+
fmt.Fprintf(&summaryBuffer, "----------------------\n")
200+
err := rerunReport.PrintGotestsumOutput(&summaryBuffer, "pkgname")
187201
if err != nil {
188202
log.Error().Err(err).Msg("Error printing gotestsum output")
189203
}
@@ -192,10 +206,10 @@ var RunTestsCmd = &cobra.Command{
192206
Int("noSuccessTests", len(failedAfterRerun)).
193207
Int("reruns", rerunFailedCount).
194208
Msg("Some tests are still failing after multiple reruns with no successful attempts.")
195-
os.Exit(ErrorExitCode)
209+
flushSummaryAndExit(ErrorExitCode)
196210
} else {
197211
log.Info().Msg("All tests passed at least once after reruns")
198-
os.Exit(0)
212+
flushSummaryAndExit(0)
199213
}
200214
} else {
201215
// Filter flaky tests using FilterTests
@@ -208,11 +222,13 @@ var RunTestsCmd = &cobra.Command{
208222
Int("count", len(flakyTests)).
209223
Str("stability threshold", fmt.Sprintf("%.0f%%", passRatioThreshold*100)).
210224
Msg("Found flaky tests")
211-
os.Exit(FlakyTestsExitCode)
225+
flushSummaryAndExit(FlakyTestsExitCode)
212226
} else {
213227
log.Info().Msg("All tests passed stability requirements")
214228
}
215229
}
230+
231+
flushSummaryAndExit(0)
216232
},
217233
}
218234

tools/flakeguard/reports/data.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"os"
89
"os/exec"
910
"sort"
@@ -47,13 +48,12 @@ func (testReport *TestReport) SaveToFile(outputPath string) error {
4748
return nil
4849
}
4950

50-
func (tr *TestReport) PrintGotestsumOutput(format string) error {
51+
func (tr *TestReport) PrintGotestsumOutput(w io.Writer, format string) error {
5152
if len(tr.JSONOutputPaths) == 0 {
52-
fmt.Printf("No JSON output paths found in test report\n")
53+
fmt.Fprintf(w, "No JSON output paths found in test report\n")
5354
return nil
5455
}
5556

56-
fmt.Println("---------------------")
5757
for _, path := range tr.JSONOutputPaths {
5858
cmdStr := fmt.Sprintf("cat %q | gotestsum --raw-command --format %q -- cat", path, format)
5959
cmd := exec.Command("bash", "-c", cmdStr)
@@ -66,8 +66,8 @@ func (tr *TestReport) PrintGotestsumOutput(format string) error {
6666
return fmt.Errorf("gotestsum command failed for file %s: %w\nOutput: %s", path, err, outBuf.String())
6767
}
6868

69-
fmt.Print(outBuf.String())
70-
fmt.Println("---------------------")
69+
fmt.Fprint(w, outBuf.String())
70+
fmt.Fprintln(w, "---------------------")
7171
}
7272
return nil
7373
}

0 commit comments

Comments
 (0)