diff --git a/hcl2template/formatter_test.go b/hcl2template/formatter_test.go index ad88f6c4f..9e565b5f7 100644 --- a/hcl2template/formatter_test.go +++ b/hcl2template/formatter_test.go @@ -23,6 +23,8 @@ func TestHCL2Formatter_Format(t *testing.T) { {Name: "Unformatted vars file", Paths: []string{"testdata/format/unformatted.pkrvars.hcl"}, FormatExpected: true}, {Name: "Formatted file", Paths: []string{"testdata/format/formatted.pkr.hcl"}}, {Name: "Directory", Paths: []string{"testdata/format"}, FormatExpected: true}, + {Name: "No file", Paths: []string{}, FormatExpected: false}, + {Name: "Multi File", Paths: []string{"testdata/format/unformatted.pkr.hcl", "testdata/format/unformatted.pkrvars.hcl"}, FormatExpected: true}, } for _, tc := range tt { @@ -113,3 +115,24 @@ func TestHCL2Formatter_Format_ShowDiff(t *testing.T) { } } + +func TestHCL2Formatter_FormatNegativeCases(t *testing.T) { + tt := []struct { + Name string + Paths []string + errExpected bool + }{ + {Name: "Unformatted file", Paths: []string{"testdata/format/test.json"}, errExpected: true}, + } + + for _, tc := range tt { + tc := tc + var buf bytes.Buffer + f := NewHCL2Formatter() + f.Output = &buf + _, diags := f.Format(tc.Paths) + if tc.errExpected && !diags.HasErrors() { + t.Fatalf("Expected error but got none") + } + } +} diff --git a/hcl2template/testdata/format/test.json b/hcl2template/testdata/format/test.json new file mode 100644 index 000000000..eab6f7dd1 --- /dev/null +++ b/hcl2template/testdata/format/test.json @@ -0,0 +1,19 @@ +{ + "builders": [ + { + "type": "amazon-ebs", + "access_key": "YOUR_AWS_ACCESS_KEY", + "secret_key": "YOUR_AWS_SECRET_KEY", + "region": "us-east-1" + } + ], + "provisioners": [ + { + "type": "shell", + "inline": [ + "sudo apt-get update", + "sudo apt-get install -y nginx" + ] + } + ] +}