From 7cc8877189380e0f67e69a2a0b7880520781d5e5 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Wed, 11 Dec 2019 15:38:09 +0100 Subject: [PATCH] fix integration tests --- cli/cli.go | 9 ++- cli/cli_test.go | 60 +++---------------- .../arduino-cli.yaml | 3 + .../arduino-cli.yaml | 3 + go.mod | 1 - 5 files changed, 21 insertions(+), 55 deletions(-) create mode 100644 cli/testdata/Test3rdPartyCoreIntegration/arduino-cli.yaml create mode 100644 cli/testdata/TestInvalidCoreURLIntegration/arduino-cli.yaml diff --git a/cli/cli.go b/cli/cli.go index 9b5962776c8..ecff7636c67 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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 diff --git a/cli/cli_test.go b/cli/cli_test.go index 9e8aa36e61f..0aa4eeb0662 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -28,6 +28,8 @@ import ( "runtime" "testing" + "github.com/spf13/viper" + "github.com/arduino/arduino-cli/cli/feedback" "bou.ke/monkey" @@ -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 @@ -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") @@ -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") @@ -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) @@ -543,6 +497,7 @@ 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) @@ -550,6 +505,7 @@ func TestSearchConfigTreeSameFolder(t *testing.T) { 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) diff --git a/cli/testdata/Test3rdPartyCoreIntegration/arduino-cli.yaml b/cli/testdata/Test3rdPartyCoreIntegration/arduino-cli.yaml new file mode 100644 index 00000000000..16c07d44a1e --- /dev/null +++ b/cli/testdata/Test3rdPartyCoreIntegration/arduino-cli.yaml @@ -0,0 +1,3 @@ +board_manager: + additional_urls: + - https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json \ No newline at end of file diff --git a/cli/testdata/TestInvalidCoreURLIntegration/arduino-cli.yaml b/cli/testdata/TestInvalidCoreURLIntegration/arduino-cli.yaml new file mode 100644 index 00000000000..1fbaa8b1c90 --- /dev/null +++ b/cli/testdata/TestInvalidCoreURLIntegration/arduino-cli.yaml @@ -0,0 +1,3 @@ +board_manager: + additional_urls: + - http://www.invalid-domain-asjkdakdhadjkh.com/package_example_index.json \ No newline at end of file diff --git a/go.mod b/go.mod index 5afaf2fb653..6625a7a6427 100644 --- a/go.mod +++ b/go.mod @@ -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