Skip to content

Commit

Permalink
[breaking] fix: remove tree config lookup (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bikappa authored Feb 27, 2023
1 parent 5e251e8 commit e7ba99f
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 178 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
}
71 changes: 4 additions & 67 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,38 +60,15 @@ 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
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
target := filepath.Join(tmp, "foo", "bar", "baz")
os.MkdirAll(target, os.ModePerm)
require.Nil(t, os.Chdir(target))

// Create a config file
f, err := os.Create(filepath.Join(target, "..", "..", "arduino-cli.yaml"))
require.Nil(t, err)
f.Close()

configFile = FindConfigFileInArgsOrWorkingDirectory([]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{})
require.Equal(t, filepath.Join(target, "arduino-cli.yaml"), configFile)
}
5 changes: 5 additions & 0 deletions docs/UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Here you can find a list of migration guides to handle breaking changes between releases of the CLI.

## 0.32.0

Configuration file lookup in current working directory and its parents is dropped. The command line flag `--config-file`
must be specified to use an alternative configuration file from the one in the data directory.

## 0.31.0

### Added `post_install` script support for tools
Expand Down
2 changes: 0 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ The configuration file must be named `arduino-cli`, with the appropriate file ex
Configuration files in the following locations are recognized by Arduino CLI:

1. Location specified by the [`--config-file`][arduino cli command reference] command line flag
1. Current working directory
1. Any parent directory of the current working directory (more immediate parents having higher precedence)
1. Arduino CLI data directory (as configured by `directories.data`)

If multiple configuration files are present, the one highest on the above list is used. Configuration files are not
Expand Down
2 changes: 1 addition & 1 deletion docsgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {

os.MkdirAll(os.Args[1], 0755) // Create the output folder if it doesn't already exist

configuration.Settings = configuration.Init(configuration.FindConfigFileInArgsOrWorkingDirectory(os.Args))
configuration.Settings = configuration.Init(configuration.FindConfigFileInArgs(os.Args))
cli := cli.NewCommand()
cli.DisableAutoGenTag = true // Disable addition of auto-generated date stamp
err := doc.GenMarkdownTree(cli, os.Args[1])
Expand Down
36 changes: 22 additions & 14 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ func compileWithExportBinariesConfig(t *testing.T, env *integrationtest.Environm
defer cli.WorkingDir().Join("arduino-cli.yaml").Remove()

// Test if arduino-cli config file written in the previous run has the `always_export_binaries` flag set.
stdout, _, err := cli.Run("config", "dump", "--format", "json")
stdout, _, err := cli.Run("config", "dump", "--format", "json", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
requirejson.Contains(t, stdout, `
{
Expand All @@ -536,7 +536,7 @@ func compileWithExportBinariesConfig(t *testing.T, env *integrationtest.Environm
}`)

// Test compilation with export binaries env var set
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String())
_, _, err = cli.Run("compile", "-b", fqbn, "--config-file", "arduino-cli.yaml", sketchPath.String())
require.NoError(t, err)
require.DirExists(t, sketchPath.Join("build").String())

Expand All @@ -563,7 +563,7 @@ func compileWithInvalidUrl(t *testing.T, env *integrationtest.Environment, cli *
require.NoError(t, err)
defer cli.WorkingDir().Join("arduino-cli.yaml").Remove()

_, stderr, err := cli.Run("compile", "-b", fqbn, sketchPath.String())
_, stderr, err := cli.Run("compile", "-b", fqbn, "--config-file", "arduino-cli.yaml", sketchPath.String())
require.NoError(t, err)
require.Contains(t, string(stderr), "Error initializing instance: Loading index file: loading json index file")
expectedIndexfile := cli.DataDir().Join("package_example_index.json")
Expand Down Expand Up @@ -813,10 +813,10 @@ func TestCompileWithCustomLibraries(t *testing.T) {
require.NoError(t, err)

// Init the environment explicitly
_, _, err = cli.Run("update")
_, _, err = cli.Run("update", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "esp8266:esp8266")
_, _, err = cli.Run("core", "install", "esp8266:esp8266", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

sketchName := "sketch_with_multiple_custom_libraries"
Expand All @@ -825,7 +825,12 @@ func TestCompileWithCustomLibraries(t *testing.T) {

firstLib := sketchPath.Join("libraries1")
secondLib := sketchPath.Join("libraries2")
_, _, err = cli.Run("compile", "--libraries", firstLib.String(), "--libraries", secondLib.String(), "-b", fqbn, sketchPath.String())
_, _, err = cli.Run("compile", "--libraries",
firstLib.String(),
"--libraries", secondLib.String(),
"-b", fqbn,
"--config-file", "arduino-cli.yaml",
sketchPath.String())
require.NoError(t, err)
}

Expand All @@ -839,26 +844,26 @@ func TestCompileWithArchivesAndLongPaths(t *testing.T) {
require.NoError(t, err)

// Init the environment explicitly
_, _, err = cli.Run("update")
_, _, err = cli.Run("update", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

// Install core to compile
_, _, err = cli.Run("core", "install", "esp8266:esp8266@2.7.4")
_, _, err = cli.Run("core", "install", "esp8266:esp8266@2.7.4", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

// Install test library
_, _, err = cli.Run("lib", "install", "ArduinoIoTCloud")
_, _, err = cli.Run("lib", "install", "ArduinoIoTCloud", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

stdout, _, err := cli.Run("lib", "examples", "ArduinoIoTCloud", "--format", "json")
stdout, _, err := cli.Run("lib", "examples", "ArduinoIoTCloud", "--format", "json", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
var libOutput []map[string]interface{}
err = json.Unmarshal(stdout, &libOutput)
require.NoError(t, err)
sketchPath := paths.New(libOutput[0]["library"].(map[string]interface{})["install_dir"].(string))
sketchPath = sketchPath.Join("examples", "ArduinoIoTCloud-Advanced")

_, _, err = cli.Run("compile", "-b", "esp8266:esp8266:huzzah", sketchPath.String())
_, _, err = cli.Run("compile", "-b", "esp8266:esp8266:huzzah", sketchPath.String(), "--config-file", "arduino-cli.yaml")
require.NoError(t, err)
}

Expand Down Expand Up @@ -908,16 +913,19 @@ func TestCompileWithFullyPrecompiledLibrary(t *testing.T) {
// https://arduino.github.io/arduino-cli/latest/library-specification/#precompiled-binaries
wd, err := paths.Getwd()
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "--zip-path", wd.Parent().Join("testdata", "Arduino_TensorFlowLite-2.1.0-ALPHA-precompiled.zip").String())
_, _, err = cli.Run("lib", "install",
"--zip-path", wd.Parent().Join("testdata", "Arduino_TensorFlowLite-2.1.0-ALPHA-precompiled.zip").String(),
"--config-file", "arduino-cli.yaml",
)
require.NoError(t, err)
sketchFolder := cli.SketchbookDir().Join("libraries", "Arduino_TensorFlowLite", "examples", "hello_world")

// Install example dependency
_, _, err = cli.Run("lib", "install", "Arduino_LSM9DS1")
_, _, err = cli.Run("lib", "install", "Arduino_LSM9DS1", "--config-file", "arduino-cli.yaml")
require.NoError(t, err)

// Compile and verify dependencies detection for fully precompiled library is skipped
stdout, _, err := cli.Run("compile", "-b", fqbn, sketchFolder.String(), "-v")
stdout, _, err := cli.Run("compile", "-b", fqbn, "--config-file", "arduino-cli.yaml", sketchFolder.String(), "-v")
require.NoError(t, err)
require.Contains(t, string(stdout), "Skipping dependencies detection for precompiled library Arduino_TensorFlowLite")
}
Expand Down
Loading

0 comments on commit e7ba99f

Please sign in to comment.