Update kind subsort to sort unknown kinds alphabetically.

Remove length check from subsort test. Add check for unknowns.
pull/2816/head
Justin Scott 7 years ago
parent 87d2b7ab67
commit f239050996

@ -114,18 +114,19 @@ func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifes
func (k *kindSorter) Less(i, j int) bool { func (k *kindSorter) Less(i, j int) bool {
a := k.manifests[i] a := k.manifests[i]
b := k.manifests[j] b := k.manifests[j]
first, ok := k.ordering[a.head.Kind] first, aok := k.ordering[a.head.Kind]
if !ok { second, bok := k.ordering[b.head.Kind]
// Unknown is always last if first == second {
// same kind (including unknown) so sub sort alphanumeric
return a.name < b.name
}
// unknown kind is last
if !aok {
return false return false
} }
second, ok := k.ordering[b.head.Kind] if !bok {
if !ok {
return true return true
} }
if first == second { // sort different kinds
// same kind so sub sort alphanumeric
return a.name < b.name
}
return first < second return first < second
} }

@ -175,19 +175,29 @@ func TestKindSorterSubSort(t *testing.T) {
name: "!", name: "!",
head: &util.SimpleHead{Kind: "ClusterRoleBinding"}, head: &util.SimpleHead{Kind: "ClusterRoleBinding"},
}, },
{
name: "u3",
head: &util.SimpleHead{Kind: "Unknown"},
},
{
name: "u1",
head: &util.SimpleHead{Kind: "Unknown"},
},
{
name: "u2",
head: &util.SimpleHead{Kind: "Unknown"},
},
} }
for _, test := range []struct { for _, test := range []struct {
description string description string
order SortOrder order SortOrder
expected string expected string
}{ }{
{"cm,clusterRole,clusterRoleBinding", InstallOrder, "01Aa!z"}, // expectation is sorted by kind (unknown is last) and then sub sorted alphabetically within each group
{"cm,clusterRole,clusterRoleBinding,Unknown", InstallOrder, "01Aa!zu1u2u3"},
} { } {
var buf bytes.Buffer var buf bytes.Buffer
t.Run(test.description, func(t *testing.T) { t.Run(test.description, func(t *testing.T) {
if got, want := len(test.expected), len(manifests); got != want {
t.Fatalf("Expected %d names in order, got %d", want, got)
}
defer buf.Reset() defer buf.Reset()
for _, r := range sortByKind(manifests, test.order) { for _, r := range sortByKind(manifests, test.order) {
buf.WriteString(r.name) buf.WriteString(r.name)

Loading…
Cancel
Save