Skip to content

Commit 7d26649

Browse files
authored
Fix NewRunner hook not injecting a custom runner (#252)
1 parent db53187 commit 7d26649

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

plugin/host2plugin/host2plugin_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type mockRuleSetImpl struct {
4141
configSchema func() *hclext.BodySchema
4242
applyGlobalConfig func(*tflint.Config) error
4343
applyConfig func(*hclext.BodyContent) error
44+
newRunner func(tflint.Runner) (tflint.Runner, error)
4445
check func(tflint.Runner) error
4546
}
4647

@@ -79,6 +80,13 @@ func (r *mockRuleSet) ApplyConfig(content *hclext.BodyContent) error {
7980
return nil
8081
}
8182

83+
func (r *mockRuleSet) NewRunner(runner tflint.Runner) (tflint.Runner, error) {
84+
if r.impl.newRunner != nil {
85+
return r.impl.newRunner(runner)
86+
}
87+
return runner, nil
88+
}
89+
8290
func (r *mockRuleSet) Check(runner tflint.Runner) error {
8391
if r.impl.check != nil {
8492
return r.impl.check(runner)
@@ -575,6 +583,14 @@ func (s *mockServer) GetFiles(tflint.ModuleCtxType) map[string][]byte {
575583
return map[string][]byte{}
576584
}
577585

586+
type mockCustomRunner struct {
587+
tflint.Runner
588+
}
589+
590+
func (s *mockCustomRunner) Hello() string {
591+
return "Hello from custom runner!"
592+
}
593+
578594
func TestCheck(t *testing.T) {
579595
// default error check helper
580596
neverHappend := func(err error) bool { return err != nil }
@@ -589,10 +605,11 @@ func TestCheck(t *testing.T) {
589605
}
590606

591607
tests := []struct {
592-
Name string
593-
Arg func() plugin2host.Server
594-
ServerImpl func(tflint.Runner) error
595-
ErrCheck func(error) bool
608+
Name string
609+
Arg func() plugin2host.Server
610+
ServerImpl func(tflint.Runner) error
611+
NewRunnerImpl func(tflint.Runner) (tflint.Runner, error)
612+
ErrCheck func(error) bool
596613
}{
597614
{
598615
Name: "bidirectional",
@@ -667,11 +684,26 @@ resource "aws_instance" "foo" {
667684
return err == nil || err.Error() != "unexpected error"
668685
},
669686
},
687+
{
688+
Name: "inject new runner",
689+
Arg: func() plugin2host.Server {
690+
return &mockServer{}
691+
},
692+
NewRunnerImpl: func(runner tflint.Runner) (tflint.Runner, error) {
693+
return &mockCustomRunner{runner}, nil
694+
},
695+
ServerImpl: func(runner tflint.Runner) error {
696+
return errors.New(runner.(*mockCustomRunner).Hello())
697+
},
698+
ErrCheck: func(err error) bool {
699+
return err == nil || err.Error() != "Hello from custom runner!"
700+
},
701+
},
670702
}
671703

672704
for _, test := range tests {
673705
t.Run(test.Name, func(t *testing.T) {
674-
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl}))
706+
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl, newRunner: test.NewRunnerImpl}))
675707

676708
err := client.Check(test.Arg())
677709
if test.ErrCheck(err) {

plugin/host2plugin/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,11 @@ func (s *GRPCServer) Check(ctx context.Context, req *proto.Check_Request) (*prot
116116
}
117117
defer conn.Close()
118118

119-
err = s.impl.Check(&plugin2host.GRPCClient{Client: proto.NewRunnerClient(conn)})
120-
119+
runner, err := s.impl.NewRunner(&plugin2host.GRPCClient{Client: proto.NewRunnerClient(conn)})
120+
if err != nil {
121+
return nil, toproto.Error(codes.FailedPrecondition, err)
122+
}
123+
err = s.impl.Check(runner)
121124
if err != nil {
122125
return nil, toproto.Error(codes.Aborted, err)
123126
}

tflint/ruleset.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ func (r *BuiltinRuleSet) NewRunner(runner Runner) (Runner, error) {
106106

107107
// Check runs inspection for each rule by applying Runner.
108108
func (r *BuiltinRuleSet) Check(runner Runner) error {
109-
runner, err := r.NewRunner(runner)
110-
if err != nil {
111-
return err
112-
}
113-
114109
for _, rule := range r.EnabledRules {
115110
if err := rule.Check(runner); err != nil {
116111
return fmt.Errorf("Failed to check `%s` rule: %s", rule.Name(), err)

0 commit comments

Comments
 (0)