Commit fa96057b authored by Matt Butcher's avatar Matt Butcher Committed by GitHub

Merge pull request #1900 from technosophos/fix/1897-search-sort-order

fix(helm): fix sort order on helm search
parents c1e66832 f018364f
...@@ -62,6 +62,7 @@ const verSep = "$$" ...@@ -62,6 +62,7 @@ const verSep = "$$"
// AddRepo adds a repository index to the search index. // AddRepo adds a repository index to the search index.
func (i *Index) AddRepo(rname string, ind *repo.IndexFile, all bool) { func (i *Index) AddRepo(rname string, ind *repo.IndexFile, all bool) {
ind.SortEntries()
for name, ref := range ind.Entries { for name, ref := range ind.Entries {
if len(ref) == 0 { if len(ref) == 0 {
// Skip chart names that have zero releases. // Skip chart names that have zero releases.
...@@ -175,7 +176,7 @@ func (i *Index) SearchRegexp(re string, threshold int) ([]*Result, error) { ...@@ -175,7 +176,7 @@ func (i *Index) SearchRegexp(re string, threshold int) ([]*Result, error) {
return buf, nil return buf, nil
} }
// Chart returns the ChartRef for a particular name. // Chart returns the ChartVersion for a particular name.
func (i *Index) Chart(name string) (*repo.ChartVersion, error) { func (i *Index) Chart(name string) (*repo.ChartVersion, error) {
c, ok := i.charts[name] c, ok := i.charts[name]
if !ok { if !ok {
...@@ -220,6 +221,8 @@ func (s scoreSorter) Less(a, b int) bool { ...@@ -220,6 +221,8 @@ func (s scoreSorter) Less(a, b int) bool {
if err != nil { if err != nil {
return true return true
} }
// Sort so that the newest chart is higher than the oldest chart. This is
// the opposite of what you'd expect in a function called Less.
return v1.GreaterThan(v2) return v1.GreaterThan(v2)
} }
return first.Name < second.Name return first.Name < second.Name
......
...@@ -26,14 +26,16 @@ import ( ...@@ -26,14 +26,16 @@ import (
func TestSortScore(t *testing.T) { func TestSortScore(t *testing.T) {
in := []*Result{ in := []*Result{
{Name: "bbb", Score: 0}, {Name: "bbb", Score: 0, Chart: &repo.ChartVersion{Metadata: &chart.Metadata{Version: "1.2.3"}}},
{Name: "aaa", Score: 5}, {Name: "aaa", Score: 5},
{Name: "abb", Score: 5}, {Name: "abb", Score: 5},
{Name: "aab", Score: 0}, {Name: "aab", Score: 0},
{Name: "bab", Score: 5}, {Name: "bab", Score: 5},
{Name: "ver", Score: 5, Chart: &repo.ChartVersion{Metadata: &chart.Metadata{Version: "1.2.4"}}},
{Name: "ver", Score: 5, Chart: &repo.ChartVersion{Metadata: &chart.Metadata{Version: "1.2.3"}}},
} }
expect := []string{"aab", "bbb", "aaa", "abb", "bab"} expect := []string{"aab", "bbb", "aaa", "abb", "bab", "ver", "ver"}
expectScore := []int{0, 0, 5, 5, 5} expectScore := []int{0, 0, 5, 5, 5, 5, 5}
SortScore(in) SortScore(in)
// Test Score // Test Score
...@@ -48,6 +50,14 @@ func TestSortScore(t *testing.T) { ...@@ -48,6 +50,14 @@ func TestSortScore(t *testing.T) {
t.Errorf("Sort error: expected %s, got %s", expect[i], in[i].Name) t.Errorf("Sort error: expected %s, got %s", expect[i], in[i].Name)
} }
} }
// Test version of last two items
if in[5].Chart.Version != "1.2.4" {
t.Errorf("Expected 1.2.4, got %s", in[5].Chart.Version)
}
if in[6].Chart.Version != "1.2.3" {
t.Error("Expected 1.2.3 to be last")
}
} }
var indexfileEntries = map[string]repo.ChartVersions{ var indexfileEntries = map[string]repo.ChartVersions{
...@@ -123,6 +133,20 @@ func TestAll(t *testing.T) { ...@@ -123,6 +133,20 @@ func TestAll(t *testing.T) {
} }
} }
func TestAddRepo_Sort(t *testing.T) {
i := loadTestIndex(t, true)
sr, err := i.Search("testing/santa-maria", 100, false)
if err != nil {
t.Fatal(err)
}
ch := sr[0]
expect := "1.2.3"
if ch.Chart.Version != expect {
t.Errorf("Expected %q, got %q", expect, ch.Chart.Version)
}
}
func TestSearchByName(t *testing.T) { func TestSearchByName(t *testing.T) {
tests := []struct { tests := []struct {
......
...@@ -39,7 +39,7 @@ func TestSearchCmd(t *testing.T) { ...@@ -39,7 +39,7 @@ func TestSearchCmd(t *testing.T) {
{ {
name: "search for 'alpine', expect two matches", name: "search for 'alpine', expect two matches",
args: []string{"alpine"}, args: []string{"alpine"},
expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \tDeploy a basic Alpine Linux pod", expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \tDeploy a basic Alpine Linux pod",
}, },
{ {
name: "search for 'alpine' with versions, expect three matches", name: "search for 'alpine' with versions, expect three matches",
...@@ -56,7 +56,7 @@ func TestSearchCmd(t *testing.T) { ...@@ -56,7 +56,7 @@ func TestSearchCmd(t *testing.T) {
name: "search for 'alp[a-z]+', expect two matches", name: "search for 'alp[a-z]+', expect two matches",
args: []string{"alp[a-z]+"}, args: []string{"alp[a-z]+"},
flags: []string{"--regexp"}, flags: []string{"--regexp"},
expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.1.0 \tDeploy a basic Alpine Linux pod", expect: "NAME \tVERSION\tDESCRIPTION \ntesting/alpine\t0.2.0 \tDeploy a basic Alpine Linux pod",
regexp: true, regexp: true,
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment