diff --git a/stores/yaml/store.go b/stores/yaml/store.go index da9b946b2..0a95d7c6a 100644 --- a/stores/yaml/store.go +++ b/stores/yaml/store.go @@ -233,7 +233,7 @@ func (store *Store) appendTreeBranch(branch sops.TreeBranch, mapping *yaml.Node) if beginning { comments = store.addCommentsHead(mapping, comments) } else { - comments = store.addCommentsFoot(mapping.Content[len(mapping.Content) - 1], comments) + comments = store.addCommentsFoot(mapping.Content[len(mapping.Content) - 2], comments) } } } diff --git a/stores/yaml/store_test.go b/stores/yaml/store_test.go index bf7c972f5..245a43104 100644 --- a/stores/yaml/store_test.go +++ b/stores/yaml/store_test.go @@ -111,6 +111,53 @@ var COMMENT_6_BRANCHES = sops.TreeBranches{ }, } +// The following is a regression test for https://github.com/mozilla/sops/issues/1068 +var COMMENT_7_IN = []byte(`a: + b: + c: d + # comment + +e: + - f +`) + +var COMMENT_7_BRANCHES = sops.TreeBranches{ + sops.TreeBranch{ + sops.TreeItem{ + Key: "a", + Value: sops.TreeBranch{ + sops.TreeItem{ + Key: "b", + Value: sops.TreeBranch{ + sops.TreeItem{ + Key: "c", + Value: "d", + }, + }, + }, + sops.TreeItem{ + Key: sops.Comment{" comment"}, + Value: nil, + }, + }, + }, + sops.TreeItem{ + Key: "e", + Value: []interface{}{ + "f", + }, + }, + }, +} + +var COMMENT_7_OUT = []byte(`a: + b: + c: d + # comment +e: + - f +`) + func TestUnmarshalMetadataFromNonSOPSFile(t *testing.T) { data := []byte(`hello: 2`) _, err := (&Store{}).LoadEncryptedFile(data) @@ -224,3 +271,13 @@ func TestEmitValue(t *testing.T) { assert.Equal(t, string(PLAIN_0), string(bytes)) assert.Equal(t, PLAIN_0, bytes) } + +func TestComment7(t *testing.T) { + branches, err := (&Store{}).LoadPlainFile(COMMENT_7_IN) + assert.Nil(t, err) + assert.Equal(t, COMMENT_7_BRANCHES, branches) + bytes, err := (&Store{}).EmitPlainFile(branches) + assert.Nil(t, err) + assert.Equal(t, string(COMMENT_7_OUT), string(bytes)) + assert.Equal(t, COMMENT_7_OUT, bytes) +}