hcl/printer: file with only comments formats properly

This commit is contained in:
Mitchell Hashimoto 2016-10-08 11:42:34 +08:00
parent 3c8ec494b1
commit 16dda36a23
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 33 additions and 1 deletions

View File

@ -282,6 +282,29 @@ func TestObjectKey(t *testing.T) {
}
}
func TestCommentGroup(t *testing.T) {
var cases = []struct {
src string
groups int
}{
{"# Hello\n# World", 1},
}
for _, tc := range cases {
t.Run(tc.src, func(t *testing.T) {
p := newParser([]byte(tc.src))
file, err := p.Parse()
if err != nil {
t.Fatalf("parse error: %s", err)
}
if len(file.Comments) != tc.groups {
t.Fatalf("bad: %#v", file.Comments)
}
})
}
}
// Official HCL tests
func TestParse(t *testing.T) {
cases := []struct {

View File

@ -123,7 +123,10 @@ func (p *printer) output(n interface{}) []byte {
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
// if we hit the end add newlines so we can print the comment
if index == len(t.Items) {
// we don't do this if prev is invalid which means the
// beginning of the file since the first comment should
// be at the first line.
if p.prev.IsValid() && index == len(t.Items) {
buf.Write([]byte{newline, newline})
}

View File

@ -33,6 +33,7 @@ var data = []entry{
{"list.input", "list.golden"},
{"comment.input", "comment.golden"},
{"comment_aligned.input", "comment_aligned.golden"},
{"comment_newline.input", "comment_newline.golden"},
{"comment_standalone.input", "comment_standalone.golden"},
{"empty_block.input", "empty_block.golden"},
{"list_of_objects.input", "list_of_objects.golden"},

View File

@ -0,0 +1,3 @@
# Hello
# World

View File

@ -0,0 +1,2 @@
# Hello
# World