Skip to content

Commit cb800ab

Browse files
cli: core serach show only latest compatible version
1 parent e7b9c66 commit cb800ab

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

Diff for: commands/core/search.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,24 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
9595
out[i].Installed = platformRelease.Version.String()
9696
}
9797
}
98+
9899
// Sort result alphabetically and put deprecated platforms at the bottom
99-
sort.Slice(
100-
out, func(i, j int) bool {
101-
return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
102-
})
103-
sort.SliceStable(
104-
out, func(i, j int) bool {
105-
if !out[i].Deprecated && out[j].Deprecated {
100+
sort.Slice(out, func(i, j int) bool {
101+
return strings.ToLower(out[i].Name) < strings.ToLower(out[j].Name)
102+
})
103+
if !req.AllVersions {
104+
sort.SliceStable(out, func(i, j int) bool {
105+
if !out[i].Incompatible && out[j].Incompatible {
106106
return true
107107
}
108108
return false
109109
})
110+
}
111+
sort.SliceStable(out, func(i, j int) bool {
112+
if !out[i].Deprecated && out[j].Deprecated {
113+
return true
114+
}
115+
return false
116+
})
110117
return &rpc.PlatformSearchResponse{SearchOutput: out}, nil
111118
}

Diff for: internal/cli/core/search.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,13 @@ func (sr searchResults) String() string {
103103
if item.Deprecated {
104104
name = fmt.Sprintf("[%s] %s", tr("DEPRECATED"), name)
105105
}
106-
t.AddRow(item.GetId(), item.GetLatest(), name)
106+
latestVersion := item.GetLatest()
107+
// We only show conpatible version when not seraching for all core
108+
if !allVersions && item.Incompatible {
109+
latestVersion = ""
110+
}
111+
112+
t.AddRow(item.GetId(), latestVersion, name)
107113
}
108114
return t.Render()
109115
}

Diff for: internal/integrationtest/core/core_test.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ func TestCoreSearchSortedResults(t *testing.T) {
667667
require.NoError(t, err)
668668

669669
out := strings.Split(strings.TrimSpace(string(stdout)), "\n")
670-
var lines, deprecated, notDeprecated [][]string
670+
var lines, deprecated, notDeprecated, incompatibles [][]string
671671
for i, v := range out {
672672
if i > 0 {
673673
v = strings.Join(strings.Fields(v), " ")
@@ -677,6 +677,10 @@ func TestCoreSearchSortedResults(t *testing.T) {
677677
for _, v := range lines {
678678
if strings.HasPrefix(v[2], "[DEPRECATED]") {
679679
deprecated = append(deprecated, v)
680+
continue
681+
}
682+
if _, err := semver.Parse(v[1]); err != nil {
683+
incompatibles = append(incompatibles, v)
680684
} else {
681685
notDeprecated = append(notDeprecated, v)
682686
}
@@ -689,9 +693,13 @@ func TestCoreSearchSortedResults(t *testing.T) {
689693
require.True(t, sort.SliceIsSorted(notDeprecated, func(i, j int) bool {
690694
return strings.ToLower(notDeprecated[i][2]) < strings.ToLower(notDeprecated[j][2])
691695
}))
696+
require.True(t, sort.SliceIsSorted(incompatibles, func(i, j int) bool {
697+
return strings.ToLower(incompatibles[i][2]) < strings.ToLower(incompatibles[j][2])
698+
}))
692699

700+
result := append(notDeprecated, incompatibles...)
693701
// verify that deprecated platforms are the last ones
694-
require.Equal(t, lines, append(notDeprecated, deprecated...))
702+
require.Equal(t, lines, append(result, deprecated...))
695703

696704
// test same behaviour with json output
697705
stdout, _, err = cli.Run("core", "search", "--additional-urls="+url.String(), "--format=json")
@@ -705,7 +713,7 @@ func TestCoreSearchSortedResults(t *testing.T) {
705713
require.Equal(t, sortedDeprecated, notSortedDeprecated)
706714

707715
sortedNotDeprecated := requirejson.Parse(t, stdout).Query(
708-
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | .name ] | sort").String()
716+
"[ .[] | select(.deprecated != true) | .name |=ascii_downcase | {name:.name, incompatible:.incompatible} ] | sort_by(.incompatible) | [.[] | .name]").String()
709717
notSortedNotDeprecated := requirejson.Parse(t, stdout).Query(
710718
"[.[] | select(.deprecated != true) | .name |=ascii_downcase | .name]").String()
711719
require.Equal(t, sortedNotDeprecated, notSortedNotDeprecated)
@@ -1161,4 +1169,13 @@ func TestCoreHavingIncompatibleDepTools(t *testing.T) {
11611169
require.NoError(t, err)
11621170
requirejson.Query(t, stdout, `.[] | select(.id == "foo_vendor:avr") | .latest`, `"1.0.1"`)
11631171
requirejson.Query(t, stdout, `.[] | select(.id == "incompatible_vendor:avr") | .incompatible`, `true`)
1172+
1173+
// In text mode, core search doesn't show any version if no compatible one are present
1174+
stdout, _, err = cli.Run("core", "search", additionalURLs)
1175+
require.NoError(t, err)
1176+
var lines [][]string
1177+
for _, v := range strings.Split(strings.TrimSpace(string(stdout)), "\n") {
1178+
lines = append(lines, strings.Fields(strings.TrimSpace(v)))
1179+
}
1180+
require.Contains(t, lines, []string{"incompatible_vendor:avr", "Incompatible", "Boards"})
11641181
}

0 commit comments

Comments
 (0)