From 4d787f150335a81ef4d2dcfb213444355e3a4cf5 Mon Sep 17 00:00:00 2001 From: Luca Bianconi Date: Wed, 22 Feb 2023 18:19:37 +0100 Subject: [PATCH] revert: removed config file lookup in cwd and parents --- configuration/configuration.go | 22 ++---------- configuration/configuration_test.go | 52 ++++------------------------- main.go | 2 +- 3 files changed, 9 insertions(+), 67 deletions(-) diff --git a/configuration/configuration.go b/configuration/configuration.go index 0afcd015bf3..3593f4111b7 100644 --- a/configuration/configuration.go +++ b/configuration/configuration.go @@ -136,9 +136,9 @@ func GetDefaultBuiltinLibrariesDir() string { return filepath.Join(getDefaultArduinoDataDir(), "libraries") } -// FindConfigFileInArgsOrWorkingDirectory returns the config file path using the +// FindConfigFileInArgs returns the config file path using the // argument '--config-file' (if specified) or looking in the current working dir -func FindConfigFileInArgsOrWorkingDirectory(args []string) string { +func FindConfigFileInArgs(args []string) string { // Look for '--config-file' argument for i, arg := range args { if arg == "--config-file" { @@ -147,23 +147,5 @@ func FindConfigFileInArgsOrWorkingDirectory(args []string) string { } } } - - // Look into current working directory - if cwd, err := paths.Getwd(); err != nil { - return "" - } else if configFile := searchConfigTree(cwd); configFile != nil { - return configFile.Join("arduino-cli.yaml").String() - } return "" } - -func searchConfigTree(cwd *paths.Path) *paths.Path { - // go back up to root and search for the config file - for _, path := range cwd.Parents() { - if path.Join("arduino-cli.yaml").Exist() { - return path - } - } - - return nil -} diff --git a/configuration/configuration_test.go b/configuration/configuration_test.go index 880b40a32f2..a060d5a4de3 100644 --- a/configuration/configuration_test.go +++ b/configuration/configuration_test.go @@ -21,7 +21,6 @@ import ( "path/filepath" "testing" - paths "github.com/arduino/go-paths-helper" "github.com/stretchr/testify/require" ) @@ -39,45 +38,6 @@ func tmpDirOrDie() string { return dir } -func TestSearchConfigTreeNotFound(t *testing.T) { - tmp := tmpDirOrDie() - require.Empty(t, searchConfigTree(paths.New(tmp))) -} - -func TestSearchConfigTreeSameFolder(t *testing.T) { - tmp := tmpDirOrDie() - defer os.RemoveAll(tmp) - _, err := os.Create(filepath.Join(tmp, "arduino-cli.yaml")) - require.Nil(t, err) - require.Equal(t, tmp, searchConfigTree(paths.New(tmp)).String()) -} - -func TestSearchConfigTreeInParent(t *testing.T) { - tmp := tmpDirOrDie() - defer os.RemoveAll(tmp) - target := filepath.Join(tmp, "foo", "bar") - err := os.MkdirAll(target, os.ModePerm) - require.Nil(t, err) - _, err = os.Create(filepath.Join(tmp, "arduino-cli.yaml")) - require.Nil(t, err) - require.Equal(t, tmp, searchConfigTree(paths.New(target)).String()) -} - -var result *paths.Path - -func BenchmarkSearchConfigTree(b *testing.B) { - tmp := tmpDirOrDie() - defer os.RemoveAll(tmp) - target := filepath.Join(tmp, "foo", "bar", "baz") - os.MkdirAll(target, os.ModePerm) - - var s *paths.Path - for n := 0; n < b.N; n++ { - s = searchConfigTree(paths.New(target)) - } - result = s -} - func TestInit(t *testing.T) { tmp := tmpDirOrDie() defer os.RemoveAll(tmp) @@ -100,16 +60,16 @@ func TestInit(t *testing.T) { } func TestFindConfigFile(t *testing.T) { - configFile := FindConfigFileInArgsOrWorkingDirectory([]string{"--config-file"}) + configFile := FindConfigFileInArgs([]string{"--config-file"}) require.Equal(t, "", configFile) - configFile = FindConfigFileInArgsOrWorkingDirectory([]string{"--config-file", "some/path/to/config"}) + configFile = FindConfigFileInArgs([]string{"--config-file", "some/path/to/config"}) require.Equal(t, "some/path/to/config", configFile) - configFile = FindConfigFileInArgsOrWorkingDirectory([]string{"--config-file", "some/path/to/config/arduino-cli.yaml"}) + configFile = FindConfigFileInArgs([]string{"--config-file", "some/path/to/config/arduino-cli.yaml"}) require.Equal(t, "some/path/to/config/arduino-cli.yaml", configFile) - configFile = FindConfigFileInArgsOrWorkingDirectory([]string{}) + configFile = FindConfigFileInArgs([]string{}) require.Equal(t, "", configFile) // Create temporary directories @@ -124,7 +84,7 @@ func TestFindConfigFile(t *testing.T) { require.Nil(t, err) f.Close() - configFile = FindConfigFileInArgsOrWorkingDirectory([]string{}) + configFile = FindConfigFileInArgs([]string{}) require.Equal(t, filepath.Join(tmp, "foo", "arduino-cli.yaml"), configFile) // Create another config file @@ -132,6 +92,6 @@ func TestFindConfigFile(t *testing.T) { require.Nil(t, err) f.Close() - configFile = FindConfigFileInArgsOrWorkingDirectory([]string{}) + configFile = FindConfigFileInArgs([]string{}) require.Equal(t, filepath.Join(target, "arduino-cli.yaml"), configFile) } diff --git a/main.go b/main.go index 4063632a42d..55f139c5ca1 100644 --- a/main.go +++ b/main.go @@ -25,7 +25,7 @@ import ( ) func main() { - configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args)) + configuration.Settings = configuration.Init(configuration.FindConfigFileInArgs(os.Args)) i18n.Init(configuration.Settings.GetString("locale")) arduinoCmd := cli.NewCommand() if err := arduinoCmd.Execute(); err != nil {