fixes #4728 problem that arise because of the zero value of unknown and the loging of kind_sorter

this messes helm template when having unknown (ie NOTES) or other files and the sorter catches namespace

Signed-off-by: Filinto Duran <duranto@gmail.com>
pull/5062/head
Filinto Duran 7 years ago
parent c7009a155c
commit c9a5b0bb49

@ -124,12 +124,13 @@ func (k *kindSorter) Less(i, j int) bool {
b := k.manifests[j]
first, aok := k.ordering[a.Head.Kind]
second, bok := k.ordering[b.Head.Kind]
// if same kind (including unknown) sub sort alphanumeric
if first == second {
// if both are unknown and of different kind sort by kind alphabetically
if !aok && !bok && a.Head.Kind != b.Head.Kind {
// if both are unknown
if !aok && !bok {
// and of different kind sort by kind alphabetically
if a.Head.Kind != b.Head.Kind {
return a.Head.Kind < b.Head.Kind
}
// and of same kind sort by name alphabetically
return a.Name < b.Name
}
// unknown kind is last
@ -139,6 +140,10 @@ func (k *kindSorter) Less(i, j int) bool {
if !bok {
return true
}
// if same kind sort alphanumeric
if first == second {
return a.Name < b.Name
}
// sort different kinds
return first < second
}

@ -223,3 +223,39 @@ func TestKindSorterSubSort(t *testing.T) {
})
}
}
func TestKindSorterUnknownsWithNamespace(t *testing.T) {
// a.Kind=Namespace should win over b.Kind=Unknown even if the a.Name > b.Name
manifests := []Manifest{
{
Name: "a",
Head: &util.SimpleHead{Kind: "Namespace"},
},
{
Name: "N",
Head: &util.SimpleHead{Kind: "Unknown"},
},
}
for _, test := range []struct {
description string
order SortOrder
expected string
}{
// expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
{"Namespace,Unknown", InstallOrder, "aN"},
} {
var buf bytes.Buffer
t.Run(test.description, func(t *testing.T) {
defer buf.Reset()
m := make([]Manifest, len(manifests))
copy(m, manifests)
for _, r := range sortByKind(m, test.order) {
buf.WriteString(r.Name)
}
if got := buf.String(); got != test.expected {
t.Errorf("Expected %q, got %q", test.expected, got)
}
})
}
}

Loading…
Cancel
Save