Skip to content

Commit

Permalink
Add tests for block finder
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Dec 19, 2022
1 parent 1d75a3b commit 3de0df3
Showing 1 changed file with 156 additions and 41 deletions.
197 changes: 156 additions & 41 deletions tfwrite/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,68 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestFileFindResourcesByType(t *testing.T) {
func TestParseBlock(t *testing.T) {
cases := []struct {
desc string
src string
resourceType string
want string
ok bool
desc string
src string
want string
ok bool
}{
{
desc: "resource",
src: `
resource "foo_test" "example" {}
`,
want: "resource",
ok: true,
},
{
desc: "data",
src: `
data "foo_test" "example" {}
`,
want: "data",
ok: true,
},
{
desc: "provider",
src: `
provider "foo" {}
`,
want: "provider",
ok: true,
},
{
desc: "unknown",
src: `
b "foo" {}
`,
want: "b",
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
f := parseTestFile(t, tc.src)
b := parseBlock(findFirstTestBlock(t, f).Raw())
got := b.Type()
if got != tc.want {
t.Errorf("got = %s, but want = %s", got, tc.want)
}
})
}
}

func TestFileBlocks(t *testing.T) {
cases := []struct {
desc string
src string
want []Block
ok bool
}{
{
desc: "simple",
Expand All @@ -22,7 +75,66 @@ resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
data "foo_test" "example1" {}
`,
resourceType: "foo_test",
want: []Block{
NewEmptyResource("foo_test", "example1"),
NewEmptyResource("foo_test", "example2"),
NewEmptyResource("foo_bar", "example1"),
NewEmptyDataSource("foo_test", "example1"),
},
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
f := parseTestFile(t, tc.src)
got := f.Blocks()
opts := cmpopts.IgnoreUnexported(Resource{}, DataSource{})
if diff := cmp.Diff(got, tc.want, opts); diff != "" {
t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff)
}
})
}
}

func TestFileFindBlocksByType(t *testing.T) {
cases := []struct {
desc string
src string
blockType string
schemaType string
want string
ok bool
}{
{
desc: "resource",
src: `
resource "foo_test" "example1" {}
resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
data "foo_test" "example1" {}
`,
blockType: "resource",
schemaType: "",
want: `
resource "foo_test" "example1" {}
resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
`,
ok: true,
},
{
desc: "resource with schemaType",
src: `
resource "foo_test" "example1" {}
resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
data "foo_test" "example1" {}
`,
blockType: "resource",
schemaType: "foo_test",
want: `
resource "foo_test" "example1" {}
Expand All @@ -35,57 +147,60 @@ resource "foo_test" "example2" {}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
f := parseTestFile(t, tc.src)
resources := f.FindResourcesByType(tc.resourceType)
blocks := f.FindBlocksByType(tc.blockType, tc.schemaType)

newFile := NewEmptyFile()
for _, r := range resources {
for _, r := range blocks {
newFile.AppendBlock(r)
}

got := printTestFile(t, newFile)
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Fatalf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff)
t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff)
}
})
}
}

func TestFileFindProvidersByType(t *testing.T) {
func TestFileFindResourcesByType(t *testing.T) {
cases := []struct {
desc string
src string
providerType string
want string
ok bool
desc string
src string
schemaType string
want string
ok bool
}{
{
desc: "simple",
desc: "resource",
src: `
provider "aws" {
region = "ap-northeast-1"
alias = "ap_northeast_1"
}
provider "aws" {
region = "us-east-1"
alias = "us_east_1"
}
resource "foo_test" "example1" {}
resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
data "foo_test" "example1" {}
`,
schemaType: "",
want: `
resource "foo_test" "example1" {}
provider "google" {}
resource "foo_test" "example2" {}
resource "aws_s3_bucket" "example" {}
resource "foo_bar" "example1" {}
`,
ok: true,
},
{
desc: "resource with schemaType",
src: `
resource "foo_test" "example1" {}
resource "foo_test" "example2" {}
resource "foo_bar" "example1" {}
data "foo_test" "example1" {}
`,
providerType: "aws",
schemaType: "foo_test",
want: `
provider "aws" {
region = "ap-northeast-1"
alias = "ap_northeast_1"
}
resource "foo_test" "example1" {}
provider "aws" {
region = "us-east-1"
alias = "us_east_1"
}
resource "foo_test" "example2" {}
`,
ok: true,
},
Expand All @@ -94,16 +209,16 @@ provider "aws" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
f := parseTestFile(t, tc.src)
providers := f.FindProvidersByType(tc.providerType)
blocks := f.FindResourcesByType(tc.schemaType)

newFile := NewEmptyFile()
for _, p := range providers {
newFile.AppendBlock(p)
for _, r := range blocks {
newFile.AppendBlock(r)
}

got := printTestFile(t, newFile)
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Fatalf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff)
t.Errorf("got:\n%s\nwant:\n%s\ndiff:\n%s", got, tc.want, diff)
}
})
}
Expand Down

0 comments on commit 3de0df3

Please sign in to comment.