mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Add tests for reading json
This commit is contained in:
@@ -298,6 +298,25 @@ metadata:
|
|||||||
OmitReaderAnnotations: true,
|
OmitReaderAnnotations: true,
|
||||||
SetAnnotations: map[string]string{"foo": "bar"}},
|
SetAnnotations: map[string]string{"foo": "bar"}},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
{
|
||||||
|
name: "json",
|
||||||
|
input: `
|
||||||
|
{
|
||||||
|
"a": "b",
|
||||||
|
"c": [1, 2]
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
expectedItems: []string{
|
||||||
|
`
|
||||||
|
{"a": "b", "c": [1, 2], metadata: {annotations: {config.kubernetes.io/index: '0'}}}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
instance: ByteReader{},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range testCases {
|
for i := range testCases {
|
||||||
|
|||||||
@@ -141,6 +141,56 @@ metadata:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||||
|
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
|
defer s.clean()
|
||||||
|
|
||||||
|
s.writeFile(t, filepath.Join("a_test.json"), []byte(`{
|
||||||
|
"a": "b"
|
||||||
|
}`))
|
||||||
|
s.writeFile(t, filepath.Join("b_test.json"), []byte(`{
|
||||||
|
"e": "f",
|
||||||
|
"g": {
|
||||||
|
"h": ["i", "j"]
|
||||||
|
}
|
||||||
|
}`))
|
||||||
|
|
||||||
|
paths := []struct {
|
||||||
|
path string
|
||||||
|
}{
|
||||||
|
{path: "./"},
|
||||||
|
{path: s.root},
|
||||||
|
}
|
||||||
|
for _, p := range paths {
|
||||||
|
rfr := LocalPackageReader{PackagePath: p.path, MatchFilesGlob: []string{"*.json"}}
|
||||||
|
nodes, err := rfr.Read()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !assert.Len(t, nodes, 2) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
// TODO: Fix https://github.com/go-yaml/yaml/issues/44 so these are printed correctly
|
||||||
|
expected := []string{
|
||||||
|
`{"a": "b", metadata: {annotations: {config.kubernetes.io/index: '0', config.kubernetes.io/path: 'a_test.json'}}}
|
||||||
|
`,
|
||||||
|
`{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {config.kubernetes.io/index: '0',
|
||||||
|
config.kubernetes.io/path: 'b_test.json'}}}
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
for i := range nodes {
|
||||||
|
val, err := nodes[i].String()
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !assert.Equal(t, expected[i], val) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLocalPackageReader_Read_file(t *testing.T) {
|
func TestLocalPackageReader_Read_file(t *testing.T) {
|
||||||
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
s := setupDirectories(t, filepath.Join("a", "b"), filepath.Join("a", "c"))
|
||||||
defer s.clean()
|
defer s.clean()
|
||||||
|
|||||||
Reference in New Issue
Block a user