Skip to content

Change lib deps CLI command output to sorted alphabetically #1934

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 4 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion cli/lib/check_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"os"
"sort"

"github.com/arduino/arduino-cli/cli/arguments"
"github.com/arduino/arduino-cli/cli/errorcodes"
Expand Down Expand Up @@ -81,13 +82,27 @@ func (dr checkDepResult) Data() interface{} {

func (dr checkDepResult) String() string {
res := ""
for _, dep := range dr.deps.GetDependencies() {
deps := dr.deps.Dependencies

// Sort depedencies alphabetically and then puts installed ones on top
sort.Slice(deps, func(i, j int) bool {
return deps[i].Name < deps[j].Name
})
sort.SliceStable(deps, func(i, j int) bool {
if deps[i].VersionInstalled != "" && deps[j].VersionInstalled == "" {
return true
}
return false
})

for _, dep := range deps {
res += outputDep(dep)
}
return res
}

func outputDep(dep *rpc.LibraryDependencyStatus) string {

res := ""
green := color.New(color.FgGreen)
red := color.New(color.FgRed)
Expand Down
4 changes: 4 additions & 0 deletions commands/lib/resolve_deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package lib
import (
"context"
"errors"
"sort"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/arduino/libraries"
Expand Down Expand Up @@ -74,5 +75,8 @@ func LibraryResolveDependencies(ctx context.Context, req *rpc.LibraryResolveDepe
VersionInstalled: installed,
})
}
sort.Slice(res, func(i, j int) bool {
return res[i].Name < res[j].Name
})
return &rpc.LibraryResolveDependenciesResponse{Dependencies: res}, nil
}
46 changes: 46 additions & 0 deletions internal/integrationtest/lib/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package lib_test

import (
"strings"
"testing"

"github.com/arduino/arduino-cli/internal/integrationtest"
Expand Down Expand Up @@ -136,3 +137,48 @@ func TestDuplicateLibInstallDetection(t *testing.T) {
require.Error(t, err)
require.Contains(t, string(stdErr), "The library ArduinoOTA has multiple installations")
}

func TestLibDepsOutput(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 some libraries that are dependencies of another library
_, _, err = cli.Run("lib", "install", "[email protected]")
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "[email protected]")
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "[email protected]")
require.NoError(t, err)
_, _, err = cli.Run("lib", "install", "[email protected]")
require.NoError(t, err)

stdOut, _, err := cli.Run("lib", "deps", "[email protected]", "--no-color")
require.NoError(t, err)
lines := strings.Split(strings.TrimSpace(string(stdOut)), "\n")
require.Len(t, lines, 7)
require.Equal(t, "✓ Arduino_DebugUtils 1.3.0 is already installed.", lines[0])
require.Equal(t, "✓ MKRGSM 1.5.0 is already installed.", lines[1])
require.Equal(t, "✓ MKRNB 1.5.1 is already installed.", lines[2])
require.Equal(t, "✓ WiFiNINA 1.8.13 is already installed.", lines[3])
require.Equal(t, "✕ Arduino_ConnectionHandler 0.6.6 must be installed.", lines[4])
require.Equal(t, "✕ MKRWAN 1.1.0 must be installed.", lines[5])
require.Equal(t, "✕ WiFi101 0.16.1 must be installed.", lines[6])

stdOut, _, err = cli.Run("lib", "deps", "[email protected]", "--format", "json")
require.NoError(t, err)
expectedOutput := `{"dependencies":[
{"name":"Arduino_ConnectionHandler","version_required":"0.6.6"},
{"name":"Arduino_DebugUtils","version_required":"1.3.0","version_installed":"1.3.0"},
{"name":"MKRGSM","version_required":"1.5.0","version_installed":"1.5.0"},
{"name":"MKRNB","version_required":"1.5.1","version_installed":"1.5.1"},
{"name":"MKRWAN","version_required":"1.1.0"},
{"name":"WiFi101","version_required":"0.16.1"},
{"name":"WiFiNINA","version_required":"1.8.13","version_installed":"1.8.13"}]}`
require.JSONEq(t, expectedOutput, string(stdOut))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as in the other PR, this test will fail as soon as one of the libraries gets another release.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, there's no way of setting the version of libraries' dependencies, didn't think about that.
I'll find a reliable way to test it.

}