From 86a1f22d5110b4ec6959e99c83664be55ae37add Mon Sep 17 00:00:00 2001 From: wata_mac Date: Sun, 24 May 2020 15:10:38 +0900 Subject: [PATCH] helper: Compare Rule types with custom comparer --- helper/testing.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/helper/testing.go b/helper/testing.go index 7f5fd1e..0c30084 100644 --- a/helper/testing.go +++ b/helper/testing.go @@ -1,12 +1,14 @@ package helper import ( + "reflect" "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclparse" + "github.com/terraform-linters/tflint-plugin-sdk/tflint" ) // TestRunner returns a pseudo Runner for testing @@ -30,7 +32,7 @@ func AssertIssues(t *testing.T, expected Issues, actual Issues) { opts := []cmp.Option{ // Byte field will be ignored because it's not important in tests such as positions cmpopts.IgnoreFields(hcl.Pos{}, "Byte"), - cmpopts.IgnoreFields(Issue{}, "Rule"), + ruleComparer(), } if !cmp.Equal(expected, actual, opts...) { t.Fatalf("Expected issues are not matched:\n %s\n", cmp.Diff(expected, actual, opts...)) @@ -41,9 +43,17 @@ func AssertIssues(t *testing.T, expected Issues, actual Issues) { func AssertIssuesWithoutRange(t *testing.T, expected Issues, actual Issues) { opts := []cmp.Option{ cmpopts.IgnoreFields(Issue{}, "Range"), - cmpopts.IgnoreFields(Issue{}, "Rule"), + ruleComparer(), } if !cmp.Equal(expected, actual, opts...) { t.Fatalf("Expected issues are not matched:\n %s\n", cmp.Diff(expected, actual, opts...)) } } + +// ruleComparer returns a Comparer func that checks that two rule interfaces +// have the same underlying type. It does not compare struct fields. +func ruleComparer() cmp.Option { + return cmp.Comparer(func(x, y tflint.Rule) bool { + return reflect.TypeOf(x) == reflect.TypeOf(y) + }) +}