Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comments displacement #1069

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stores/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
57 changes: 57 additions & 0 deletions stores/yaml/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}