diff --git a/processor/processdocuments_test.go b/processor/processdocuments_test.go new file mode 100644 index 0000000..c92a0da --- /dev/null +++ b/processor/processdocuments_test.go @@ -0,0 +1,43 @@ +package processor + +import "testing" + +// processDocuments backs both ProcessFile and ProcessStdin. Previously stdin +// only ever processed the first document; this verifies the shared path +// collects images from every document in a multi-document stream. +func TestProcessDocumentsMultiple(t *testing.T) { + data := []byte(` +apiVersion: v1 +kind: Pod +metadata: + name: first +spec: + containers: + - name: c + image: image-one +--- +apiVersion: v1 +kind: Pod +metadata: + name: second +spec: + containers: + - name: c + image: image-two +`) + + images, err := processDocuments(data) + if err != nil { + t.Fatalf("processDocuments() error = %v", err) + } + + expected := []string{"image-one", "image-two"} + if len(images) != len(expected) { + t.Fatalf("expected %d images, got %d: %v", len(expected), len(images), images) + } + for i, img := range images { + if img != expected[i] { + t.Errorf("expected image %q, got %q", expected[i], img) + } + } +} diff --git a/processor/processor.go b/processor/processor.go index ac70631..20deac6 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -1,7 +1,6 @@ package processor import ( - "bufio" "bytes" "fmt" "io" @@ -11,19 +10,11 @@ import ( ) func ProcessStdin() ([]string, error) { - reader := bufio.NewReader(os.Stdin) - var data []byte - for { - line, err := reader.ReadBytes('\n') - if err != nil && err != io.EOF { - return nil, fmt.Errorf("error reading stdin: %v", err) - } - data = append(data, line...) - if err == io.EOF { - break - } + data, err := io.ReadAll(os.Stdin) + 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) { @@ -31,7 +22,13 @@ func ProcessFile(filePath string) ([]string, error) { 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 {