Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Massimiliano Pippi committed Dec 11, 2019
1 parent 2d66c29 commit 7cc8877
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 55 deletions.
9 changes: 7 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,18 @@ 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
configuration.Init(configPath)
configFile := viper.ConfigFileUsed()
fmt.Println("--->", configPath, configFile)

//
// Prepare logging
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

0 comments on commit 7cc8877

Please sign in to comment.