From 0f7cca45b5e31ae2549dd9bdbc4d7493470932bc Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Tue, 20 Sep 2022 16:14:48 +0200 Subject: [PATCH 1/3] 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. --- internal/integrationtest/arduino-cli.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/integrationtest/arduino-cli.go b/internal/integrationtest/arduino-cli.go index fc6894237d0..8bfdc066392 100644 --- a/internal/integrationtest/arduino-cli.go +++ b/internal/integrationtest/arduino-cli.go @@ -43,8 +43,8 @@ func init() { // FindRepositoryRootPath returns the repository root path func FindRepositoryRootPath(t *testing.T) *paths.Path { - repoRootPath := paths.New(".") - require.NoError(t, repoRootPath.ToAbs()) + repoRootPath, err := paths.Getwd() + require.NoError(t, err) for !repoRootPath.Join(".git").Exist() { require.Contains(t, repoRootPath.String(), "arduino-cli", "Error searching for repository root path") repoRootPath = repoRootPath.Parent() @@ -75,6 +75,7 @@ type ArduinoCLI struct { stagingDir *paths.Path dataDir *paths.Path sketchbookDir *paths.Path + workingDir *paths.Path daemonAddr string daemonConn *grpc.ClientConn daemonClient commands.ArduinoCoreServiceClient @@ -96,6 +97,7 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC dataDir: env.RootDir().Join("A"), sketchbookDir: env.RootDir().Join("Arduino"), stagingDir: env.RootDir().Join("Arduino15/staging"), + workingDir: env.RootDir(), } if config.UseSharedStagingFolder { cli.stagingDir = env.SharedDownloadsDir() @@ -130,6 +132,11 @@ func (cli *ArduinoCLI) SketchbookDir() *paths.Path { return cli.sketchbookDir } +// WorkingDir returns the working directory +func (cli *ArduinoCLI) WorkingDir() *paths.Path { + return cli.workingDir +} + // CopySketch copies a sketch inside the testing environment and returns its path func (cli *ArduinoCLI) CopySketch(sketchName string) *paths.Path { p, err := paths.Getwd() @@ -180,6 +187,7 @@ func (cli *ArduinoCLI) RunWithCustomEnv(env map[string]string, args ...string) ( cli.t.NoError(err) _, err = cliProc.StdinPipe() cli.t.NoError(err) + cliProc.SetDir(cli.WorkingDir().String()) cli.t.NoError(cliProc.Start()) From 6a77eda7f7eb6635a41dea633bf935a2e3607053 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Mon, 26 Sep 2022 17:18:49 +0200 Subject: [PATCH 2/3] Fix errors related to the change of the working directory --- internal/integrationtest/compile/compile_part_1_test.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/integrationtest/compile/compile_part_1_test.go b/internal/integrationtest/compile/compile_part_1_test.go index b7e8ad574e6..75cad418fae 100644 --- a/internal/integrationtest/compile/compile_part_1_test.go +++ b/internal/integrationtest/compile/compile_part_1_test.go @@ -164,10 +164,7 @@ func TestOutputFlagDefaultPath(t *testing.T) { require.NoError(t, err) // Test the --output-dir flag defaulting to current working dir - workingDir, err := paths.Getwd() - require.NoError(t, err) - target := workingDir.Join("test") - defer target.RemoveAll() + target := cli.WorkingDir().Join("test") _, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--output-dir", "test") require.NoError(t, err) require.DirExists(t, target.String()) From 12ea44c6a16574b7216aaea79fd1f7fcebfa90a3 Mon Sep 17 00:00:00 2001 From: Matteo Pologruto Date: Mon, 26 Sep 2022 17:38:49 +0200 Subject: [PATCH 3/3] Use absolute path to create daemon environment --- internal/integrationtest/daemon/daemon_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/integrationtest/daemon/daemon_test.go b/internal/integrationtest/daemon/daemon_test.go index f43dbcccc22..a399b960b06 100644 --- a/internal/integrationtest/daemon/daemon_test.go +++ b/internal/integrationtest/daemon/daemon_test.go @@ -19,7 +19,6 @@ import ( "testing" "github.com/arduino/arduino-cli/internal/integrationtest" - "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" "go.bug.st/testsuite" ) @@ -31,7 +30,7 @@ func createEnvForDaemon(t *testing.T) (*testsuite.Environment, *integrationtest. env := testsuite.NewEnvironment(t) cli := integrationtest.NewArduinoCliWithinEnvironment(env, &integrationtest.ArduinoCLIConfig{ - ArduinoCLIPath: paths.New("..", "..", "..", "arduino-cli"), + ArduinoCLIPath: integrationtest.FindRepositoryRootPath(t).Join("arduino-cli"), UseSharedStagingFolder: true, })