@@ -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 ("\n Flakeguard Main Summary:\n " )
142- reports .RenderResults (os .Stdout , * mainReport , false , false )
143- fmt .Println ()
150+ // Accumulate main summary into summaryBuffer
151+ fmt .Fprint (& summaryBuffer , "\n Flakeguard 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 ("\n All Tests That Were Rerun:\n " )
163- reports .PrintTestResultsTable (os .Stdout , rerunReport .Results , false , false )
164- fmt .Println ()
173+ fmt .Fprint (& summaryBuffer , "\n All 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 ("\n Tests That Have 0 Success Runs:\n " )
182- reports .PrintTestResultsTable (os .Stdout , failedAfterRerun , false , false )
183- fmt .Println ()
184-
185- fmt .Printf ("\n Logs From All Reruns:\n " )
186- err := rerunReport .PrintGotestsumOutput ("pkgname" )
193+ fmt .Fprint (& summaryBuffer , "\n Tests 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 , "\n Logs 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
0 commit comments