Skip to content

Commit 736608f

Browse files
authored
Fixed panic on lib upgrade command (#1874)
* Fixed NPE on 'lib upgrade' command * Added tests
1 parent 8b52e80 commit 736608f

File tree

8 files changed

+85
-9
lines changed

8 files changed

+85
-9
lines changed

arduino/discovery/discovery_client/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13R
314314
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
315315
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
316316
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
317-
go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
317+
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
318318
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
319319
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
320320
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=

client_example/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13R
296296
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
297297
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
298298
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
299-
go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
299+
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
300300
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
301301
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
302302
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=

commands/lib/upgrade.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,19 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa
4848
func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
4949
lm := commands.GetLibraryManager(req)
5050

51-
// get the libs to upgrade
52-
lib := filterByName(listLibraries(lm, true, true), req.GetName())
51+
// Get the library to upgrade
52+
name := req.GetName()
53+
lib := filterByName(listLibraries(lm, false, false), name)
54+
if lib == nil {
55+
// library not installed...
56+
return &arduino.LibraryNotFoundError{Library: name}
57+
}
58+
if lib.Available == nil {
59+
taskCB(&rpc.TaskProgress{Message: tr("Library %s is already at the latest version", name), Completed: true})
60+
return nil
61+
}
5362

54-
// do it
63+
// Install update
5564
return upgrade(lm, []*installedLib{lib}, downloadCB, taskCB)
5665
}
5766

docsgen/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
372372
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
373373
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
374374
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
375-
go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
375+
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
376376
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
377377
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
378378
go.etcd.io/etcd/client/pkg/v3 v3.5.0/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require (
5858
)
5959

6060
require (
61-
go.bug.st/testifyjson v1.1.0
61+
go.bug.st/testifyjson v1.1.1
6262
go.bug.st/testsuite v0.1.0
6363
)
6464

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
374374
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
375375
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
376376
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
377-
go.bug.st/testifyjson v1.1.0 h1:5EkYXNIA3JkcA4Lu7QluzZRPDzKz91RMMYb5Zz4tOmw=
378-
go.bug.st/testifyjson v1.1.0/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
377+
go.bug.st/testifyjson v1.1.1 h1:nHotIMK151LF3vYsU/b2RaoVaWCgrf2kvQeGNoZkGaA=
378+
go.bug.st/testifyjson v1.1.1/go.mod h1:nZyy2icFbv3OE3zW3mGVOnC/GhWgb93LRu+29n2tJlI=
379379
go.bug.st/testsuite v0.1.0 h1:oX4zdIB62+G5A0Kq4dja7Vy8tDiKqKVhhxkzhpMGgog=
380380
go.bug.st/testsuite v0.1.0/go.mod h1:xCIDf97kf9USoz960Foy3CoquwhQmfuFRNh9git70as=
381381
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=

internal/integrationtest/arduino-cli.go

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func NewArduinoCliWithinEnvironment(env *testsuite.Environment, config *ArduinoC
9898
}
9999

100100
cli.cliEnvVars = []string{
101+
"LANG=en",
101102
fmt.Sprintf("ARDUINO_DATA_DIR=%s", cli.dataDir),
102103
fmt.Sprintf("ARDUINO_DOWNLOADS_DIR=%s", cli.stagingDir),
103104
fmt.Sprintf("ARDUINO_SKETCHBOOK_DIR=%s", cli.sketchbookDir),
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2022 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package lib_test
17+
18+
import (
19+
"testing"
20+
21+
"github.com/arduino/arduino-cli/internal/integrationtest"
22+
"github.com/stretchr/testify/require"
23+
"go.bug.st/testifyjson/requirejson"
24+
)
25+
26+
func TestLibUpgradeCommand(t *testing.T) {
27+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
28+
defer env.CleanUp()
29+
30+
// Updates index for cores and libraries
31+
_, _, err := cli.Run("core", "update-index")
32+
require.NoError(t, err)
33+
_, _, err = cli.Run("lib", "update-index")
34+
require.NoError(t, err)
35+
36+
// Install core (this will help to check interaction with platform bundled libraries)
37+
_, _, err = cli.Run("core", "install", "arduino:avr@1.6.3")
38+
require.NoError(t, err)
39+
40+
// Test upgrade of not-installed library
41+
_, stdErr, err := cli.Run("lib", "upgrade", "Servo")
42+
require.Error(t, err)
43+
require.Contains(t, string(stdErr), "Library 'Servo' not found")
44+
45+
// Test upgrade of installed library
46+
_, _, err = cli.Run("lib", "install", "Servo@1.1.6")
47+
require.NoError(t, err)
48+
stdOut, _, err := cli.Run("lib", "list", "--format", "json")
49+
require.NoError(t, err)
50+
requirejson.Contains(t, stdOut, `[ { "library":{ "name":"Servo", "version": "1.1.6" } } ]`)
51+
52+
_, _, err = cli.Run("lib", "upgrade", "Servo")
53+
require.NoError(t, err)
54+
stdOut, _, err = cli.Run("lib", "list", "--format", "json")
55+
require.NoError(t, err)
56+
jsonOut := requirejson.Parse(t, stdOut)
57+
jsonOut.MustNotContain(`[ { "library":{ "name":"Servo", "version": "1.1.6" } } ]`)
58+
servoVersion := jsonOut.Query(`.[].library | select(.name=="Servo") | .version`).String()
59+
60+
// Upgrade of already up-to-date library
61+
_, _, err = cli.Run("lib", "upgrade", "Servo")
62+
require.NoError(t, err)
63+
stdOut, _, err = cli.Run("lib", "list", "--format", "json")
64+
require.NoError(t, err)
65+
requirejson.Query(t, stdOut, `.[].library | select(.name=="Servo") | .version`, servoVersion)
66+
}

0 commit comments

Comments
 (0)