Skip to content

Commit

Permalink
Improve handling of some empty documents.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Dec 30, 2020
1 parent 5eaf2d1 commit 24214d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stores/yaml/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func (store Store) appendYamlNodeToTreeBranch(node *yaml.Node, branch sops.TreeB
branch = store.appendCommentToMap(key.FootComment, branch)
}
case yaml.ScalarNode:
// A empty document with a document start marker without comments results in null
if node.ShortTag() == "!!null" {
return branch, nil
}
return nil, fmt.Errorf("YAML documents that are values are not supported")
case yaml.AliasNode:
branch, err = store.appendYamlNodeToTreeBranch(node.Alias, branch, false)
Expand Down
21 changes: 21 additions & 0 deletions stores/yaml/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@ func TestComment4(t *testing.T) {
assert.Equal(t, COMMENT_4, bytes)
}

func TestEmpty(t *testing.T) {
// First iteration: load and store
branches, err := (&Store{}).LoadPlainFile([]byte(``))
assert.Nil(t, err)
assert.Equal(t, len(branches), 0)
bytes, err := (&Store{}).EmitPlainFile(branches)
assert.Nil(t, err)
assert.Equal(t, ``, string(bytes))
}

func TestEmpty2(t *testing.T) {
// First iteration: load and store
branches, err := (&Store{}).LoadPlainFile([]byte(`---`))
assert.Nil(t, err)
assert.Equal(t, len(branches), 1)
assert.Equal(t, len(branches[0]), 0)
bytes, err := (&Store{}).EmitPlainFile(branches)
assert.Nil(t, err)
assert.Equal(t, ``, string(bytes))
}

func TestEmitValue(t *testing.T) {
// First iteration: load and store
bytes, err := (&Store{}).EmitValue(BRANCHES[0])
Expand Down

0 comments on commit 24214d3

Please sign in to comment.