1
0
mirror of https://github.com/ko-build/ko.git synced 2024-12-12 08:54:09 +02:00

Fix mult-doc yaml re-joining (#63)

This commit is contained in:
jonjohnsonjr 2019-07-23 09:04:40 -07:00 committed by GitHub
parent 0df8b2497e
commit 3a0e70e520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 44 deletions

View File

@ -17,13 +17,14 @@ package resolve
import (
"bytes"
"fmt"
"regexp"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
yaml2json "k8s.io/apimachinery/pkg/util/yaml"
"regexp"
"sigs.k8s.io/yaml"
"strings"
)
// FilterBySelector filters out any resources
@ -87,7 +88,7 @@ func FilterBySelector(input []byte, selectorString string) ([]byte, error) {
}
// re-join the objects into a single manifest
return bytes.Join(outputObjectsYaml, []byte("---\n")), nil
return bytes.Join(outputObjectsYaml, []byte("\n---")), nil
}
var yamlSeparatorRegex = regexp.MustCompile("\n---")

View File

@ -36,10 +36,6 @@ metadata:
app: db
name: rss-db
`
bothPods = webPod + `
---
` + dbPod
podList = `apiVersion: v1
kind: List
metadata:
@ -61,6 +57,7 @@ items:
`
webSelector = `app=web`
notWebSelector = `app!=web`
nopSelector = `foo!=bark`
webPodList = `apiVersion: v1
items:
@ -90,6 +87,8 @@ metadata:
`
)
var bothPods = strings.Join([]string{webPod, dbPod}, "\n---\n")
func TestSelector(t *testing.T) {
tests := []struct {
desc string
@ -101,43 +100,42 @@ func TestSelector(t *testing.T) {
input: webPod,
selector: webSelector,
expected: webPod,
},
{
desc: "single object with non-matching selector",
input: webPod,
selector: notWebSelector,
expected: ``,
},
{
desc: "selector matching 1 of two objects",
input: bothPods,
selector: webSelector,
expected: webPod,
},
{
desc: "selector matching 1 of two objects",
input: bothPods,
selector: notWebSelector,
expected: dbPod,
},
{
desc: "selector matching elements of list object",
input: podList,
selector: webSelector,
expected: webPodList,
},
{
desc: "selector matching elements of list object",
input: podList,
selector: notWebSelector,
expected: dbPodList,
},
{
desc: "selector matching all elements of list object",
input: podList,
selector: ``,
expected: podList,
}}
}, {
desc: "single object with non-matching selector",
input: webPod,
selector: notWebSelector,
expected: ``,
}, {
desc: "selector matching 1 of two objects",
input: bothPods,
selector: webSelector,
expected: webPod,
}, {
desc: "selector matching 1 of two objects",
input: bothPods,
selector: notWebSelector,
expected: dbPod,
}, {
desc: "selector matching both objects",
input: bothPods,
selector: nopSelector,
expected: bothPods,
}, {
desc: "selector matching elements of list object",
input: podList,
selector: webSelector,
expected: webPodList,
}, {
desc: "selector matching elements of list object",
input: podList,
selector: notWebSelector,
expected: dbPodList,
}, {
desc: "selector matching all elements of list object",
input: podList,
selector: ``,
expected: podList,
}}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {