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.
|
||||||
|
20
hcl/printer/testdata/comment.golden
vendored
20
hcl/printer/testdata/comment.golden
vendored
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
// This comes from Terraform, as a test
|
// This comes from Terraform, as a test
|
||||||
variable "foo" {
|
variable "foo" {
|
||||||
# Standalone comment should be still here
|
# Standalone comment should be still here
|
||||||
|
|
||||||
default = "bar"
|
default = "bar"
|
||||||
description = "bar" # yooo
|
description = "bar" # yooo
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This is a multi line standalone
|
/* This is a multi line standalone
|
||||||
@ -21,16 +21,16 @@ numbers = [1, 2] // another line here
|
|||||||
|
|
||||||
# Another comment
|
# Another comment
|
||||||
variable = {
|
variable = {
|
||||||
description = "bar" # another yooo
|
description = "bar" # another yooo
|
||||||
|
|
||||||
foo {
|
foo {
|
||||||
# Nested standalone
|
# Nested standalone
|
||||||
|
|
||||||
bar = "fatih"
|
bar = "fatih"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// lead comment
|
// lead comment
|
||||||
foo {
|
foo {
|
||||||
bar = "fatih" // line comment 2
|
bar = "fatih" // line comment 2
|
||||||
} // line comment 3
|
} // line comment 3
|
||||||
|
50
hcl/printer/testdata/comment_aligned.golden
vendored
50
hcl/printer/testdata/comment_aligned.golden
vendored
@ -1,32 +1,32 @@
|
|||||||
aligned {
|
aligned {
|
||||||
# We have some aligned items below
|
# We have some aligned items below
|
||||||
foo = "fatih" # yoo1
|
foo = "fatih" # yoo1
|
||||||
default = "bar" # yoo2
|
default = "bar" # yoo2
|
||||||
bar = "bar and foo" # yoo3
|
bar = "bar and foo" # yoo3
|
||||||
|
|
||||||
default = {
|
default = {
|
||||||
bar = "example"
|
bar = "example"
|
||||||
}
|
}
|
||||||
|
|
||||||
#deneme arslan
|
#deneme arslan
|
||||||
fatih = ["fatih"] # yoo4
|
fatih = ["fatih"] # yoo4
|
||||||
|
|
||||||
#fatih arslan
|
#fatih arslan
|
||||||
fatiharslan = ["arslan"] // yoo5
|
fatiharslan = ["arslan"] // yoo5
|
||||||
|
|
||||||
default = {
|
default = {
|
||||||
bar = "example"
|
bar = "example"
|
||||||
}
|
}
|
||||||
|
|
||||||
security_groups = [
|
security_groups = [
|
||||||
"foo", # kenya 1
|
"foo", # kenya 1
|
||||||
"${aws_security_group.firewall.foo}", # kenya 2
|
"${aws_security_group.firewall.foo}", # kenya 2
|
||||||
]
|
]
|
||||||
|
|
||||||
security_groups2 = [
|
security_groups2 = [
|
||||||
"foo", # kenya 1
|
"foo", # kenya 1
|
||||||
"bar", # kenya 1.5
|
"bar", # kenya 1.5
|
||||||
"${aws_security_group.firewall.foo}", # kenya 2
|
"${aws_security_group.firewall.foo}", # kenya 2
|
||||||
"foobar", # kenya 3
|
"foobar", # kenya 3
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
// A standalone comment
|
// A standalone comment
|
||||||
|
|
||||||
aligned {
|
aligned {
|
||||||
# Standalone 1
|
# Standalone 1
|
||||||
|
|
||||||
a = "bar" # yoo1
|
a = "bar" # yoo1
|
||||||
default = "bar" # yoo2
|
default = "bar" # yoo2
|
||||||
|
|
||||||
# Standalone 2
|
# Standalone 2
|
||||||
}
|
}
|
||||||
|
|
||||||
# Standalone 3
|
# Standalone 3
|
||||||
@ -14,3 +14,4 @@ aligned {
|
|||||||
numbers = [1, 2] // another line here
|
numbers = [1, 2] // another line here
|
||||||
|
|
||||||
# Standalone 4
|
# Standalone 4
|
||||||
|
|
||||||
|
48
hcl/printer/testdata/complexhcl.golden
vendored
48
hcl/printer/testdata/complexhcl.golden
vendored
@ -1,54 +1,54 @@
|
|||||||
variable "foo" {
|
variable "foo" {
|
||||||
default = "bar"
|
default = "bar"
|
||||||
description = "bar"
|
description = "bar"
|
||||||
}
|
}
|
||||||
|
|
||||||
developer = ["fatih", "arslan"]
|
developer = ["fatih", "arslan"]
|
||||||
|
|
||||||
provider "aws" {
|
provider "aws" {
|
||||||
access_key = "foo"
|
access_key = "foo"
|
||||||
secret_key = "bar"
|
secret_key = "bar"
|
||||||
}
|
}
|
||||||
|
|
||||||
provider "do" {
|
provider "do" {
|
||||||
api_key = "${var.foo}"
|
api_key = "${var.foo}"
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_security_group" "firewall" {
|
resource "aws_security_group" "firewall" {
|
||||||
count = 5
|
count = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
resource aws_instance "web" {
|
resource aws_instance "web" {
|
||||||
ami = "${var.foo}"
|
ami = "${var.foo}"
|
||||||
|
|
||||||
security_groups = [
|
security_groups = [
|
||||||
"foo",
|
"foo",
|
||||||
"${aws_security_group.firewall.foo}",
|
"${aws_security_group.firewall.foo}",
|
||||||
]
|
]
|
||||||
|
|
||||||
network_interface {
|
network_interface {
|
||||||
device_index = 0
|
device_index = 0
|
||||||
description = "Main network interface"
|
description = "Main network interface"
|
||||||
}
|
}
|
||||||
|
|
||||||
network_interface = {
|
network_interface = {
|
||||||
device_index = 1
|
device_index = 1
|
||||||
|
|
||||||
description = <<EOF
|
description = <<EOF
|
||||||
ANOTHER NETWORK INTERFACE
|
ANOTHER NETWORK INTERFACE
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
resource "aws_instance" "db" {
|
resource "aws_instance" "db" {
|
||||||
security_groups = "${aws_security_group.firewall.*.id}"
|
security_groups = "${aws_security_group.firewall.*.id}"
|
||||||
VPC = "foo"
|
VPC = "foo"
|
||||||
|
|
||||||
depends_on = ["aws_instance.web"]
|
depends_on = ["aws_instance.web"]
|
||||||
}
|
}
|
||||||
|
|
||||||
output "web_ip" {
|
output "web_ip" {
|
||||||
value = <<EOF
|
value = <<EOF
|
||||||
TUBES
|
TUBES
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
6
hcl/printer/testdata/empty_block.golden
vendored
6
hcl/printer/testdata/empty_block.golden
vendored
@ -3,11 +3,11 @@ variable "foo" {}
|
|||||||
variable "foo" {}
|
variable "foo" {}
|
||||||
|
|
||||||
variable "foo" {
|
variable "foo" {
|
||||||
# Standalone comment should be still here
|
# Standalone comment should be still here
|
||||||
}
|
}
|
||||||
|
|
||||||
foo {}
|
foo {}
|
||||||
|
|
||||||
foo {
|
foo {
|
||||||
bar = "mssola"
|
bar = "mssola"
|
||||||
}
|
}
|
||||||
|
18
hcl/printer/testdata/list.golden
vendored
18
hcl/printer/testdata/list.golden
vendored
@ -3,17 +3,17 @@ foo = ["fatih", "arslan"]
|
|||||||
foo = ["bar", "qaz"]
|
foo = ["bar", "qaz"]
|
||||||
|
|
||||||
foo = ["zeynep",
|
foo = ["zeynep",
|
||||||
"arslan",
|
"arslan",
|
||||||
]
|
]
|
||||||
|
|
||||||
foo = ["fatih", "zeynep",
|
foo = ["fatih", "zeynep",
|
||||||
"arslan",
|
"arslan",
|
||||||
]
|
]
|
||||||
|
|
||||||
foo = [
|
foo = [
|
||||||
"vim-go",
|
"vim-go",
|
||||||
"golang",
|
"golang",
|
||||||
"hcl",
|
"hcl",
|
||||||
]
|
]
|
||||||
|
|
||||||
foo = []
|
foo = []
|
||||||
@ -21,7 +21,7 @@ foo = []
|
|||||||
foo = [1, 2, 3, 4]
|
foo = [1, 2, 3, 4]
|
||||||
|
|
||||||
foo = [
|
foo = [
|
||||||
"kenya",
|
"kenya",
|
||||||
"ethiopia",
|
"ethiopia",
|
||||||
"columbia",
|
"columbia",
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user