Skip to content

Commit

Permalink
fix: Added RaiseErr field in tests to allow checking for errors
Browse files Browse the repository at this point in the history
- Added the RaiseErr field in the test table struct, this is due to
  having the ability of checking errors apart of helper issues, also
  checked if the provider existed using the mentioned mechanism
- Erased unecessary error checking when decoding provider config
  • Loading branch information
JorgeReus committed Jun 12, 2023
1 parent 876038f commit 467e17e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
)


require (
github.com/stretchr/testify v1.7.2
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
golang.org/x/net v0.10.0
)


require (
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand All @@ -46,10 +46,12 @@ require (
github.com/kr/pretty v0.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/tools v0.8.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
8 changes: 2 additions & 6 deletions rules/aws_resource_missing_tags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rules

import (
"errors"
"fmt"
"sort"
"strings"
Expand Down Expand Up @@ -174,18 +175,13 @@ func (r *AwsResourceMissingTagsRule) Check(runner tflint.Runner) error {
providerAlias,
)
logger.Error("Error querying provider tags: %s", errString)
runner.EmitIssue(r, errString, *provider.AliasRange)
return nil
return errors.New(errString)
}

if diagnostics.HasErrors() {
logger.Error("error decoding provider: %w", diagnostics)
return diagnostics
}

if err != nil {
return err
}
}

resourceTags := make(map[string]string)
Expand Down
23 changes: 11 additions & 12 deletions rules/aws_resource_missing_tags_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package rules

import (
"errors"
"testing"

hcl "github.com/hashicorp/hcl/v2"
"github.com/stretchr/testify/assert"
"github.com/terraform-linters/tflint-plugin-sdk/helper"
)

Expand All @@ -13,6 +15,7 @@ func Test_AwsResourceMissingTags(t *testing.T) {
Content string
Config string
Expected helper.Issues
RaiseErr error
}{
{
Name: "Wanted tags: Bar,Foo, found: bar,foo",
Expand Down Expand Up @@ -401,16 +404,8 @@ rule "aws_resource_missing_tags" {
tags = ["Foo"]
}`,
Expected: helper.Issues{
{
Rule: NewAwsResourceMissingTagsRule(),
Message: "The aws provider with alias \"west\" doesn't exist.",
Range: hcl.Range{
Filename: "module.tf",
Start: hcl.Pos{Line: 11, Column: 17},
End: hcl.Pos{Line: 11, Column: 22},
},
},
},
RaiseErr: errors.New("The aws provider with alias \"west\" doesn't exist."),
},
}

Expand All @@ -419,9 +414,13 @@ rule "aws_resource_missing_tags" {
for _, tc := range cases {
runner := helper.TestRunner(t, map[string]string{"module.tf": tc.Content, ".tflint.hcl": tc.Config})

if err := rule.Check(runner); err != nil {
t.Fatalf("Unexpected error occurred: %s", err)
}
err := rule.Check(runner)

if tc.RaiseErr == nil && err != nil {
t.Fatalf("Unexpected error occurred in test \"%s\": %s", tc.Name, err)
}

assert.Equal(t, tc.RaiseErr, err)

helper.AssertIssues(t, tc.Expected, runner.Issues)
}
Expand Down

0 comments on commit 467e17e

Please sign in to comment.