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:
parent
2604f3bda7
commit
2eb00dcf94
@ -60,5 +60,8 @@ func Format(src []byte) ([]byte, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add trailing newline to result
|
||||||
|
buf.WriteString("\n")
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
// +build -windows
|
// +build !windows
|
||||||
// TODO(jen20): These need fixing on Windows but printer is not used right now
|
// 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
|
// and red CI is making it harder to process other bugs, so ignore until
|
||||||
// we get around to fixing them.package printer
|
// 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
|
// src is syntactically correct, and returns the resulting src or an error
|
||||||
// if any.
|
// if any.
|
||||||
func format(src []byte) ([]byte, error) {
|
func format(src []byte) ([]byte, error) {
|
||||||
// parse src
|
formatted, err := Format(src)
|
||||||
node, err := parser.Parse(src)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parse: %s\n%s", err, src)
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
cfg := &Config{}
|
|
||||||
if err := cfg.Fprint(&buf, node); err != nil {
|
|
||||||
return nil, fmt.Errorf("print: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure formatted output is syntactically correct
|
// make sure formatted output is syntactically correct
|
||||||
res := buf.Bytes()
|
if _, err := parser.Parse(formatted); err != nil {
|
||||||
|
|
||||||
if _, err := parser.Parse(src); err != nil {
|
|
||||||
return nil, fmt.Errorf("parse: %s\n%s", err, src)
|
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.
|
// lineAt returns the line in text starting at offset offs.
|
||||||
|
@ -14,3 +14,4 @@ aligned {
|
|||||||
numbers = [1, 2] // another line here
|
numbers = [1, 2] // another line here
|
||||||
|
|
||||||
# Standalone 4
|
# Standalone 4
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user