Skip to content

Commit e58a4d6

Browse files
committed
Fix lib upgrade command not installing new libraries dependencies
1 parent 960c210 commit e58a4d6

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

Diff for: commands/lib/upgrade.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ package lib
1717

1818
import (
1919
"context"
20-
"errors"
2120

2221
"github.com/arduino/arduino-cli/arduino"
23-
"github.com/arduino/arduino-cli/arduino/libraries"
24-
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2522
"github.com/arduino/arduino-cli/commands"
2623
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2724
)
@@ -33,7 +30,7 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa
3330
return &arduino.InvalidInstanceError{}
3431
}
3532

36-
if err := upgrade(lm, listLibraries(lm, true, false), downloadCB, taskCB); err != nil {
33+
if err := upgrade(req.Instance, listLibraries(lm, true, false), downloadCB, taskCB); err != nil {
3734
return err
3835
}
3936

@@ -47,6 +44,9 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa
4744
// LibraryUpgrade upgrades a library
4845
func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
4946
lm := commands.GetLibraryManager(req)
47+
if lm == nil {
48+
return &arduino.InvalidInstanceError{}
49+
}
5050

5151
// Get the library to upgrade
5252
name := req.GetName()
@@ -61,23 +61,21 @@ func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloa
6161
}
6262

6363
// Install update
64-
return upgrade(lm, []*installedLib{lib}, downloadCB, taskCB)
64+
return upgrade(req.Instance, []*installedLib{lib}, downloadCB, taskCB)
6565
}
6666

67-
func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
68-
// Go through the list and download them
67+
func upgrade(instance *rpc.Instance, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error {
6968
for _, lib := range libs {
70-
if err := downloadLibrary(lm, lib.Available, downloadCB, taskCB); err != nil {
71-
return err
69+
libInstallReq := &rpc.LibraryInstallRequest{
70+
Instance: instance,
71+
Name: lib.Library.Name,
72+
Version: "",
73+
NoDeps: false,
74+
NoOverwrite: false,
7275
}
73-
}
74-
75-
// Go through the list and install them
76-
for _, lib := range libs {
77-
if err := installLibrary(lm, lib.Available, libraries.User, taskCB); err != nil {
78-
if !errors.Is(err, librariesmanager.ErrAlreadyInstalled) {
79-
return err
80-
}
76+
err := LibraryInstall(context.Background(), libInstallReq, downloadCB, taskCB)
77+
if err != nil {
78+
return err
8179
}
8280
}
8381

Diff for: internal/integrationtest/lib/lib_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,48 @@ func TestLibDepsOutput(t *testing.T) {
199199
require.Equal(t, "WiFiNINA", jsonDeps.Dependencies[6].Name)
200200
require.Equal(t, jsonDeps.Dependencies[6].VersionInstalled, jsonDeps.Dependencies[6].VersionRequired)
201201
}
202+
203+
func TestUpgradeLibraryWithDependencies(t *testing.T) {
204+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
205+
defer env.CleanUp()
206+
207+
// Updates index for cores and libraries
208+
_, _, err := cli.Run("core", "update-index")
209+
require.NoError(t, err)
210+
_, _, err = cli.Run("lib", "update-index")
211+
require.NoError(t, err)
212+
213+
// Install library
214+
_, _, err = cli.Run("lib", "install", "[email protected]")
215+
require.NoError(t, err)
216+
stdOut, _, err := cli.Run("lib", "deps", "[email protected]", "--format", "json")
217+
require.NoError(t, err)
218+
219+
var jsonDeps struct {
220+
Dependencies []struct {
221+
Name string `json:"name"`
222+
VersionRequired string `json:"version_required"`
223+
VersionInstalled string `json:"version_installed"`
224+
} `json:"dependencies"`
225+
}
226+
err = json.Unmarshal(stdOut, &jsonDeps)
227+
require.NoError(t, err)
228+
229+
require.Len(t, jsonDeps.Dependencies, 6)
230+
require.Equal(t, "Arduino_ConnectionHandler", jsonDeps.Dependencies[0].Name)
231+
require.Equal(t, "Arduino_DebugUtils", jsonDeps.Dependencies[1].Name)
232+
require.Equal(t, "MKRGSM", jsonDeps.Dependencies[2].Name)
233+
require.Equal(t, "MKRNB", jsonDeps.Dependencies[3].Name)
234+
require.Equal(t, "WiFi101", jsonDeps.Dependencies[4].Name)
235+
require.Equal(t, "WiFiNINA", jsonDeps.Dependencies[5].Name)
236+
237+
// Test lib upgrade also install new dependencies of already installed library
238+
_, _, err = cli.Run("lib", "upgrade", "Arduino_ConnectionHandler")
239+
require.NoError(t, err)
240+
stdOut, _, err = cli.Run("lib", "deps", "Arduino_ConnectionHandler", "--format", "json")
241+
require.NoError(t, err)
242+
243+
jsonOut := requirejson.Parse(t, stdOut)
244+
dependency := jsonOut.Query(`.dependencies[] | select(.name=="MKRWAN")`)
245+
require.Equal(t, dependency.Query(".version_required"), dependency.Query(".version_installed"))
246+
}

0 commit comments

Comments
 (0)