Skip to content

Commit e07d9af

Browse files
committed
Added integration test function to run with custom env
1 parent 736608f commit e07d9af

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

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

+32-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type ArduinoCLI struct {
6666
path *paths.Path
6767
t *require.Assertions
6868
proc *executils.Process
69-
cliEnvVars []string
69+
cliEnvVars map[string]string
7070
cliConfigPath *paths.Path
7171
stagingDir *paths.Path
7272
dataDir *paths.Path
@@ -97,11 +97,11 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
9797
cli.stagingDir = env.SharedDownloadsDir()
9898
}
9999

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

129129
// Run executes the given arduino-cli command and returns the output.
130130
func (cli *ArduinoCLI) Run(args ...string) ([]byte, []byte, error) {
131+
return cli.RunWithCustomEnv(cli.cliEnvVars, args...)
132+
}
133+
134+
// GetDefaultEnv returns a copy of the default execution env used with the Run method.
135+
func (cli *ArduinoCLI) GetDefaultEnv() map[string]string {
136+
res := map[string]string{}
137+
for k, v := range cli.cliEnvVars {
138+
res[k] = v
139+
}
140+
return res
141+
}
142+
143+
// convertEnvForExecutils returns a string array made of "key=value" strings
144+
// with (key,value) pairs obtained from the given map.
145+
func (cli *ArduinoCLI) convertEnvForExecutils(env map[string]string) []string {
146+
envVars := []string{}
147+
for k, v := range env {
148+
envVars = append(envVars, fmt.Sprintf("%s=%s", k, v))
149+
}
150+
return envVars
151+
}
152+
153+
// RunWithCustomEnv executes the given arduino-cli command adding the given custom env and returns the output.
154+
func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ([]byte, []byte, error) {
131155
if cli.cliConfigPath != nil {
132156
args = append([]string{"--config-file", cli.cliConfigPath.String()}, args...)
133157
}
134158
fmt.Println(color.HiBlackString(">>> Running: ") + color.HiYellowString("%s %s", cli.path, strings.Join(args, " ")))
135-
cliProc, err := executils.NewProcessFromPath(cli.cliEnvVars, cli.path, args...)
159+
cliProc, err := executils.NewProcessFromPath(cli.convertEnvForExecutils(env), cli.path, args...)
136160
cli.t.NoError(err)
137161
stdout, err := cliProc.StdoutPipe()
138162
cli.t.NoError(err)
@@ -177,7 +201,7 @@ func (cli *ArduinoCLI) StartDaemon(verbose bool) string {
177201
if verbose {
178202
args = append(args, "-v", "--log-level", "debug")
179203
}
180-
cliProc, err := executils.NewProcessFromPath(cli.cliEnvVars, cli.path, args...)
204+
cliProc, err := executils.NewProcessFromPath(cli.convertEnvForExecutils(cli.cliEnvVars), cli.path, args...)
181205
cli.t.NoError(err)
182206
stdout, err := cliProc.StdoutPipe()
183207
cli.t.NoError(err)

0 commit comments

Comments
 (0)