Skip to content

Commit

Permalink
fix empty yaml on documents with comment only sections (#166)
Browse files Browse the repository at this point in the history
* fix empty yaml when running vals eval on documents with comment only sections

Signed-off-by: Stephen Claypool <[email protected]>

* add tests for nodesFromReader

Signed-off-by: Stephen Claypool <[email protected]>

---------

Signed-off-by: Stephen Claypool <[email protected]>
  • Loading branch information
alstephenclaypool authored Sep 20, 2023
1 parent ae04ccd commit b51a963
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion io.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func nodesFromReader(reader io.Reader) ([]yaml.Node, error) {
}
break
}
nodes = append(nodes, node)
if len(node.Content[0].Content) > 0 {
nodes = append(nodes, node)
}
}
return nodes, nil
}
Expand Down
50 changes: 50 additions & 0 deletions io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,53 @@ func Test_InputOutput(t *testing.T) {
})
}
}

func Test_NodesFromReader(t *testing.T) {
simpleDocument := "---\nfoo: bar\n"
commentDocument := "---\n# comment\n"

tests := []struct {
name string
input string
nodes int
}{
{
name: "single document",
input: simpleDocument,
nodes: 1,
},
{
name: "multi document",
input: simpleDocument + simpleDocument,
nodes: 2,
},
{
name: "single comment document",
input: commentDocument,
nodes: 0,
},
{
name: "multiple comment document",
input: commentDocument + commentDocument,
nodes: 0,
},
{
name: "mixed documents",
input: simpleDocument + commentDocument,
nodes: 1,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
nodes, err := nodesFromReader(strings.NewReader(tt.input))
if err != nil {
t.Fatal(err)
}

if len(nodes) != tt.nodes {
t.Errorf("Expected %v nodes, got %v", tt.nodes, len(nodes))
}
})
}
}

0 comments on commit b51a963

Please sign in to comment.