From 86d02ac36811dfadad089f9a5d2ceb95b1288056 Mon Sep 17 00:00:00 2001 From: Steve Larkin Date: Sun, 10 Dec 2017 13:00:14 +0100 Subject: [PATCH] Fix YAMLDecoder Read behaviour Make it adhere to the Read contract by returning the number of bytes read. --- .../k8s.io/apimachinery/pkg/util/yaml/decoder.go | 2 +- .../apimachinery/pkg/util/yaml/decoder_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go index 6ebfaea707d..56de33a7fdf 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder.go @@ -122,7 +122,7 @@ func (d *YAMLDecoder) Read(data []byte) (n int, err error) { if left <= len(data) { copy(data, d.remaining) d.remaining = nil - return len(d.remaining), nil + return left, nil } // caller will need to reread diff --git a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go index bd4403648f4..1eebd2018f0 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/yaml/decoder_test.go @@ -22,12 +22,26 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "math/rand" "reflect" "strings" "testing" ) +func TestYAMLDecoder(t *testing.T) { + d := `--- +stuff: 1 + test-foo: 1 +` + s := NewDocumentDecoder(ioutil.NopCloser(bytes.NewReader([]byte(d)))) + b := make([]byte, len(d)) + n, err := s.Read(b) + if err != nil || n != len(d) { + t.Fatalf("unexpected body: %d / %v", n, err) + } +} + func TestSplitYAMLDocument(t *testing.T) { testCases := []struct { input string