Skip to content

Commit

Permalink
revert: removed config file lookup in cwd and parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Bianconi committed Feb 22, 2023
1 parent c95f890 commit 4d787f1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 67 deletions.
22 changes: 2 additions & 20 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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
}
52 changes: 6 additions & 46 deletions configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"path/filepath"
"testing"

paths "github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
)

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -124,14 +84,14 @@ 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
f, err = os.Create(filepath.Join(target, "arduino-cli.yaml"))
require.Nil(t, err)
f.Close()

configFile = FindConfigFileInArgsOrWorkingDirectory([]string{})
configFile = FindConfigFileInArgs([]string{})
require.Equal(t, filepath.Join(target, "arduino-cli.yaml"), configFile)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4d787f1

Please sign in to comment.