Skip to content

Commit

Permalink
Add test helpers for building block filters
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Dec 13, 2022
1 parent b138fe1 commit 408ebe1
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketAccelerationStatusFilter(t *testing.T) {
Expand Down Expand Up @@ -55,13 +54,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketAccelerationStatusResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketAccelerationStatusResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketACLFilter(t *testing.T) {
Expand Down Expand Up @@ -55,13 +54,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketACLResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketACLResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketCorsRuleFilter(t *testing.T) {
Expand Down Expand Up @@ -116,13 +115,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketCorsRuleResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketCorsRuleResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_grant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketGrantFilter(t *testing.T) {
Expand Down Expand Up @@ -108,13 +107,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketGrantResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketGrantResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketLifecycleRuleFilter(t *testing.T) {
Expand Down Expand Up @@ -549,13 +548,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketLifecycleRuleResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketLifecycleRuleResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketLoggingFilter(t *testing.T) {
Expand Down Expand Up @@ -76,13 +75,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketLoggingResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketLoggingResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketObjectLockConfigurationFilter(t *testing.T) {
Expand Down Expand Up @@ -73,13 +72,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketObjectLockConfigurationResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketObjectLockConfigurationResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketPolicyFilter(t *testing.T) {
Expand Down Expand Up @@ -87,13 +86,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketPolicyResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketPolicyResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketReplicationConfigurationFilter(t *testing.T) {
Expand Down Expand Up @@ -131,13 +130,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketReplicationConfigurationResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketReplicationConfigurationResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_request_payer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketRequestPayerFilter(t *testing.T) {
Expand Down Expand Up @@ -55,13 +54,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketRequestPayerResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketRequestPayerResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketServerSideEncryptionConfigurationFilter(t *testing.T) {
Expand Down Expand Up @@ -70,13 +69,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketServerSideEncryptionConfigurationResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketServerSideEncryptionConfigurationResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
11 changes: 11 additions & 0 deletions filter/awsv4upgrade/aws_s3_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"github.com/minamijoyo/tfedit/tfeditor"
)

// buildTestResourceFilter is a helper function which builds an editor filter for testing.
func buildTestResourceFilter(f tfeditor.ResourceFilterFunc) editor.Filter {
return tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(f),
},
},
)
}

func TestAWSS3BucketFilter(t *testing.T) {
cases := []struct {
name string
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_versioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketVersioningFilter(t *testing.T) {
Expand Down Expand Up @@ -174,13 +173,7 @@ resource "aws_s3_bucket_versioning" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketVersioningResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketVersioningResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/aws_s3_bucket_website_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestAWSS3BucketWebsiteFilter(t *testing.T) {
Expand Down Expand Up @@ -68,13 +67,7 @@ resource "aws_s3_bucket" "example" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&AWSS3BucketFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ResourceFilterFunc(AWSS3BucketWebsiteResourceFilter),
},
},
)
filter := buildTestResourceFilter(AWSS3BucketWebsiteResourceFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
9 changes: 1 addition & 8 deletions filter/awsv4upgrade/provider_aws_s3_force_path_style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/hcledit/editor"
"github.com/minamijoyo/tfedit/tfeditor"
)

func TestProviderAWSS3ForcePathStyleFilter(t *testing.T) {
Expand Down Expand Up @@ -47,13 +46,7 @@ provider "aws" {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
filter := tfeditor.NewAllBlocksFilter(
&ProviderAWSFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ProviderFilterFunc(AWSS3ForcePathStyleProviderFilter),
},
},
)
filter := buildTestProviderFilter(AWSS3ForcePathStyleProviderFilter)
o := editor.NewEditOperator(filter)
output, err := o.Apply([]byte(tc.src), "test")
if tc.ok && err != nil {
Expand Down
11 changes: 11 additions & 0 deletions filter/awsv4upgrade/provider_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"github.com/minamijoyo/tfedit/tfeditor"
)

// buildTestProviderFilter is a helper function which builds an editor filter for testing.
func buildTestProviderFilter(f tfeditor.ProviderFilterFunc) editor.Filter {
return tfeditor.NewAllBlocksFilter(
&ProviderAWSFilter{
filters: []tfeditor.BlockFilter{
tfeditor.ProviderFilterFunc(f),
},
},
)
}

func TestProviderAWSFilter(t *testing.T) {
cases := []struct {
name string
Expand Down

0 comments on commit 408ebe1

Please sign in to comment.