Unverified Commit baa322f9 authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

[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
parent 6c3755c7
......@@ -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())
......
......@@ -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())
......
......@@ -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,
})
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment