Skip to content

Commit

Permalink
cli: core serach show only latest compatible version
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-perugini committed Sep 21, 2023
1 parent e7b9c66 commit cb800ab
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
21 changes: 14 additions & 7 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion internal/cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
23 changes: 20 additions & 3 deletions internal/integrationtest/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), " ")
Expand All @@ -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)
}
Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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"})
}

0 comments on commit cb800ab

Please sign in to comment.