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")
}
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()

@ -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)
}
})
}

@ -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)
}

Loading…
Cancel
Save