diff --git a/commands/lib/upgrade.go b/commands/lib/upgrade.go index a156553ee6a..b02ed8d5733 100644 --- a/commands/lib/upgrade.go +++ b/commands/lib/upgrade.go @@ -17,11 +17,8 @@ package lib import ( "context" - "errors" "github.com/arduino/arduino-cli/arduino" - "github.com/arduino/arduino-cli/arduino/libraries" - "github.com/arduino/arduino-cli/arduino/libraries/librariesmanager" "github.com/arduino/arduino-cli/commands" rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" ) @@ -33,7 +30,7 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa return &arduino.InvalidInstanceError{} } - if err := upgrade(lm, listLibraries(lm, true, false), downloadCB, taskCB); err != nil { + if err := upgrade(req.Instance, listLibraries(lm, true, false), downloadCB, taskCB); err != nil { return err } @@ -47,6 +44,9 @@ func LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequest, downloadCB rpc.Downloa // LibraryUpgrade upgrades a library func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { lm := commands.GetLibraryManager(req) + if lm == nil { + return &arduino.InvalidInstanceError{} + } // Get the library to upgrade name := req.GetName() @@ -61,23 +61,21 @@ func LibraryUpgrade(ctx context.Context, req *rpc.LibraryUpgradeRequest, downloa } // Install update - return upgrade(lm, []*installedLib{lib}, downloadCB, taskCB) + return upgrade(req.Instance, []*installedLib{lib}, downloadCB, taskCB) } -func upgrade(lm *librariesmanager.LibrariesManager, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { - // Go through the list and download them +func upgrade(instance *rpc.Instance, libs []*installedLib, downloadCB rpc.DownloadProgressCB, taskCB rpc.TaskProgressCB) error { for _, lib := range libs { - if err := downloadLibrary(lm, lib.Available, downloadCB, taskCB); err != nil { - return err + libInstallReq := &rpc.LibraryInstallRequest{ + Instance: instance, + Name: lib.Library.Name, + Version: "", + NoDeps: false, + NoOverwrite: false, } - } - - // Go through the list and install them - for _, lib := range libs { - if err := installLibrary(lm, lib.Available, libraries.User, taskCB); err != nil { - if !errors.Is(err, librariesmanager.ErrAlreadyInstalled) { - return err - } + err := LibraryInstall(context.Background(), libInstallReq, downloadCB, taskCB) + if err != nil { + return err } } diff --git a/internal/integrationtest/lib/lib_test.go b/internal/integrationtest/lib/lib_test.go index c409a43f855..679d0fd499b 100644 --- a/internal/integrationtest/lib/lib_test.go +++ b/internal/integrationtest/lib/lib_test.go @@ -199,3 +199,48 @@ func TestLibDepsOutput(t *testing.T) { require.Equal(t, "WiFiNINA", jsonDeps.Dependencies[6].Name) require.Equal(t, jsonDeps.Dependencies[6].VersionInstalled, jsonDeps.Dependencies[6].VersionRequired) } + +func TestUpgradeLibraryWithDependencies(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 library + _, _, err = cli.Run("lib", "install", "Arduino_ConnectionHandler@0.3.3") + require.NoError(t, err) + stdOut, _, err := cli.Run("lib", "deps", "Arduino_ConnectionHandler@0.3.3", "--format", "json") + require.NoError(t, err) + + var jsonDeps struct { + Dependencies []struct { + Name string `json:"name"` + VersionRequired string `json:"version_required"` + VersionInstalled string `json:"version_installed"` + } `json:"dependencies"` + } + err = json.Unmarshal(stdOut, &jsonDeps) + require.NoError(t, err) + + require.Len(t, jsonDeps.Dependencies, 6) + require.Equal(t, "Arduino_ConnectionHandler", jsonDeps.Dependencies[0].Name) + require.Equal(t, "Arduino_DebugUtils", jsonDeps.Dependencies[1].Name) + require.Equal(t, "MKRGSM", jsonDeps.Dependencies[2].Name) + require.Equal(t, "MKRNB", jsonDeps.Dependencies[3].Name) + require.Equal(t, "WiFi101", jsonDeps.Dependencies[4].Name) + require.Equal(t, "WiFiNINA", jsonDeps.Dependencies[5].Name) + + // Test lib upgrade also install new dependencies of already installed library + _, _, err = cli.Run("lib", "upgrade", "Arduino_ConnectionHandler") + require.NoError(t, err) + stdOut, _, err = cli.Run("lib", "deps", "Arduino_ConnectionHandler", "--format", "json") + require.NoError(t, err) + + jsonOut := requirejson.Parse(t, stdOut) + dependency := jsonOut.Query(`.dependencies[] | select(.name=="MKRWAN")`) + require.Equal(t, dependency.Query(".version_required"), dependency.Query(".version_installed")) +}