mirror of https://github.com/hashicorp/packer
command/fmt: Ensure all variable files ending in `.pkrvars.hcl` get formatted (#10377)
Before change ``` ⇶ packer fmt -check /tmp/unformatted.pkrvars.hcl Error: Cannot tell whether /tmp/unformatted.pkrvars.hcl contains HCL2 configuration data ⇶ echo $? 1 ``` After fix ``` ⇶ packer fmt -check /tmp/unformatted.pkrvars.hcl /tmp/unformatted.pkrvars.hcl ⇶ echo $? 3 ⇶ packer fmt -check command/test-fixtures/fmt command/test-fixtures/fmt/unformatted.pkr.hcl command/test-fixtures/fmt/unformatted.auto.pkrvars.hcl command/test-fixtures/fmt/unformatted.pkrvars.hcl ```pull/10389/head
parent
889e42443a
commit
4e58987026
@ -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)
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,7 @@
|
||||
source "null" "example" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["source.null.example"]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
region ="us-west-2"
|
||||
@ -0,0 +1,11 @@
|
||||
variable "region" {
|
||||
type =string
|
||||
}
|
||||
|
||||
source "amazon-ebs" "example" {
|
||||
region = var.region
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["source.amazon-ebs.example"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
||||
ami_filter_owners =[ "137112412989" ]
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
source "null" "example" {
|
||||
communicator = "none"
|
||||
}
|
||||
|
||||
build {
|
||||
sources = ["source.null.example"]
|
||||
}
|
||||
@ -0,0 +1,3 @@
|
||||
ami_filter_name ="amzn2-ami-hvm-*-x86_64-gp2"
|
||||
ami_filter_owners =[ "137112412989" ]
|
||||
|
||||
Loading…
Reference in new issue