From a73f7a38afe56f9b9c952a62a54240b51526d365 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 9 Sep 2022 12:16:29 +0200 Subject: [PATCH 1/2] Fixed NPE on 'lib upgrade' command --- commands/lib/upgrade.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/commands/lib/upgrade.go b/commands/lib/upgrade.go index b2b676fcb2f..6a8b810e2a1 100644 --- a/commands/lib/upgrade.go +++ b/commands/lib/upgrade.go @@ -48,10 +48,19 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { lm := commands.GetLibraryManager(req) - // get the libs to upgrade - lib := filterByName(listLibraries(lm, true, true), req.GetName()) + // Get the library to upgrade + name := req.GetName() + lib := filterByName(listLibraries(lm, false, false), name) + if lib == nil { + // library not installed... + return &arduino.LibraryNotFoundError{Library: name} + } + if lib.Available == nil { + taskCB(&rpc.TaskProgress{Message: tr("Library %s is already at the latest version", name), Completed: true}) + return nil + } - // do it + // Install update return upgrade(lm, []*installedLib{lib}, downloadCB, taskCB) } From 73957685a8443bf25a2bea8afd3b127f500393b5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 9 Sep 2022 17:01:24 +0200 Subject: [PATCH 2/2] Added tests --- arduino/discovery/discovery_client/go.sum | 2 +- client_example/go.sum | 2 +- docsgen/go.sum | 2 +- go.mod | 2 +- go.sum | 4 +- internal/integrationtest/arduino-cli.go | 1 + internal/integrationtest/lib/lib_test.go | 66 +++++++++++++++++++++++ 7 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 internal/integrationtest/lib/lib_test.go diff --git a/arduino/discovery/discovery_client/go.sum b/arduino/discovery/discovery_client/go.sum index a7289e54e83..09c4fe481bc 100644 --- a/arduino/discovery/discovery_client/go.sum +++ b/arduino/discovery/discovery_client/go.sum @@ -314,7 +314,7 @@ go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13R go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q= go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw= -go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= +go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= diff --git a/client_example/go.sum b/client_example/go.sum index cbc1be3a617..4db2fe0ef52 100644 --- a/client_example/go.sum +++ b/client_example/go.sum @@ -296,7 +296,7 @@ go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13R go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q= go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw= -go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= +go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= diff --git a/docsgen/go.sum b/docsgen/go.sum index 966421249ff..a6a5f8dac52 100644 --- a/docsgen/go.sum +++ b/docsgen/go.sum @@ -372,7 +372,7 @@ go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk= go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw= -go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= +go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= diff --git a/go.mod b/go.mod index c5ebac2b5be..18cbec0bb80 100644 --- a/go.mod +++ b/go.mod @@ -58,7 +58,7 @@ require ( ) require ( - go.bug.st/testifyjson v1.1.0 + go.bug.st/testifyjson v1.1.1 go.bug.st/testsuite v0.1.0 ) diff --git a/go.sum b/go.sum index aa2f4c56d40..c048463cd5a 100644 --- a/go.sum +++ b/go.sum @@ -374,8 +374,8 @@ go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk= go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI= go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw= -go.bug.st/testifyjson v1.1.0 h1:5EkYXNIA3JkcA4Lu7QluzZRPDzKz91RMMYb5Zz4tOmw= -go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= +go.bug.st/testifyjson v1.1.1 h1:nHotIMK151LF3vYsU/b2RaoVaWCgrf2kvQeGNoZkGaA= +go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI= go.bug.st/testsuite v0.1.0 h1:oX4zdIB62+G5A0Kq4dja7Vy8tDiKqKVhhxkzhpMGgog= go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as= go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs= diff --git a/internal/integrationtest/arduino-cli.go b/internal/integrationtest/arduino-cli.go index b7e7b6ffeed..c67d5d675c2 100644 --- a/internal/integrationtest/arduino-cli.go +++ b/internal/integrationtest/arduino-cli.go @@ -98,6 +98,7 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC } cli.cliEnvVars = []string{ + "LANG=en", fmt.Sprintf("ARDUINO_DATA_DIR=%s", cli.dataDir), fmt.Sprintf("ARDUINO_DOWNLOADS_DIR=%s", cli.stagingDir), fmt.Sprintf("ARDUINO_SKETCHBOOK_DIR=%s", cli.sketchbookDir), diff --git a/internal/integrationtest/lib/lib_test.go b/internal/integrationtest/lib/lib_test.go new file mode 100644 index 00000000000..08afa128841 --- /dev/null +++ b/internal/integrationtest/lib/lib_test.go @@ -0,0 +1,66 @@ +// This file is part of arduino-cli. +// +// Copyright 2022 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + +package lib_test + +import ( + "testing" + + "github.com/arduino/arduino-cli/internal/integrationtest" + "github.com/stretchr/testify/require" + "go.bug.st/testifyjson/requirejson" +) + +func TestLibUpgradeCommand(t *testing.T) { + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) + defer env.CleanUp() + + // Updates index for cores and libraries + _, _, err := cli.Run("core", "update-index") + require.NoError(t, err) + _, _, err = cli.Run("lib", "update-index") + require.NoError(t, err) + + // Install core (this will help to check interaction with platform bundled libraries) + _, _, err = cli.Run("core", "install", "arduino:avr@1.6.3") + require.NoError(t, err) + + // Test upgrade of not-installed library + _, stdErr, err := cli.Run("lib", "upgrade", "Servo") + require.Error(t, err) + require.Contains(t, string(stdErr), "Library 'Servo' not found") + + // Test upgrade of installed library + _, _, err = cli.Run("lib", "install", "Servo@1.1.6") + require.NoError(t, err) + stdOut, _, err := cli.Run("lib", "list", "--format", "json") + require.NoError(t, err) + requirejson.Contains(t, stdOut, `[ { "library":{ "name":"Servo", "version": "1.1.6" } } ]`) + + _, _, err = cli.Run("lib", "upgrade", "Servo") + require.NoError(t, err) + stdOut, _, err = cli.Run("lib", "list", "--format", "json") + require.NoError(t, err) + jsonOut := requirejson.Parse(t, stdOut) + jsonOut.MustNotContain(`[ { "library":{ "name":"Servo", "version": "1.1.6" } } ]`) + servoVersion := jsonOut.Query(`.[].library | select(.name=="Servo") | .version`).String() + + // Upgrade of already up-to-date library + _, _, err = cli.Run("lib", "upgrade", "Servo") + require.NoError(t, err) + stdOut, _, err = cli.Run("lib", "list", "--format", "json") + require.NoError(t, err) + requirejson.Query(t, stdOut, `.[].library | select(.name=="Servo") | .version`, servoVersion) +}