diff --git a/stores/yaml/store.go b/stores/yaml/store.go index a3ae2fb46..d9e78b528 100644 --- a/stores/yaml/store.go +++ b/stores/yaml/store.go @@ -70,7 +70,7 @@ func (store Store) nodeToTreeValue(node *yaml.Node, commentsWereHandled bool) (i return result, nil case yaml.MappingNode: branch := make(sops.TreeBranch, 0) - return store.appendYamlNodeToTreeBranch(node, branch, false) + return store.appendYamlNodeToTreeBranch(node, branch, commentsWereHandled) case yaml.ScalarNode: var result interface{} node.Decode(&result) diff --git a/stores/yaml/store_test.go b/stores/yaml/store_test.go index be6b90eb2..05249f4a5 100644 --- a/stores/yaml/store_test.go +++ b/stores/yaml/store_test.go @@ -91,6 +91,26 @@ var COMMENT_5 = []byte(`# foo key: value `) +// The following is a regression test for https://github.com/mozilla/sops/issues/865 +var COMMENT_6 = []byte(`a: + - a + # I no longer get duplicated + - {} +`) + +var COMMENT_6_BRANCHES = sops.TreeBranches{ + sops.TreeBranch{ + sops.TreeItem{ + Key: "a", + Value: []interface{}{ + "a", + sops.Comment{" I no longer get duplicated"}, + sops.TreeBranch{}, + }, + }, + }, +} + func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) { data := []byte(`hello: 2`) _, err := (&Store{}).LoadEncryptedFile(data) @@ -178,6 +198,16 @@ func TestEmpty2(t *testing.T) { } */ +func TestComment6(t *testing.T) { + branches, err := (&Store{}).LoadPlainFile(COMMENT_6) + assert.Nil(t, err) + assert.Equal(t, COMMENT_6_BRANCHES, branches) + bytes, err := (&Store{}).EmitPlainFile(branches) + assert.Nil(t, err) + assert.Equal(t, string(COMMENT_6), string(bytes)) + assert.Equal(t, COMMENT_6, bytes) +} + func TestEmitValue(t *testing.T) { // First iteration: load and store bytes, err := (&Store{}).EmitValue(BRANCHES[0])