Skip to content

Commit 86a1f22

Browse files
committed
helper: Compare Rule types with custom comparer
1 parent 1505d63 commit 86a1f22

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

helper/testing.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package helper
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/google/go-cmp/cmp"
78
"github.com/google/go-cmp/cmp/cmpopts"
89
"github.com/hashicorp/hcl/v2"
910
"github.com/hashicorp/hcl/v2/hclparse"
11+
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
1012
)
1113

1214
// TestRunner returns a pseudo Runner for testing
@@ -30,7 +32,7 @@ func AssertIssues(t *testing.T, expected Issues, actual Issues) {
3032
opts := []cmp.Option{
3133
// Byte field will be ignored because it's not important in tests such as positions
3234
cmpopts.IgnoreFields(hcl.Pos{}, "Byte"),
33-
cmpopts.IgnoreFields(Issue{}, "Rule"),
35+
ruleComparer(),
3436
}
3537
if !cmp.Equal(expected, actual, opts...) {
3638
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) {
4143
func AssertIssuesWithoutRange(t *testing.T, expected Issues, actual Issues) {
4244
opts := []cmp.Option{
4345
cmpopts.IgnoreFields(Issue{}, "Range"),
44-
cmpopts.IgnoreFields(Issue{}, "Rule"),
46+
ruleComparer(),
4547
}
4648
if !cmp.Equal(expected, actual, opts...) {
4749
t.Fatalf("Expected issues are not matched:\n %s\n", cmp.Diff(expected, actual, opts...))
4850
}
4951
}
52+
53+
// ruleComparer returns a Comparer func that checks that two rule interfaces
54+
// have the same underlying type. It does not compare struct fields.
55+
func ruleComparer() cmp.Option {
56+
return cmp.Comparer(func(x, y tflint.Rule) bool {
57+
return reflect.TypeOf(x) == reflect.TypeOf(y)
58+
})
59+
}

0 commit comments

Comments
 (0)