From d99e54c73b76875bc93ddc9651381a1dd23a1bee Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Thu, 21 Sep 2023 16:04:34 +0200 Subject: [PATCH] cli: `core serach` show only latest compatible version --- commands/core/search.go | 21 +++++++++++++------- internal/cli/core/search.go | 8 +++++++- internal/integrationtest/core/core_test.go | 23 +++++++++++++++++++--- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/commands/core/search.go b/commands/core/search.go index b7fc3f92817..e38c06f8088 100644 --- a/commands/core/search.go +++ b/commands/core/search.go @@ -95,17 +95,24 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse out[i].Installed = platformRelease.Version.String() } } + // Sort result alphabetically and put deprecated platforms at the bottom - sort.Slice( - out, func(i, j int) bool { - return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name) - }) - sort.SliceStable( - out, func(i, j int) bool { - if !out[i].Deprecated && out[j].Deprecated { + sort.Slice(out, func(i, j int) bool { + return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name) + }) + if !req.AllVersions { + sort.SliceStable(out, func(i, j int) bool { + if !out[i].Incompatible && out[j].Incompatible { return true } return false }) + } + sort.SliceStable(out, func(i, j int) bool { + if !out[i].Deprecated && out[j].Deprecated { + return true + } + return false + }) return &rpc.PlatformSearchResponse{SearchOutput: out}, nil } diff --git a/internal/cli/core/search.go b/internal/cli/core/search.go index 0259d35bf44..472d490c4c8 100644 --- a/internal/cli/core/search.go +++ b/internal/cli/core/search.go @@ -103,7 +103,13 @@ func (sr searchResults) String() string { if item.Deprecated { name = fmt.Sprintf("[%s] %s", tr("DEPRECATED"), name) } - t.AddRow(item.GetId(), item.GetLatest(), name) + latestVersion := item.GetLatest() + // We only show conpatible version when not seraching for all core + if !allVersions && item.Incompatible { + latestVersion = "" + } + + t.AddRow(item.GetId(), latestVersion, name) } return t.Render() } diff --git a/internal/integrationtest/core/core_test.go b/internal/integrationtest/core/core_test.go index 42c29331d71..32ed98983b9 100644 --- a/internal/integrationtest/core/core_test.go +++ b/internal/integrationtest/core/core_test.go @@ -667,7 +667,7 @@ func TestCoreSearchSortedResults(t *testing.T) { require.NoError(t, err) out := strings.Split(strings.TrimSpace(string(stdout)), "\n") - var lines, deprecated, notDeprecated [][]string + var lines, deprecated, notDeprecated, incompatibles [][]string for i, v := range out { if i > 0 { v = strings.Join(strings.Fields(v), " ") @@ -677,6 +677,10 @@ func TestCoreSearchSortedResults(t *testing.T) { for _, v := range lines { if strings.HasPrefix(v[2], "[DEPRECATED]") { deprecated = append(deprecated, v) + continue + } + if _, err := semver.Parse(v[1]); err != nil { + incompatibles = append(incompatibles, v) } else { notDeprecated = append(notDeprecated, v) } @@ -689,9 +693,13 @@ func TestCoreSearchSortedResults(t *testing.T) { require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool { return strings.ToLower(notDeprecated[i][2]) < strings.ToLower(notDeprecated[j][2]) })) + require.True(t, sort.SliceIsSorted(incompatibles, func(i, j int) bool { + return strings.ToLower(incompatibles[i][2]) < strings.ToLower(incompatibles[j][2]) + })) + result := append(notDeprecated, incompatibles...) // verify that deprecated platforms are the last ones - require.Equal(t, lines, append(notDeprecated, deprecated...)) + require.Equal(t, lines, append(result, deprecated...)) // test same behaviour with json output stdout, _, err = cli.Run("core", "search", "--additional-urls="+url.String(), "--format=json") @@ -705,7 +713,7 @@ func TestCoreSearchSortedResults(t *testing.T) { require.Equal(t, sortedDeprecated, notSortedDeprecated) sortedNotDeprecated := requirejson.Parse(t, stdout).Query( - "[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String() + "[ .[] | select(.deprecated != true) | .name |=ascii_downcase | {name:.name, incompatible:.incompatible} ] | sort_by(.incompatible) | [.[] | .name]").String() notSortedNotDeprecated := requirejson.Parse(t, stdout).Query( "[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String() require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated) @@ -1161,4 +1169,13 @@ func TestCoreHavingIncompatibleDepTools(t *testing.T) { require.NoError(t, err) requirejson.Query(t, stdout, `.[] | select(.id == "foo_vendor:avr") | .latest`, `"1.0.1"`) requirejson.Query(t, stdout, `.[] | select(.id == "incompatible_vendor:avr") | .incompatible`, `true`) + + // In text mode, core search doesn't show any version if no compatible one are present + stdout, _, err = cli.Run("core", "search", additionalURLs) + require.NoError(t, err) + var lines [][]string + for _, v := range strings.Split(strings.TrimSpace(string(stdout)), "\n") { + lines = append(lines, strings.Fields(strings.TrimSpace(v))) + } + require.Contains(t, lines, []string{"incompatible_vendor:avr", "Incompatible", "Boards"}) }