Skip to content

Commit ecc1ece

Browse files
authored
[skip-changelog] Parallelize upload_mock integration tests (#2444)
* Parallelize upload_mock integration tests * Accumulate output in integration test' runs of arduino-cli Otherwise the output may be interleaved if tests are run in parallel.
1 parent ab03161 commit ecc1ece

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

Diff for: internal/integrationtest/arduino-cli.go

+16-9
Original file line numberDiff line numberDiff line change
@@ -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
}

Diff for: internal/integrationtest/upload_mock/upload_mock_test.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type parametersMap struct {
4444

4545
func TestUploadSketch(t *testing.T) {
4646
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
47-
defer env.CleanUp()
47+
t.Cleanup(env.CleanUp)
4848

4949
indexes := []string{
5050
"https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json",
@@ -655,13 +655,14 @@ func TestUploadSketch(t *testing.T) {
655655
sketchPath := cli.SketchbookDir().Join(sketchName)
656656
_, _, err := cli.Run("sketch", "new", sketchPath.String())
657657
require.NoError(t, err)
658+
buildDir := generateBuildDir(sketchPath, t)
659+
t.Cleanup(func() { buildDir.RemoveAll() })
658660

659-
var stdout []byte
660-
661-
for i, test := range testParameters {
661+
for i, _test := range testParameters {
662+
test := _test
662663
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
663-
buildDir := generateBuildDir(sketchPath, t)
664-
defer buildDir.RemoveAll()
664+
t.Parallel()
665+
var stdout []byte
665666
if test.Programmer != "" {
666667
if test.UploadPort != "" {
667668
stdout, _, err = cli.Run("upload", "-p", test.UploadPort, "-P", test.Programmer, "-b", test.Fqbn, sketchPath.String(), "--dry-run", "-v")
@@ -686,10 +687,11 @@ func TestUploadSketch(t *testing.T) {
686687
})
687688
}
688689

689-
for i, test := range testParametersMap {
690+
for i, _test := range testParametersMap {
691+
test := _test
690692
t.Run(fmt.Sprintf("WithMap%d", i), func(t *testing.T) {
691-
buildDir := generateBuildDir(sketchPath, t)
692-
defer buildDir.RemoveAll()
693+
t.Parallel()
694+
var stdout []byte
693695
if test.Programmer != "" {
694696
if test.UploadPort != "" {
695697
stdout, _, err = cli.Run("upload", "-p", test.UploadPort, "-P", test.Programmer, "-b", test.Fqbn, sketchPath.String(), "--dry-run", "-v")

0 commit comments

Comments
 (0)