55 "encoding/json"
66 "fmt"
77 "io"
8- "math"
98 "os"
109 "path/filepath"
1110 "sort"
@@ -193,6 +192,7 @@ func PrintTests(
193192 w io.Writer ,
194193 tests []TestResult ,
195194 maxPassRatio float64 ,
195+ includeCodeOwners bool , // Include code owners in the output. Set to true if test results have code owners
196196) (runs , passes , fails , skips , panickedTests , racedTests , flakyTests int ) {
197197 p := message .NewPrinter (language .English ) // For formatting numbers
198198 sortTestResults (tests )
@@ -210,19 +210,17 @@ func PrintTests(
210210 "**Package**" ,
211211 "**Package Panicked?**" ,
212212 "**Avg Duration**" ,
213- "**Code Owners**" ,
213+ }
214+
215+ if includeCodeOwners {
216+ headers = append (headers , "**Code Owners**" )
214217 }
215218
216219 // Build test rows and summary data
217220 rows := [][]string {}
218221 for _ , test := range tests {
219222 if test .PassRatio < maxPassRatio {
220- owners := "Unknown"
221- if len (test .CodeOwners ) > 0 {
222- owners = strings .Join (test .CodeOwners , ", " )
223- }
224-
225- rows = append (rows , []string {
223+ row := []string {
226224 test .TestName ,
227225 fmt .Sprintf ("%.2f%%" , test .PassRatio * 100 ),
228226 fmt .Sprintf ("%t" , test .Panic ),
@@ -235,8 +233,17 @@ func PrintTests(
235233 test .TestPackage ,
236234 fmt .Sprintf ("%t" , test .PackagePanic ),
237235 avgDuration (test .Durations ).String (),
238- owners ,
239- })
236+ }
237+
238+ if includeCodeOwners {
239+ owners := "Unknown"
240+ if len (test .CodeOwners ) > 0 {
241+ owners = strings .Join (test .CodeOwners , ", " )
242+ }
243+ row = append (row , owners )
244+ }
245+
246+ rows = append (rows , row )
240247 }
241248
242249 runs += test .Runs
@@ -254,66 +261,8 @@ func PrintTests(
254261 }
255262 }
256263
257- var (
258- passRatioStr string
259- flakeRatioStr string
260- )
261- if runs == 0 || passes == runs {
262- passRatioStr = "100%"
263- flakeRatioStr = "0%"
264- } else {
265- passPercentage := float64 (passes ) / float64 (runs ) * 100
266- truncatedPassPercentage := math .Floor (passPercentage * 100 ) / 100 // Truncate to 2 decimal places
267- flakePercentage := float64 (flakyTests ) / float64 (len (tests )) * 100
268- truncatedFlakePercentage := math .Floor (flakePercentage * 100 ) / 100 // Truncate to 2 decimal places
269- passRatioStr = fmt .Sprintf ("%.2f%%" , truncatedPassPercentage )
270- flakeRatioStr = fmt .Sprintf ("%.2f%%" , truncatedFlakePercentage )
271- }
272-
273- // Print out summary data
274- summaryData := [][]string {
275- {"**Category**" , "**Total**" },
276- {"**Tests**" , p .Sprint (len (tests ))},
277- {"**Panicked Tests**" , p .Sprint (panickedTests )},
278- {"**Raced Tests**" , p .Sprint (racedTests )},
279- {"**Flaky Tests**" , p .Sprint (flakyTests )},
280- {"**Flaky Test Ratio**" , flakeRatioStr },
281- {"**Runs**" , p .Sprint (runs )},
282- {"**Passes**" , p .Sprint (passes )},
283- {"**Failures**" , p .Sprint (fails )},
284- {"**Skips**" , p .Sprint (skips )},
285- {"**Pass Ratio**" , passRatioStr },
286- }
287- colWidths := make ([]int , len (summaryData [0 ]))
288-
289- for _ , row := range summaryData {
290- for i , cell := range row {
291- if len (cell ) > colWidths [i ] {
292- colWidths [i ] = len (cell )
293- }
294- }
295- }
296-
297- if len (rows ) == 0 {
298- fmt .Fprintf (w , "No tests found under pass ratio of %.2f%%\n " , maxPassRatio * 100 )
299- return
300- }
301-
302- printRow := func (cells []string ) {
303- fmt .Fprintf (w , "| %-*s | %-*s |\n " , colWidths [0 ], cells [0 ], colWidths [1 ], cells [1 ])
304- }
305- printSeparator := func () {
306- fmt .Fprintf (w , "|-%s-|-%s-|\n " , strings .Repeat ("-" , colWidths [0 ]), strings .Repeat ("-" , colWidths [1 ]))
307- }
308- printRow (summaryData [0 ])
309- printSeparator ()
310- for _ , row := range summaryData [1 :] {
311- printRow (row )
312- }
313- fmt .Fprintln (w )
314-
315- // Print out test data
316- colWidths = make ([]int , len (headers ))
264+ // Adjust column widths and print
265+ colWidths := make ([]int , len (headers ))
317266 for i , header := range headers {
318267 colWidths [i ] = len (header )
319268 }
@@ -325,15 +274,15 @@ func PrintTests(
325274 }
326275 }
327276
328- printRow = func (cells []string ) {
277+ printRow : = func (cells []string ) {
329278 var buffer bytes.Buffer
330279 for i , cell := range cells {
331280 buffer .WriteString (fmt .Sprintf (" %-*s |" , colWidths [i ], cell ))
332281 }
333282 fmt .Fprintln (w , "|" + buffer .String ())
334283 }
335284
336- printSeparator = func () {
285+ printSeparator : = func () {
337286 var buffer bytes.Buffer
338287 for _ , width := range colWidths {
339288 buffer .WriteString (" " + strings .Repeat ("-" , width ) + " |" )
@@ -350,7 +299,7 @@ func PrintTests(
350299}
351300
352301// MarkdownSummary builds a summary of test results in markdown format, handy for reporting in CI and Slack
353- func MarkdownSummary (w io.Writer , testReport * TestReport , maxPassRatio float64 ) {
302+ func MarkdownSummary (w io.Writer , testReport * TestReport , maxPassRatio float64 , includeCodeOwners bool ) {
354303 var (
355304 avgPassRatio = 1.0
356305 testsData = bytes .NewBuffer (nil )
@@ -401,7 +350,7 @@ func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64)
401350 return
402351 }
403352
404- allRuns , passes , _ , _ , _ , _ , _ := PrintTests (testsData , tests , maxPassRatio )
353+ allRuns , passes , _ , _ , _ , _ , _ := PrintTests (testsData , tests , maxPassRatio , includeCodeOwners )
405354 if allRuns > 0 {
406355 avgPassRatio = float64 (passes ) / float64 (allRuns )
407356 }
@@ -414,7 +363,7 @@ func MarkdownSummary(w io.Writer, testReport *TestReport, maxPassRatio float64)
414363}
415364
416365// Helper function to save filtered results and logs to specified paths
417- func SaveFilteredResultsAndLogs (outputResultsPath , outputLogsPath string , report * TestReport ) error {
366+ func SaveFilteredResultsAndLogs (outputResultsPath , outputLogsPath string , report * TestReport , includeCodeOwners bool ) error {
418367 if outputResultsPath != "" {
419368 if err := os .MkdirAll (filepath .Dir (outputResultsPath ), 0755 ); err != nil { //nolint:gosec
420369 return fmt .Errorf ("error creating output directory: %w" , err )
@@ -430,7 +379,7 @@ func SaveFilteredResultsAndLogs(outputResultsPath, outputLogsPath string, report
430379 return fmt .Errorf ("error creating markdown file: %w" , err )
431380 }
432381 defer summaryFile .Close ()
433- MarkdownSummary (summaryFile , report , 1.0 )
382+ MarkdownSummary (summaryFile , report , 1.0 , includeCodeOwners )
434383 fmt .Printf ("Test results saved to %s and summary to %s\n " , jsonFileName , mdFileName )
435384 } else {
436385 fmt .Println ("No failed tests found based on the specified threshold and min pass ratio." )
0 commit comments