Skip to content

Fix lib upgrade command not installing new libraries dependencies #1935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions commands/lib/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
}

Expand All @@ -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()
Expand All @@ -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
}
}

Expand Down
45 changes: 45 additions & 0 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
require.NoError(t, err)
stdOut, _, err := cli.Run("lib", "deps", "[email protected]", "--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"))
}