@@ -22,6 +22,7 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"io"
25
+ "maps"
25
26
"os"
26
27
"runtime"
27
28
"strings"
@@ -190,12 +191,16 @@ func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
190
191
return cli .RunWithCustomEnv (cli .cliEnvVars , args ... )
191
192
}
192
193
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
+
193
200
// GetDefaultEnv returns a copy of the default execution env used with the Run method.
194
201
func (cli * ArduinoCLI ) GetDefaultEnv () map [string ]string {
195
202
res := map [string ]string {}
196
- for k , v := range cli .cliEnvVars {
197
- res [k ] = v
198
- }
203
+ maps .Copy (res , cli .cliEnvVars )
199
204
return res
200
205
}
201
206
@@ -324,8 +329,13 @@ func (cli *ArduinoCLI) InstallMockedAvrdude(t *testing.T) {
324
329
325
330
// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
326
331
func (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 ) {
327
337
var stdoutBuf , stderrBuf bytes.Buffer
328
- err := cli .run (& stdoutBuf , & stderrBuf , nil , env , args ... )
338
+ err := cli .run (ctx , & stdoutBuf , & stderrBuf , nil , env , args ... )
329
339
330
340
errBuf := stderrBuf .Bytes ()
331
341
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) (
336
346
// RunWithCustomInput executes the given arduino-cli command pushing the given input stream and returns the output.
337
347
func (cli * ArduinoCLI ) RunWithCustomInput (in io.Reader , args ... string ) ([]byte , []byte , error ) {
338
348
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 ... )
340
350
341
351
errBuf := stderrBuf .Bytes ()
342
352
cli .t .NotContains (string (errBuf ), "panic: runtime error:" , "arduino-cli panicked" )
343
353
344
354
return stdoutBuf .Bytes (), errBuf , err
345
355
}
346
356
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 {
348
358
if cli .cliConfigPath != nil {
349
359
args = append ([]string {"--config-file" , cli .cliConfigPath .String ()}, args ... )
350
360
}
@@ -402,8 +412,8 @@ func (cli *ArduinoCLI) run(stdoutBuff, stderrBuff io.Writer, stdinBuff io.Reader
402
412
}
403
413
}()
404
414
}
415
+ cliErr := cliProc .WaitWithinContext (ctx )
405
416
wg .Wait ()
406
- cliErr := cliProc .Wait ()
407
417
fmt .Fprintln (terminalOut , color .HiBlackString ("<<< Run completed (err = %v)" , cliErr ))
408
418
409
419
return cliErr
0 commit comments