terraform fmt: add .tfmock.hcl as a supported file extension (#34580)

* terraform fmt: add .tfmock.hcl as a supported file extension

* terraform fmt: add test cases for .tfmock.hcl files formatting

* fix: update test to run on .tfmock.hcl files
pull/34578/head
HenriBlacksmith 2 years ago committed by GitHub
parent 8faa757e20
commit 49663bfc33
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -32,6 +32,7 @@ var (
".tf",
".tfvars",
".tftest.hcl",
".tfmock.hcl",
}
)

@ -16,6 +16,70 @@ import (
"github.com/hashicorp/cli"
)
func TestFmt_MockDataFiles(t *testing.T) {
const inSuffix = "_in.tfmock.hcl"
const outSuffix = "_out.tfmock.hcl"
const gotSuffix = "_got.tfmock.hcl"
entries, err := ioutil.ReadDir("testdata/tfmock-fmt")
if err != nil {
t.Fatal(err)
}
tmpDir, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
for _, info := range entries {
if info.IsDir() {
continue
}
filename := info.Name()
if !strings.HasSuffix(filename, inSuffix) {
continue
}
testName := filename[:len(filename)-len(inSuffix)]
t.Run(testName, func(t *testing.T) {
inFile := filepath.Join("testdata", "tfmock-fmt", testName+inSuffix)
wantFile := filepath.Join("testdata", "tfmock-fmt", testName+outSuffix)
gotFile := filepath.Join(tmpDir, testName+gotSuffix)
input, err := ioutil.ReadFile(inFile)
if err != nil {
t.Fatal(err)
}
want, err := ioutil.ReadFile(wantFile)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(gotFile, input, 0700)
if err != nil {
t.Fatal(err)
}
ui := cli.NewMockUi()
c := &FmtCommand{
Meta: Meta{
testingOverrides: metaOverridesForProvider(testProvider()),
Ui: ui,
},
}
args := []string{gotFile}
if code := c.Run(args); code != 0 {
t.Fatalf("fmt command was unsuccessful:\n%s", ui.ErrorWriter.String())
}
got, err := ioutil.ReadFile(gotFile)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(string(want), string(got)); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
})
}
}
func TestFmt_TestFiles(t *testing.T) {
const inSuffix = "_in.tftest.hcl"
const outSuffix = "_out.tftest.hcl"

@ -0,0 +1,22 @@
mock_data "aws_availability_zones" {
defaults = {
names = [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
]
}
}
override_data {
target = data.aws_subnets.private_subnets
values = {
ids = ["subnet-a",
"subnet-b",
"subnet-c"
]
}
}

@ -0,0 +1,22 @@
mock_data "aws_availability_zones" {
defaults = {
names = [
"us-east-1a",
"us-east-1b",
"us-east-1c",
"us-east-1d",
"us-east-1e",
"us-east-1f"
]
}
}
override_data {
target = data.aws_subnets.private_subnets
values = {
ids = ["subnet-a",
"subnet-b",
"subnet-c"
]
}
}

@ -0,0 +1,9 @@
mock_resource "aws_s3_bucket" {
defaults = {
arn = "arn:aws:s3:::name"}
}
override_resource {
target = aws_launch_template.vm
values = {id = "lt-xyz"}
}

@ -0,0 +1,9 @@
mock_resource "aws_s3_bucket" {
defaults = {
arn = "arn:aws:s3:::name" }
}
override_resource {
target = aws_launch_template.vm
values = { id = "lt-xyz" }
}
Loading…
Cancel
Save