Skip to content

integration test: Added function to run arduino-cli with custom env #1877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions internal/integrationtest/arduino-cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type ArduinoCLI struct {
path *paths.Path
t *require.Assertions
proc *executils.Process
cliEnvVars []string
cliEnvVars map[string]string
cliConfigPath *paths.Path
stagingDir *paths.Path
dataDir *paths.Path
Expand Down Expand Up @@ -97,11 +97,11 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
cli.stagingDir = env.SharedDownloadsDir()
}

cli.cliEnvVars = []string{
"LANG=en",
fmt.Sprintf("ARDUINO_DATA_DIR=%s", cli.dataDir),
fmt.Sprintf("ARDUINO_DOWNLOADS_DIR=%s", cli.stagingDir),
fmt.Sprintf("ARDUINO_SKETCHBOOK_DIR=%s", cli.sketchbookDir),
cli.cliEnvVars = map[string]string{
"LANG": "en",
"ARDUINO_DATA_DIR": cli.dataDir.String(),
"ARDUINO_DOWNLOADS_DIR": cli.stagingDir.String(),
"ARDUINO_SKETCHBOOK_DIR": cli.sketchbookDir.String(),
}
env.RegisterCleanUpCallback(cli.CleanUp)
return cli
Expand All @@ -128,11 +128,35 @@ func (cli *ArduinoCLI) SketchbookDir() *paths.Path {

// Run executes the given arduino-cli command and returns the output.
func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
return cli.RunWithCustomEnv(cli.cliEnvVars, args...)
}

// GetDefaultEnv returns a copy of the default execution env used with the Run method.
func (cli *ArduinoCLI) GetDefaultEnv() map[string]string {
res := map[string]string{}
for k, v := range cli.cliEnvVars {
res[k] = v
}
return res
}

// convertEnvForExecutils returns a string array made of "key=value" strings
// with (key,value) pairs obtained from the given map.
func (cli *ArduinoCLI) convertEnvForExecutils(env map[string]string) []string {
envVars := []string{}
for k, v := range env {
envVars = append(envVars, fmt.Sprintf("%s=%s", k, v))
}
return envVars
}

// RunWithCustomEnv executes the given arduino-cli command with the given custom env and returns the output.
func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ([]byte, []byte, error) {
if cli.cliConfigPath != nil {
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
}
fmt.Println(color.HiBlackString(">>> Running: ") + color.HiYellowString("%s %s", cli.path, strings.Join(args, " ")))
cliProc, err := executils.NewProcessFromPath(cli.cliEnvVars, cli.path, args...)
cliProc, err := executils.NewProcessFromPath(cli.convertEnvForExecutils(env), cli.path, args...)
cli.t.NoError(err)
stdout, err := cliProc.StdoutPipe()
cli.t.NoError(err)
Expand Down Expand Up @@ -177,7 +201,7 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
if verbose {
args = append(args, "-v", "--log-level", "debug")
}
cliProc, err := executils.NewProcessFromPath(cli.cliEnvVars, cli.path, args...)
cliProc, err := executils.NewProcessFromPath(cli.convertEnvForExecutils(cli.cliEnvVars), cli.path, args...)
cli.t.NoError(err)
stdout, err := cliProc.StdoutPipe()
cli.t.NoError(err)
Expand Down