Skip to content

Commit 65e1af5

Browse files
authored
Ignore nil YAML (#49)
Signed-off-by: Dale Haiducek <[email protected]>
1 parent be81561 commit 65e1af5

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

internal/utils.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,15 @@ func unmarshalManifestBytes(manifestBytes []byte) (*[]map[string]interface{}, er
333333
return nil, err
334334
}
335335

336-
if _, ok := obj.(map[string]interface{}); !ok {
336+
if _, ok := obj.(map[string]interface{}); !ok && obj != nil {
337337
err := errors.New("the input manifests must be in the format of YAML objects")
338338

339339
return nil, err
340340
}
341341

342-
yamlDocs = append(yamlDocs, obj.(map[string]interface{}))
342+
if obj != nil {
343+
yamlDocs = append(yamlDocs, obj.(map[string]interface{}))
344+
}
343345
}
344346

345347
return &yamlDocs, nil

internal/utils_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,48 @@ data:
788788
assertEqual(t, name2, "my-configmap2")
789789
}
790790

791+
func TestUnmarshalManifestFileNilYaml(t *testing.T) {
792+
t.Parallel()
793+
tmpDir := t.TempDir()
794+
manifestsPath := path.Join(tmpDir, "configmaps.yaml")
795+
yamlContent := `
796+
---
797+
---
798+
apiVersion: v1
799+
kind: ConfigMap
800+
metadata:
801+
name: my-configmap
802+
data:
803+
game.properties: |
804+
enemies=goldfish
805+
---
806+
apiVersion: v1
807+
kind: ConfigMap
808+
metadata:
809+
name: my-configmap2
810+
data:
811+
game.properties: |
812+
enemies=potato
813+
---
814+
---
815+
`
816+
err := ioutil.WriteFile(manifestsPath, []byte(yamlContent), 0o666)
817+
if err != nil {
818+
t.Fatalf("Failed to write %s", manifestsPath)
819+
}
820+
821+
manifests, err := unmarshalManifestFile(manifestsPath)
822+
if err != nil {
823+
t.Fatalf("Failed to unmarshal the YAML content, got: %v", err)
824+
}
825+
826+
assertEqual(t, len(*manifests), 2)
827+
name1, _, _ := unstructured.NestedString((*manifests)[0], "metadata", "name")
828+
assertEqual(t, name1, "my-configmap")
829+
name2, _, _ := unstructured.NestedString((*manifests)[1], "metadata", "name")
830+
assertEqual(t, name2, "my-configmap2")
831+
}
832+
791833
func TestUnmarshalManifestFileUnreadable(t *testing.T) {
792834
t.Parallel()
793835
tmpDir := t.TempDir()

0 commit comments

Comments
 (0)