@@ -299,13 +299,20 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
299299 args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
300300 }
301301
302+ // Accumulate all output to terminal and spit-out all at once at the end of the test
303+ // This allows to correctly group test output when running t.Parallel() tests.
304+ terminalOut := new (bytes.Buffer )
305+ defer func () {
306+ fmt .Print (terminalOut .String ())
307+ }()
308+
302309 // Github-actions workflow tags to fold log lines
303310 if os .Getenv ("GITHUB_ACTIONS" ) != "" {
304- fmt .Printf ( "::group::Running %s\n " , strings .Join (args , " " ))
305- defer fmt .Println ( "::endgroup::" )
311+ fmt .Fprintf ( terminalOut , "::group::Running %s\n " , strings .Join (args , " " ))
312+ defer fmt .Fprintln ( terminalOut , "::endgroup::" )
306313 }
307314
308- fmt .Println ( color .HiBlackString (">>> Running: " ) + color .HiYellowString ("%s %s" , cli .path , strings .Join (args , " " )))
315+ fmt .Fprintln ( terminalOut , color .HiBlackString (">>> Running: " )+ color .HiYellowString ("%s %s" , cli .path , strings .Join (args , " " )))
309316 cliProc , err := executils .NewProcessFromPath (cli .convertEnvForExecutils (env ), cli .path , args ... )
310317 cli .t .NoError (err )
311318 stdout , err := cliProc .StdoutPipe ()
@@ -325,29 +332,29 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
325332 if stdoutBuff == nil {
326333 stdoutBuff = io .Discard
327334 }
328- if _ , err := io .Copy (stdoutBuff , io .TeeReader (stdout , os . Stdout )); err != nil {
329- fmt .Println ( color .HiBlackString ("<<< stdout copy error:" ), err )
335+ if _ , err := io .Copy (stdoutBuff , io .TeeReader (stdout , terminalOut )); err != nil {
336+ fmt .Fprintln ( terminalOut , color .HiBlackString ("<<< stdout copy error:" ), err )
330337 }
331338 }()
332339 go func () {
333340 defer wg .Done ()
334341 if stderrBuff == nil {
335342 stderrBuff = io .Discard
336343 }
337- if _ , err := io .Copy (stderrBuff , io .TeeReader (stderr , os . Stderr )); err != nil {
338- fmt .Println ( color .HiBlackString ("<<< stderr copy error:" ), err )
344+ if _ , err := io .Copy (stderrBuff , io .TeeReader (stderr , terminalOut )); err != nil {
345+ fmt .Fprintln ( terminalOut , color .HiBlackString ("<<< stderr copy error:" ), err )
339346 }
340347 }()
341348 if stdinBuff != nil {
342349 go func () {
343350 if _ , err := io .Copy (stdin , stdinBuff ); err != nil {
344- fmt .Println ( color .HiBlackString ("<<< stdin copy error:" ), err )
351+ fmt .Fprintln ( terminalOut , color .HiBlackString ("<<< stdin copy error:" ), err )
345352 }
346353 }()
347354 }
348355 wg .Wait ()
349356 cliErr := cliProc .Wait ()
350- fmt .Println ( color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
357+ fmt .Fprintln ( terminalOut , color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
351358
352359 return cliErr
353360}
0 commit comments