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 empty yaml on documents with comment only sections #166

Merged
merged 2 commits into from
Sep 20, 2023
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
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))
}
})
}
}