Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
22 changes: 22 additions & 0 deletions approvals/kir_test.TestCLI.StdinMultiDoc.input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
spec:
containers:
- image: nginx:1.27
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup
spec:
schedule: "0 0 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- image: backup:2.1
Empty file.
2 changes: 2 additions & 0 deletions approvals/kir_test.TestCLI.StdinMultiDoc.stdout.approved.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nginx:1.27
backup:2.1
12 changes: 12 additions & 0 deletions approvals/kir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ func TestCLI(t *testing.T) {
verify(t, []string{"-"}, f)
})

// StdinMultiDoc pins the multi-document stdin contract: every document in a
// piped stream is processed, not just the first. Without this the single-doc
// Stdin case above passes regardless, so the regression would go unnoticed.
t.Run("StdinMultiDoc", func(t *testing.T) {
f, err := os.Open("kir_test.TestCLI.StdinMultiDoc.input.yaml")
if err != nil {
t.Fatal(err)
}
defer f.Close()
verify(t, []string{"-"}, f)
})

t.Run("MissingFile", func(t *testing.T) {
verify(t, []string{"does-not-exist.yaml"}, nil)
})
Expand Down
8 changes: 7 additions & 1 deletion processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ func ProcessStdin(r io.Reader) ([]string, error) {
if err != nil {
return nil, fmt.Errorf("error reading stdin: %v", err)
}
return yamlparser.ProcessData(data)
return processDocuments(data)
}

func ProcessFile(filePath string) ([]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
return processDocuments(data)
}

// processDocuments splits a (possibly multi-document) YAML stream and collects
// the images from every document. Both the file and stdin paths go through it
// so they handle multi-document input identically.
func processDocuments(data []byte) ([]string, error) {
var images []string
docs := bytes.Split(data, []byte("\n---\n"))
for _, doc := range docs {
Expand Down
Loading