From 3d84e00ce756f34f8d3cf41c1489548696edd8a7 Mon Sep 17 00:00:00 2001 From: lubingtan Date: Sun, 9 Feb 2025 16:16:08 +0800 Subject: [PATCH] fix: use Reader interface as the input of LoadValues and enhance UT of LoadValues Signed-off-by: lubingtan --- pkg/chart/loader/load.go | 7 ++++--- pkg/chart/loader/load_test.go | 19 ++++++++----------- pkg/cli/values/options.go | 5 +++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pkg/chart/loader/load.go b/pkg/chart/loader/load.go index c40a1aaf5..84a040c0f 100644 --- a/pkg/chart/loader/load.go +++ b/pkg/chart/loader/load.go @@ -107,7 +107,7 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) { return c, errors.Wrap(err, "cannot load Chart.lock") } case f.Name == "values.yaml": - values, err := LoadValues(f.Data) + values, err := LoadValues(bytes.NewReader(f.Data)) if err != nil { return c, errors.Wrap(err, "cannot load values.yaml") } @@ -207,9 +207,10 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) { return c, nil } -func LoadValues(data []byte) (map[string]interface{}, error) { +// LoadValues loads chat values from a reader. +func LoadValues(data io.Reader) (map[string]interface{}, error) { values := map[string]interface{}{} - reader := utilyaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(data))) + reader := utilyaml.NewYAMLReader(bufio.NewReader(data)) for { currentMap := map[string]interface{}{} raw, err := reader.Read() diff --git a/pkg/chart/loader/load_test.go b/pkg/chart/loader/load_test.go index 92ffa002b..e34124829 100644 --- a/pkg/chart/loader/load_test.go +++ b/pkg/chart/loader/load_test.go @@ -490,13 +490,11 @@ func TestLoadInvalidArchive(t *testing.T) { } func TestLoadValues(t *testing.T) { - testDatas := []struct { - name string + testCases := map[string]struct { data []byte expctedValues map[string]interface{} }{ - { - name: "It should load values correctly", + "It should load values correctly": { data: []byte(` foo: image: foo:v1 @@ -512,8 +510,7 @@ bar: }, }, }, - { - name: "It should load values correctly with multiple documents in one file", + "It should load values correctly with multiple documents in one file": { data: []byte(` foo: image: foo:v1 @@ -533,14 +530,14 @@ foo: }, }, } - for _, testData := range testDatas { - t.Run(testData.name, func(tt *testing.T) { - values, err := LoadValues(testData.data) + for testName, testCase := range testCases { + t.Run(testName, func(tt *testing.T) { + values, err := LoadValues(bytes.NewReader(testCase.data)) if err != nil { tt.Fatal(err) } - if !reflect.DeepEqual(values, testData.expctedValues) { - tt.Errorf("Expected values: %v, got %v", testData.expctedValues, values) + if !reflect.DeepEqual(values, testCase.expctedValues) { + tt.Errorf("Expected values: %v, got %v", testCase.expctedValues, values) } }) } diff --git a/pkg/cli/values/options.go b/pkg/cli/values/options.go index 70390f12a..54dd288bc 100644 --- a/pkg/cli/values/options.go +++ b/pkg/cli/values/options.go @@ -17,6 +17,7 @@ limitations under the License. package values import ( + "bytes" "encoding/json" "io" "net/url" @@ -47,11 +48,11 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er // User specified a values files via -f/--values for _, filePath := range opts.ValueFiles { - bytes, err := readFile(filePath, p) + raw, err := readFile(filePath, p) if err != nil { return nil, err } - currentMap, err := loader.LoadValues(bytes) + currentMap, err := loader.LoadValues(bytes.NewReader(raw)) if err != nil { return nil, errors.Wrapf(err, "failed to parse %s", filePath) }