Skip to content

Introduce Viper to manage the configuration file #487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix integration tests
  • Loading branch information
Massimiliano Pippi committed Dec 11, 2019
commit a863c5012d785ae8ec8a1c7442230ce30c75f958
8 changes: 6 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ func preRun(cmd *cobra.Command, args []string) {
}

// override the config path if --config-file was passed
if configFile != "" {
configPath = filepath.Dir(configFile)
if fi, err := os.Stat(configFile); err == nil {
if fi.IsDir() {
configPath = configFile
} else {
configPath = filepath.Dir(configFile)
}
}

// initialize the config system
Expand Down
60 changes: 8 additions & 52 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"runtime"
"testing"

"github.com/spf13/viper"

"github.com/arduino/arduino-cli/cli/feedback"

"bou.ke/monkey"
Expand Down Expand Up @@ -128,25 +130,10 @@ func executeWithArgs(args ...string) (int, []byte) {
var output []byte
var exitCode int
fmt.Printf("RUNNING: %s\n", args)
viper.Reset()

// This closure is here because we won't that the defer are executed after the end of the "executeWithArgs" method
func() {
// Create an empty config for the CLI test in the same dir of the test
conf := paths.New("arduino-cli.yaml")
if conf.Exist() {
panic("config file must not exist already")
}

if err := conf.WriteFile([]byte("board_manager:\n additional_urls:\n")); err != nil {
panic(err)
}

defer func() {
if err := conf.Remove(); err != nil {
panic(err)
}
}()

redirect := &stdOutRedirect{}
redirect.Open()
// re-init feedback so it'll write to our grabber
Expand Down Expand Up @@ -365,28 +352,7 @@ func TestCompileCommandsIntegration(t *testing.T) {
}

func TestInvalidCoreURLIntegration(t *testing.T) {
// override SetUp dirs
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
os.Setenv("ARDUINO_SKETCHBOOK_DIR", tmp)
currSketchbookDir = tmp

configFile := filepath.Join(currDataDir, "arduino-cli.yaml")
err := ioutil.WriteFile(configFile, []byte(`
board_manager:
additional_urls:
- http://www.invalid-domain-asjkdakdhadjkh.com/package_example_index.json
`), os.FileMode(0644))
require.NoError(t, err, "writing dummy config "+configFile)

err = ioutil.WriteFile(filepath.Join(currDataDir, "package_index.json"), []byte(`{ "packages": [] }`), os.FileMode(0644))
require.NoError(t, err, "Writing empty json index file")

err = ioutil.WriteFile(filepath.Join(currDataDir, "package_example_index.json"), []byte(`{ "packages": [] }`), os.FileMode(0644))
require.NoError(t, err, "Writing empty json index file")

err = ioutil.WriteFile(filepath.Join(currDataDir, "library_index.json"), []byte(`{ "libraries": [] }`), os.FileMode(0644))
require.NoError(t, err, "Writing empty json index file")
configFile := filepath.Join("testdata", t.Name())

// Dump config with cmd-line specific file
exitCode, d := executeWithArgs("--config-file", configFile, "config", "dump")
Expand All @@ -399,19 +365,7 @@ board_manager:
}

func Test3rdPartyCoreIntegration(t *testing.T) {
// override SetUp dirs
tmp := tmpDirOrDie()
defer os.RemoveAll(tmp)
os.Setenv("ARDUINO_SKETCHBOOK_DIR", tmp)
currSketchbookDir = tmp

configFile := filepath.Join(currDataDir, "arduino-cli.yaml")
err := ioutil.WriteFile(configFile, []byte(`
board_manager:
additional_urls:
- https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
`), os.FileMode(0644))
require.NoError(t, err, "writing dummy config "+configFile)
configFile := filepath.Join("testdata", t.Name())

// Update index and install esp32:esp32
exitCode, _ := executeWithArgs("--config-file", configFile, "core", "update-index")
Expand All @@ -422,7 +376,7 @@ board_manager:

// Build a simple sketch and check if all artifacts are copied
tmpSketch := paths.New(currSketchbookDir).Join("Blink")
err = paths.New("testdata/Blink").CopyDirTo(tmpSketch)
err := paths.New("testdata/Blink").CopyDirTo(tmpSketch)
require.NoError(t, err, "copying test sketch into temp dir")
exitCode, d = executeWithArgs("--config-file", configFile, "compile", "-b", "esp32:esp32:esp32", tmpSketch.String())
require.Zero(t, exitCode)
Expand Down Expand Up @@ -543,13 +497,15 @@ func TestSearchConfigTreeNotFound(t *testing.T) {

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, searchConfigTree(tmp), tmp)
}

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)
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/Test3rdPartyCoreIntegration/arduino-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
board_manager:
additional_urls:
- https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
3 changes: 3 additions & 0 deletions cli/testdata/TestInvalidCoreURLIntegration/arduino-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
board_manager:
additional_urls:
- http://www.invalid-domain-asjkdakdhadjkh.com/package_example_index.json
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/cmaglie/pb v1.0.27
github.com/codeclysm/cc v1.2.2 // indirect
github.com/codeclysm/extract v2.2.0+incompatible
github.com/creack/goselect v0.0.0-20180328191401-176c667f75aa // indirect
github.com/fatih/color v1.7.0
github.com/fluxio/iohelpers v0.0.0-20160419043813-3a4dd67a94d2 // indirect
github.com/fluxio/multierror v0.0.0-20160419044231-9c68d39025e5 // indirect
Expand Down