mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Merge pull request #2030 from pwittrock/master
Fix `kio` sorting for files with more than 9 Resources
This commit is contained in:
@@ -136,7 +136,7 @@ func SortNodes(nodes []*yaml.RNode) error {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if iIndex != jIndex {
|
if iIndex != jIndex {
|
||||||
return iValue < jValue
|
return iIndex < jIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// elements are equal
|
// elements are equal
|
||||||
|
|||||||
77
kyaml/kio/kioutil/kioutil_test.go
Normal file
77
kyaml/kio/kioutil/kioutil_test.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
// Copyright 2019 The Kubernetes Authors.
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package kioutil_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"math/rand"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||||
|
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSortNodes_moreThan10(t *testing.T) {
|
||||||
|
input := `
|
||||||
|
a: b
|
||||||
|
---
|
||||||
|
c: d
|
||||||
|
---
|
||||||
|
e: f
|
||||||
|
---
|
||||||
|
g: h
|
||||||
|
---
|
||||||
|
i: j
|
||||||
|
---
|
||||||
|
k: l
|
||||||
|
---
|
||||||
|
m: n
|
||||||
|
---
|
||||||
|
o: p
|
||||||
|
---
|
||||||
|
q: r
|
||||||
|
---
|
||||||
|
s: t
|
||||||
|
---
|
||||||
|
u: v
|
||||||
|
---
|
||||||
|
w: x
|
||||||
|
---
|
||||||
|
y: z
|
||||||
|
`
|
||||||
|
actual := &bytes.Buffer{}
|
||||||
|
rw := kio.ByteReadWriter{Reader: bytes.NewBufferString(input), Writer: actual}
|
||||||
|
nodes, err := rw.Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// randomize the list
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
rand.Shuffle(len(nodes), func(i, j int) { nodes[i], nodes[j] = nodes[j], nodes[i] })
|
||||||
|
|
||||||
|
// sort them back into their original order
|
||||||
|
if !assert.NoError(t, kioutil.SortNodes(nodes)) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the sorted values
|
||||||
|
expected := strings.Split(input, "---")
|
||||||
|
for i := range nodes {
|
||||||
|
a := strings.TrimSpace(nodes[i].MustString())
|
||||||
|
b := strings.TrimSpace(expected[i])
|
||||||
|
if !assert.Contains(t, a, b) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.NoError(t, rw.Write(nodes)) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, strings.TrimSpace(input), strings.TrimSpace(actual.String()))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user