Skip to content

Commit baa322f

Browse files
[skip-changelog] Set test environment directory as CLI WorkingDir (#1886)
* Set test environment directory as CLI WorkingDir By default, the working directory is the one containing the test.go file. This causes problems when executing commands that have to create files specifically in the working directory, because they either must be deleted manually or the user has to be aware of it and defer a deleting instruction. Furthermore, it messes with tests using relative paths. Setting the environment directory as the CLI's WorkingDir prevents the above mentioned issues from occurring. * Fix errors related to the change of the working directory * Use absolute path to create daemon environment
1 parent 6c3755c commit baa322f

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

internal/integrationtest/arduino-cli.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func init() {
4343

4444
// FindRepositoryRootPath returns the repository root path
4545
func FindRepositoryRootPath(t *testing.T) *paths.Path {
46-
repoRootPath := paths.New(".")
47-
require.NoError(t, repoRootPath.ToAbs())
46+
repoRootPath, err := paths.Getwd()
47+
require.NoError(t, err)
4848
for !repoRootPath.Join(".git").Exist() {
4949
require.Contains(t, repoRootPath.String(), "arduino-cli", "Error searching for repository root path")
5050
repoRootPath = repoRootPath.Parent()
@@ -75,6 +75,7 @@ type ArduinoCLI struct {
7575
stagingDir *paths.Path
7676
dataDir *paths.Path
7777
sketchbookDir *paths.Path
78+
workingDir *paths.Path
7879
daemonAddr string
7980
daemonConn *grpc.ClientConn
8081
daemonClient commands.ArduinoCoreServiceClient
@@ -96,6 +97,7 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
9697
dataDir: env.RootDir().Join("A"),
9798
sketchbookDir: env.RootDir().Join("Arduino"),
9899
stagingDir: env.RootDir().Join("Arduino15/staging"),
100+
workingDir: env.RootDir(),
99101
}
100102
if config.UseSharedStagingFolder {
101103
cli.stagingDir = env.SharedDownloadsDir()
@@ -130,6 +132,11 @@ func (cli *ArduinoCLI) SketchbookDir() *paths.Path {
130132
return cli.sketchbookDir
131133
}
132134

135+
// WorkingDir returns the working directory
136+
func (cli *ArduinoCLI) WorkingDir() *paths.Path {
137+
return cli.workingDir
138+
}
139+
133140
// CopySketch copies a sketch inside the testing environment and returns its path
134141
func (cli *ArduinoCLI) CopySketch(sketchName string) *paths.Path {
135142
p, err := paths.Getwd()
@@ -180,6 +187,7 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) (
180187
cli.t.NoError(err)
181188
_, err = cliProc.StdinPipe()
182189
cli.t.NoError(err)
190+
cliProc.SetDir(cli.WorkingDir().String())
183191

184192
cli.t.NoError(cliProc.Start())
185193

internal/integrationtest/compile/compile_part_1_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,7 @@ func TestOutputFlagDefaultPath(t *testing.T) {
164164
require.NoError(t, err)
165165

166166
// Test the --output-dir flag defaulting to current working dir
167-
workingDir, err := paths.Getwd()
168-
require.NoError(t, err)
169-
target := workingDir.Join("test")
170-
defer target.RemoveAll()
167+
target := cli.WorkingDir().Join("test")
171168
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--output-dir", "test")
172169
require.NoError(t, err)
173170
require.DirExists(t, target.String())

internal/integrationtest/daemon/daemon_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"testing"
2020

2121
"github.com/arduino/arduino-cli/internal/integrationtest"
22-
"github.com/arduino/go-paths-helper"
2322
"github.com/stretchr/testify/require"
2423
"go.bug.st/testsuite"
2524
)
@@ -31,7 +30,7 @@ func createEnvForDaemon(t *testing.T) (*testsuite.Environment, *integrationtest.
3130
env := testsuite.NewEnvironment(t)
3231

3332
cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{
34-
ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"),
33+
ArduinoCLIPath: integrationtest.FindRepositoryRootPath(t).Join("arduino-cli"),
3534
UseSharedStagingFolder: true,
3635
})
3736

0 commit comments

Comments
 (0)