printer: output EOF newline in formatted hcl

Noticed that the `hclfmt` tool (which uses hcl/printer) was chomping
the trailing newline from config files.

Here we switch to the more conventional newline-at-EOF output.

In the process of changing this, I realized that:

 * The printer_test.go build tag was wrong, so it wasn't running
 * The format() test helper was not exercising Format

So both of those fixes are included here
This commit is contained in:
Paul Hinze 2016-04-06 09:09:04 -05:00
parent 2604f3bda7
commit 2eb00dcf94
8 changed files with 84 additions and 90 deletions

View File

@ -60,5 +60,8 @@ func Format(src []byte) ([]byte, error) {
return nil, err
}
// Add trailing newline to result
buf.WriteString("\n")
return buf.Bytes(), nil
}

View File

@ -1,4 +1,4 @@
// +build -windows
// +build !windows
// TODO(jen20): These need fixing on Windows but printer is not used right now
// and red CI is making it harder to process other bugs, so ignore until
// we get around to fixing them.package printer
@ -116,27 +116,17 @@ func diff(aname, bname string, a, b []byte) error {
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte) ([]byte, error) {
// parse src
node, err := parser.Parse(src)
formatted, err := Format(src)
if err != nil {
return nil, fmt.Errorf("parse: %s\n%s", err, src)
}
var buf bytes.Buffer
cfg := &Config{}
if err := cfg.Fprint(&buf, node); err != nil {
return nil, fmt.Errorf("print: %s", err)
return nil, err
}
// make sure formatted output is syntactically correct
res := buf.Bytes()
if _, err := parser.Parse(src); err != nil {
if _, err := parser.Parse(formatted); err != nil {
return nil, fmt.Errorf("parse: %s\n%s", err, src)
}
return res, nil
return formatted, nil
}
// lineAt returns the line in text starting at offset offs.

View File

@ -14,3 +14,4 @@ aligned {
numbers = [1, 2] // another line here
# Standalone 4