diff --git a/command/fmt.go b/command/fmt.go index d2533f6d8..822d5b3a1 100644 --- a/command/fmt.go +++ b/command/fmt.go @@ -72,7 +72,7 @@ func (*FormatCommand) Help() string { Usage: packer fmt [options] [TEMPLATE] Rewrites all Packer configuration files to a canonical format. Both - configuration files (.pkr.hcl) and variable files (.pkrvars) are updated. + configuration files (.pkr.hcl) and variable files (.pkrvars.hcl) are updated. JSON files (.json) are not modified. If TEMPATE is "." the current directory will be used. The given content must diff --git a/command/fmt_test.go b/command/fmt_test.go new file mode 100644 index 000000000..c4a4cd91a --- /dev/null +++ b/command/fmt_test.go @@ -0,0 +1,52 @@ +package command + +import ( + "path/filepath" + "strings" + "testing" + + packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer" + "github.com/stretchr/testify/assert" +) + +func TestFmt(t *testing.T) { + s := &strings.Builder{} + ui := &packersdk.BasicUi{ + Writer: s, + } + c := &FormatCommand{ + Meta: testMeta(t), + } + + c.Ui = ui + + args := []string{"-check=true", filepath.Join(testFixture("fmt"), "formatted.pkr.hcl")} + if code := c.Run(args); code != 0 { + fatalCommand(t, c.Meta) + } + expected := "" + assert.Equal(t, expected, strings.TrimSpace(s.String())) +} + +func TestFmt_unformattedPKRVarsTemplate(t *testing.T) { + c := &FormatCommand{ + Meta: testMeta(t), + } + + args := []string{"-check=true", filepath.Join(testFixture("fmt"), "unformatted.pkrvars.hcl")} + if code := c.Run(args); code != 3 { + fatalCommand(t, c.Meta) + } +} + +func TestFmt_unfomattedTemlateDirectory(t *testing.T) { + c := &FormatCommand{ + Meta: testMeta(t), + } + + args := []string{"-check=true", filepath.Join(testFixture("fmt"), "")} + + if code := c.Run(args); code != 3 { + fatalCommand(t, c.Meta) + } +} diff --git a/command/test-fixtures/fmt/formatted.pkr.hcl b/command/test-fixtures/fmt/formatted.pkr.hcl new file mode 100644 index 000000000..17c57f159 --- /dev/null +++ b/command/test-fixtures/fmt/formatted.pkr.hcl @@ -0,0 +1,7 @@ +source "null" "example" { + communicator = "none" +} + +build { + sources = ["source.null.example"] +} diff --git a/command/test-fixtures/fmt/unformatted.auto.pkrvars.hcl b/command/test-fixtures/fmt/unformatted.auto.pkrvars.hcl new file mode 100644 index 000000000..803e3d7e9 --- /dev/null +++ b/command/test-fixtures/fmt/unformatted.auto.pkrvars.hcl @@ -0,0 +1 @@ +region ="us-west-2" diff --git a/command/test-fixtures/fmt/unformatted.pkr.hcl b/command/test-fixtures/fmt/unformatted.pkr.hcl new file mode 100644 index 000000000..761d2f895 --- /dev/null +++ b/command/test-fixtures/fmt/unformatted.pkr.hcl @@ -0,0 +1,11 @@ +variable "region" { + type =string +} + +source "amazon-ebs" "example" { + region = var.region +} + +build { + sources = ["source.amazon-ebs.example"] +} diff --git a/command/test-fixtures/fmt/unformatted.pkrvars.hcl b/command/test-fixtures/fmt/unformatted.pkrvars.hcl new file mode 100644 index 000000000..7ddc9df79 --- /dev/null +++ b/command/test-fixtures/fmt/unformatted.pkrvars.hcl @@ -0,0 +1,3 @@ +ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2" +ami_filter_owners =[ "137112412989" ] + diff --git a/formatted.pkr.hcl b/formatted.pkr.hcl new file mode 100644 index 000000000..17c57f159 --- /dev/null +++ b/formatted.pkr.hcl @@ -0,0 +1,7 @@ +source "null" "example" { + communicator = "none" +} + +build { + sources = ["source.null.example"] +} diff --git a/hcl2template/formatter_test.go b/hcl2template/formatter_test.go index d015884c9..4d1474f71 100644 --- a/hcl2template/formatter_test.go +++ b/hcl2template/formatter_test.go @@ -18,6 +18,7 @@ func TestHCL2Formatter_Format(t *testing.T) { FormatExpected bool }{ {Name: "Unformatted file", Path: "testdata/format/unformatted.pkr.hcl", FormatExpected: true}, + {Name: "Unformatted vars file", Path: "testdata/format/unformatted.pkrvars.hcl", FormatExpected: true}, {Name: "Formatted file", Path: "testdata/format/formatted.pkr.hcl"}, {Name: "Directory", Path: "testdata/format", FormatExpected: true}, } diff --git a/hcl2template/parser.go b/hcl2template/parser.go index f8ae4c338..28e7fe9a6 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -63,10 +63,12 @@ type Parser struct { } const ( - hcl2FileExt = ".pkr.hcl" - hcl2JsonFileExt = ".pkr.json" - hcl2VarFileExt = ".auto.pkrvars.hcl" - hcl2VarJsonFileExt = ".auto.pkrvars.json" + hcl2FileExt = ".pkr.hcl" + hcl2JsonFileExt = ".pkr.json" + hcl2VarFileExt = ".pkrvars.hcl" + hcl2VarJsonFileExt = ".pkrvars.json" + hcl2AutoVarFileExt = ".auto.pkrvars.hcl" + hcl2AutoVarJsonFileExt = ".auto.pkrvars.json" ) // Parse will Parse all HCL files in filename. Path can be a folder or a file. @@ -162,7 +164,7 @@ func (p *Parser) Parse(filename string, varFiles []string, argVars map[string]st // parse var files { - hclVarFiles, jsonVarFiles, moreDiags := GetHCL2Files(filename, hcl2VarFileExt, hcl2VarJsonFileExt) + hclVarFiles, jsonVarFiles, moreDiags := GetHCL2Files(filename, hcl2AutoVarFileExt, hcl2AutoVarJsonFileExt) diags = append(diags, moreDiags...) for _, file := range varFiles { switch filepath.Ext(file) { diff --git a/hcl2template/testdata/format/unformatted.pkrvars.hcl b/hcl2template/testdata/format/unformatted.pkrvars.hcl new file mode 100644 index 000000000..7ddc9df79 --- /dev/null +++ b/hcl2template/testdata/format/unformatted.pkrvars.hcl @@ -0,0 +1,3 @@ +ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2" +ami_filter_owners =[ "137112412989" ] + diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index 11e0f3d52..f67259453 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -796,7 +796,7 @@ func TestVariables_collectVariableValues(t *testing.T) { var files []*hcl.File parser := getBasicParser() for i, hclContent := range tt.args.hclFiles { - file, diags := parser.ParseHCL([]byte(hclContent), fmt.Sprintf("test_file_%d_*"+hcl2VarFileExt, i)) + file, diags := parser.ParseHCL([]byte(hclContent), fmt.Sprintf("test_file_%d_*"+hcl2AutoVarFileExt, i)) if diags != nil { t.Fatalf("ParseHCLFile %d: %v", i, diags) }