hcl/printer: file with only comments formats properly
This commit is contained in:
parent
3c8ec494b1
commit
16dda36a23
@ -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
|
// Official HCL tests
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
|
@ -123,7 +123,10 @@ func (p *printer) output(n interface{}) []byte {
|
|||||||
|
|
||||||
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
|
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
|
||||||
// if we hit the end add newlines so we can print the comment
|
// 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})
|
buf.Write([]byte{newline, newline})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ var data = []entry{
|
|||||||
{"list.input", "list.golden"},
|
{"list.input", "list.golden"},
|
||||||
{"comment.input", "comment.golden"},
|
{"comment.input", "comment.golden"},
|
||||||
{"comment_aligned.input", "comment_aligned.golden"},
|
{"comment_aligned.input", "comment_aligned.golden"},
|
||||||
|
{"comment_newline.input", "comment_newline.golden"},
|
||||||
{"comment_standalone.input", "comment_standalone.golden"},
|
{"comment_standalone.input", "comment_standalone.golden"},
|
||||||
{"empty_block.input", "empty_block.golden"},
|
{"empty_block.input", "empty_block.golden"},
|
||||||
{"list_of_objects.input", "list_of_objects.golden"},
|
{"list_of_objects.input", "list_of_objects.golden"},
|
||||||
|
3
hcl/printer/testdata/comment_newline.golden
vendored
Normal file
3
hcl/printer/testdata/comment_newline.golden
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Hello
|
||||||
|
# World
|
||||||
|
|
2
hcl/printer/testdata/comment_newline.input
vendored
Normal file
2
hcl/printer/testdata/comment_newline.input
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
# Hello
|
||||||
|
# World
|
Loading…
x
Reference in New Issue
Block a user