From 408ebe1d443e85cb1b78d05a55a73838ed6a745e Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Tue, 13 Dec 2022 13:27:45 +0900 Subject: [PATCH] Add test helpers for building block filters --- .../aws_s3_bucket_acceleration_status_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_acl_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_grant_test.go | 9 +-------- .../awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_logging_test.go | 9 +-------- .../aws_s3_bucket_object_lock_configuration_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_policy_test.go | 9 +-------- .../aws_s3_bucket_replication_configuration_test.go | 9 +-------- .../awsv4upgrade/aws_s3_bucket_request_payer_test.go | 9 +-------- ...ucket_server_side_encryption_configuration_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_test.go | 11 +++++++++++ filter/awsv4upgrade/aws_s3_bucket_versioning_test.go | 9 +-------- filter/awsv4upgrade/aws_s3_bucket_website_test.go | 9 +-------- .../provider_aws_s3_force_path_style_test.go | 9 +-------- filter/awsv4upgrade/provider_aws_test.go | 11 +++++++++++ 16 files changed, 36 insertions(+), 112 deletions(-) diff --git a/filter/awsv4upgrade/aws_s3_bucket_acceleration_status_test.go b/filter/awsv4upgrade/aws_s3_bucket_acceleration_status_test.go index 6b46ca5..d21313f 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_acceleration_status_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_acceleration_status_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_acl_test.go b/filter/awsv4upgrade/aws_s3_bucket_acl_test.go index c2cddc2..bcf0f30 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_acl_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_acl_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go b/filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go index 59aaceb..5a4d1e7 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_cors_rule_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_grant_test.go b/filter/awsv4upgrade/aws_s3_bucket_grant_test.go index 3cce6d8..b765366 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_grant_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_grant_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go b/filter/awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go index ff4f490..eb421d1 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_lifecycle_rule_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_logging_test.go b/filter/awsv4upgrade/aws_s3_bucket_logging_test.go index 73e0cbc..050c4b4 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_logging_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_logging_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_object_lock_configuration_test.go b/filter/awsv4upgrade/aws_s3_bucket_object_lock_configuration_test.go index de5df6f..453a335 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_object_lock_configuration_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_object_lock_configuration_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_policy_test.go b/filter/awsv4upgrade/aws_s3_bucket_policy_test.go index 0e5c784..9e21459 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_policy_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_policy_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_replication_configuration_test.go b/filter/awsv4upgrade/aws_s3_bucket_replication_configuration_test.go index b53cc83..eca2f79 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_replication_configuration_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_replication_configuration_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_request_payer_test.go b/filter/awsv4upgrade/aws_s3_bucket_request_payer_test.go index 192e396..85774e9 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_request_payer_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_request_payer_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_server_side_encryption_configuration_test.go b/filter/awsv4upgrade/aws_s3_bucket_server_side_encryption_configuration_test.go index c42a890..3a44c1f 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_server_side_encryption_configuration_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_server_side_encryption_configuration_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_test.go b/filter/awsv4upgrade/aws_s3_bucket_test.go index 63fd628..cce03fa 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_test.go @@ -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 diff --git a/filter/awsv4upgrade/aws_s3_bucket_versioning_test.go b/filter/awsv4upgrade/aws_s3_bucket_versioning_test.go index cad1f01..317438c 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_versioning_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_versioning_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/aws_s3_bucket_website_test.go b/filter/awsv4upgrade/aws_s3_bucket_website_test.go index 2112016..c378d95 100644 --- a/filter/awsv4upgrade/aws_s3_bucket_website_test.go +++ b/filter/awsv4upgrade/aws_s3_bucket_website_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/provider_aws_s3_force_path_style_test.go b/filter/awsv4upgrade/provider_aws_s3_force_path_style_test.go index 110fc39..9bbaef4 100644 --- a/filter/awsv4upgrade/provider_aws_s3_force_path_style_test.go +++ b/filter/awsv4upgrade/provider_aws_s3_force_path_style_test.go @@ -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) { @@ -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 { diff --git a/filter/awsv4upgrade/provider_aws_test.go b/filter/awsv4upgrade/provider_aws_test.go index b63b126..4ab0d7b 100644 --- a/filter/awsv4upgrade/provider_aws_test.go +++ b/filter/awsv4upgrade/provider_aws_test.go @@ -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