@@ -22,6 +22,7 @@ import (
2222 "errors"
2323 "fmt"
2424 "io"
25+ "maps"
2526 "os"
2627 "runtime"
2728 "strings"
@@ -190,12 +191,16 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
190191 return cli .RunWithCustomEnv (cli .cliEnvVars , args ... )
191192}
192193
194+ // RunWithContext executes the given arduino-cli command with the given context and returns the output.
195+ // If the context is canceled, the command is killed.
196+ func (cli * ArduinoCLI ) RunWithContext (ctx context.Context , args ... string ) ([]byte , []byte , error ) {
197+ return cli .RunWithCustomEnvContext (ctx , cli .cliEnvVars , args ... )
198+ }
199+
193200// GetDefaultEnv returns a copy of the default execution env used with the Run method.
194201func (cli * ArduinoCLI ) GetDefaultEnv () map [string ]string {
195202 res := map [string ]string {}
196- for k , v := range cli .cliEnvVars {
197- res [k ] = v
198- }
203+ maps .Copy (res , cli .cliEnvVars )
199204 return res
200205}
201206
@@ -324,8 +329,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {
324329
325330// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
326331func (cli * ArduinoCLI ) RunWithCustomEnv (env map [string ]string , args ... string ) ([]byte , []byte , error ) {
332+ return cli .RunWithCustomEnvContext (context .Background (), env , args ... )
333+ }
334+
335+ // RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
336+ func (cli * ArduinoCLI ) RunWithCustomEnvContext (ctx context.Context , env map [string ]string , args ... string ) ([]byte , []byte , error ) {
327337 var stdoutBuf , stderrBuf bytes.Buffer
328- err := cli .run (& stdoutBuf , & stderrBuf , nil , env , args ... )
338+ err := cli .run (ctx , & stdoutBuf , & stderrBuf , nil , env , args ... )
329339
330340 errBuf := stderrBuf .Bytes ()
331341 cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
@@ -336,15 +346,15 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) (
336346// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
337347func (cli * ArduinoCLI ) RunWithCustomInput (in io.Reader , args ... string ) ([]byte , []byte , error ) {
338348 var stdoutBuf , stderrBuf bytes.Buffer
339- err := cli .run (& stdoutBuf , & stderrBuf , in , cli .cliEnvVars , args ... )
349+ err := cli .run (context . Background (), & stdoutBuf , & stderrBuf , in , cli .cliEnvVars , args ... )
340350
341351 errBuf := stderrBuf .Bytes ()
342352 cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
343353
344354 return stdoutBuf .Bytes (), errBuf , err
345355}
346356
347- func (cli * ArduinoCLI ) run (stdoutBuff , stderrBuff io.Writer , stdinBuff io.Reader , env map [string ]string , args ... string ) error {
357+ func (cli * ArduinoCLI ) run (ctx context. Context , stdoutBuff , stderrBuff io.Writer , stdinBuff io.Reader , env map [string ]string , args ... string ) error {
348358 if cli .cliConfigPath != nil {
349359 args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
350360 }
@@ -402,8 +412,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
402412 }
403413 }()
404414 }
415+ cliErr := cliProc .WaitWithinContext (ctx )
405416 wg .Wait ()
406- cliErr := cliProc .Wait ()
407417 fmt .Fprintln (terminalOut , color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
408418
409419 return cliErr
0 commit comments