44 "bufio"
55 "fmt"
66 "io"
7+ "os"
78 "strings"
89
910 "github.com/fatih/color"
@@ -73,18 +74,22 @@ func standardJSONFormat(out io.Writer) EventFormatter {
7374 })
7475}
7576
77+ func testNameFormatTestEvent (out io.Writer , event TestEvent ) {
78+ pkgPath := RelativePackagePath (event .Package )
79+
80+ fmt .Fprintf (out , "%s %s%s %s\n " ,
81+ colorEvent (event )(strings .ToUpper (string (event .Action ))),
82+ joinPkgToTestName (pkgPath , event .Test ),
83+ formatRunID (event .RunID ),
84+ fmt .Sprintf ("(%.2fs)" , event .Elapsed ))
85+ }
86+
7687func testNameFormat (out io.Writer ) EventFormatter {
7788 buf := bufio .NewWriter (out )
7889 // nolint:errcheck
7990 return eventFormatterFunc (func (event TestEvent , exec * Execution ) error {
8091 formatTest := func () error {
81- pkgPath := RelativePackagePath (event .Package )
82-
83- fmt .Fprintf (buf , "%s %s%s %s\n " ,
84- colorEvent (event )(strings .ToUpper (string (event .Action ))),
85- joinPkgToTestName (pkgPath , event .Test ),
86- formatRunID (event .RunID ),
87- event .ElapsedFormatted ())
92+ testNameFormatTestEvent (buf , event )
8893 return buf .Flush ()
8994 }
9095
@@ -101,6 +106,7 @@ func testNameFormat(out io.Writer) EventFormatter {
101106 result := colorEvent (event )(strings .ToUpper (string (event .Action )))
102107 pkg := exec .Package (event .Package )
103108 if event .Action == ActionSkip || (event .Action == ActionPass && pkg .Total == 0 ) {
109+ event .Action = ActionSkip // always color these as skip actions
104110 result = colorEvent (event )("EMPTY" )
105111 }
106112
@@ -116,7 +122,7 @@ func testNameFormat(out io.Writer) EventFormatter {
116122 pkg .WriteOutputTo (buf , tc .ID )
117123 return formatTest ()
118124
119- case event .Action == ActionPass :
125+ case event .Action == ActionPass || event . Action == ActionSkip :
120126 return formatTest ()
121127 }
122128 return nil
@@ -307,12 +313,77 @@ func NewEventFormatter(out io.Writer, format string, formatOpts FormatOptions) E
307313 case "dots-v2" :
308314 return newDotFormatter (out , formatOpts )
309315 case "testname" , "short-verbose" :
316+ if os .Getenv ("GITHUB_ACTIONS" ) == "true" {
317+ return githubActionsFormat (out )
318+ }
310319 return testNameFormat (out )
311320 case "pkgname" , "short" :
312321 return pkgNameFormat (out , formatOpts )
313322 case "pkgname-and-test-fails" , "short-with-failures" :
314323 return pkgNameWithFailuresFormat (out , formatOpts )
324+ case "github-actions" , "github-action" :
325+ return githubActionsFormat (out )
315326 default :
316327 return nil
317328 }
318329}
330+
331+ func githubActionsFormat (out io.Writer ) EventFormatter {
332+ buf := bufio .NewWriter (out )
333+
334+ type name struct {
335+ Package string
336+ Test string
337+ }
338+ output := map [name ][]string {}
339+
340+ return eventFormatterFunc (func (event TestEvent , exec * Execution ) error {
341+ key := name {Package : event .Package , Test : event .Test }
342+
343+ // test case output
344+ if event .Test != "" && event .Action == ActionOutput {
345+ if ! isFramingLine (event .Output , event .Test ) {
346+ output [key ] = append (output [key ], event .Output )
347+ }
348+ return nil
349+ }
350+
351+ // test case end event
352+ if event .Test != "" && event .Action .IsTerminal () {
353+ if len (output [key ]) > 0 {
354+ buf .WriteString ("::group::" )
355+ } else {
356+ buf .WriteString (" " )
357+ }
358+ testNameFormatTestEvent (buf , event )
359+
360+ for _ , item := range output [key ] {
361+ buf .WriteString (item )
362+ }
363+ if len (output [key ]) > 0 {
364+ buf .WriteString ("\n ::endgroup::\n " )
365+ }
366+ delete (output , key )
367+ return buf .Flush ()
368+ }
369+
370+ // package event
371+ if ! event .Action .IsTerminal () {
372+ return nil
373+ }
374+
375+ result := colorEvent (event )(strings .ToUpper (string (event .Action )))
376+ pkg := exec .Package (event .Package )
377+ if event .Action == ActionSkip || (event .Action == ActionPass && pkg .Total == 0 ) {
378+ event .Action = ActionSkip // always color these as skip actions
379+ result = colorEvent (event )("EMPTY" )
380+ }
381+
382+ buf .WriteString (" " )
383+ buf .WriteString (result )
384+ buf .WriteString (" Package " )
385+ buf .WriteString (packageLine (event , exec .Package (event .Package )))
386+ buf .WriteString ("\n " )
387+ return buf .Flush ()
388+ })
389+ }
0 commit comments