Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper: Compare Rule types with custom comparer #23

Merged
merged 1 commit into from
May 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions helper/testing.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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...))
Expand All @@ -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)
})
}