Skip to content

Commit

Permalink
Merge pull request #23 from terraform-linters/assert_issues_with_cust…
Browse files Browse the repository at this point in the history
…om_comparer

helper: Compare Rule types with custom comparer
  • Loading branch information
wata727 authored May 24, 2020
2 parents 1505d63 + 86a1f22 commit 18bced3
Showing 1 changed file with 12 additions and 2 deletions.
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)
})
}

0 comments on commit 18bced3

Please sign in to comment.