fix: use Reader interface as the input of LoadValues and enhance UT of LoadValues

Signed-off-by: lubingtan <bingtanlu@gmail.com>
pull/13655/head
lubingtan 7 months ago
parent 92087f6e33
commit 3d84e00ce7

@ -107,7 +107,7 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) {
return c, errors.Wrap(err, "cannot load Chart.lock") return c, errors.Wrap(err, "cannot load Chart.lock")
} }
case f.Name == "values.yaml": case f.Name == "values.yaml":
values, err := LoadValues(f.Data) values, err := LoadValues(bytes.NewReader(f.Data))
if err != nil { if err != nil {
return c, errors.Wrap(err, "cannot load values.yaml") return c, errors.Wrap(err, "cannot load values.yaml")
} }
@ -207,9 +207,10 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) {
return c, nil 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{}{} values := map[string]interface{}{}
reader := utilyaml.NewYAMLReader(bufio.NewReader(bytes.NewReader(data))) reader := utilyaml.NewYAMLReader(bufio.NewReader(data))
for { for {
currentMap := map[string]interface{}{} currentMap := map[string]interface{}{}
raw, err := reader.Read() raw, err := reader.Read()

@ -490,13 +490,11 @@ func TestLoadInvalidArchive(t *testing.T) {
} }
func TestLoadValues(t *testing.T) { func TestLoadValues(t *testing.T) {
testDatas := []struct { testCases := map[string]struct {
name string
data []byte data []byte
expctedValues map[string]interface{} expctedValues map[string]interface{}
}{ }{
{ "It should load values correctly": {
name: "It should load values correctly",
data: []byte(` data: []byte(`
foo: foo:
image: foo:v1 image: foo:v1
@ -512,8 +510,7 @@ bar:
}, },
}, },
}, },
{ "It should load values correctly with multiple documents in one file": {
name: "It should load values correctly with multiple documents in one file",
data: []byte(` data: []byte(`
foo: foo:
image: foo:v1 image: foo:v1
@ -533,14 +530,14 @@ foo:
}, },
}, },
} }
for _, testData := range testDatas { for testName, testCase := range testCases {
t.Run(testData.name, func(tt *testing.T) { t.Run(testName, func(tt *testing.T) {
values, err := LoadValues(testData.data) values, err := LoadValues(bytes.NewReader(testCase.data))
if err != nil { if err != nil {
tt.Fatal(err) tt.Fatal(err)
} }
if !reflect.DeepEqual(values, testData.expctedValues) { if !reflect.DeepEqual(values, testCase.expctedValues) {
tt.Errorf("Expected values: %v, got %v", testData.expctedValues, values) tt.Errorf("Expected values: %v, got %v", testCase.expctedValues, values)
} }
}) })
} }

@ -17,6 +17,7 @@ limitations under the License.
package values package values
import ( import (
"bytes"
"encoding/json" "encoding/json"
"io" "io"
"net/url" "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 // User specified a values files via -f/--values
for _, filePath := range opts.ValueFiles { for _, filePath := range opts.ValueFiles {
bytes, err := readFile(filePath, p) raw, err := readFile(filePath, p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
currentMap, err := loader.LoadValues(bytes) currentMap, err := loader.LoadValues(bytes.NewReader(raw))
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath) return nil, errors.Wrapf(err, "failed to parse %s", filePath)
} }

Loading…
Cancel
Save