1
0
mirror of https://github.com/ko-build/ko.git synced 2025-01-20 18:28:32 +02:00

Ignore null YAML documents when using a label selector. (#107)

This commit is contained in:
Adam Harwayne 2019-11-07 14:12:24 -08:00 committed by jonjohnsonjr
parent 4833bb4a3e
commit 5a25402af9
3 changed files with 41 additions and 0 deletions

View File

@ -16,6 +16,7 @@ package commands
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"testing"
@ -101,6 +102,36 @@ func TestResolveMultiDocumentYAMLs(t *testing.T) {
}
}
func TestResolveMultiDocumentYAMLsWithSelector(t *testing.T) {
passesSelector := `apiVersion: something/v1
kind: Foo
metadata:
labels:
qux: baz
`
failsSelector := `apiVersion: other/v2
kind: Bar
`
// Note that this ends in '---', so it in ends in a final null YAML document.
inputYAML := []byte(fmt.Sprintf("%s---\n%s---", passesSelector, failsSelector))
base := mustRepository("gcr.io/multi-pass")
outputYAML, err := resolveFile(
yamlToTmpFile(t, inputYAML),
testBuilder,
kotesting.NewFixedPublish(base, testHashes),
&options.SelectorOptions{
Selector: "qux=baz",
},
&options.StrictOptions{})
if err != nil {
t.Fatalf("ImageReferences(%v) = %v", string(inputYAML), err)
}
if diff := cmp.Diff(passesSelector, string(outputYAML)); diff != "" {
t.Errorf("resolveFile (-want +got) = %v", diff)
}
}
func mustRepository(s string) name.Repository {
n, err := name.NewRepository(s)
if err != nil {

View File

@ -47,6 +47,11 @@ func MatchesSelector(doc *yaml.Node, selector labels.Selector) (bool, error) {
}
func docKind(doc *yaml.Node) (string, error) {
// Null nodes will fail the check below, so simply ignore them.
if doc.Tag == "!!null" {
return "", nil
}
it := FromNode(doc).
Filter(Intersect(
WithKind(yaml.MappingNode),

View File

@ -139,6 +139,11 @@ func TestMatchesSelector(t *testing.T) {
input: podList,
selector: labels.Nothing(),
matches: false,
}, {
desc: "null node",
input: "!!null",
selector: labels.Everything(),
matches: false,
}}
for _, test := range tests {