Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/command/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var (
".tf",
".tfvars",
".tftest.hcl",
".tfmock.hcl",
}
)

Expand Down
64 changes: 64 additions & 0 deletions internal/command/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions internal/command/testdata/tfmock-fmt/data_in.tfmock.hcl
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
22 changes: 22 additions & 0 deletions internal/command/testdata/tfmock-fmt/data_out.tfmock.hcl
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
9 changes: 9 additions & 0 deletions internal/command/testdata/tfmock-fmt/resource_in.tfmock.hcl
Original file line number Diff line number Diff line change
@@ -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"}
}
Original file line number Diff line number Diff line change
@@ -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" }
}