Skip to content

Commit

Permalink
Add GetOriginalwd method (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Dec 25, 2022
1 parent 506b8ca commit 1733df8
Show file tree
Hide file tree
Showing 9 changed files with 913 additions and 621 deletions.
5 changes: 5 additions & 0 deletions helper/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ type RuleConfig struct {

var _ tflint.Runner = &Runner{}

// GetOriginalwd always returns the current directory
func (r *Runner) GetOriginalwd() (string, error) {
return os.Getwd()
}

// GetModulePath always returns the root module path address
func (r *Runner) GetModulePath() (addrs.Module, error) {
return []string{}, nil
Expand Down
4 changes: 4 additions & 0 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ type mockServerImpl struct {
getFile func(string) (*hcl.File, error)
}

func (s *mockServer) GetOriginalwd() string {
return "/work"
}

func (s *mockServer) GetModulePath() []string {
return []string{}
}
Expand Down
17 changes: 17 additions & 0 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"os"
"strings"

"github.com/hashicorp/hcl/v2"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/zclconf/go-cty/cty/gocty"
"github.com/zclconf/go-cty/cty/json"
"github.com/zclconf/go-cty/cty/msgpack"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// GRPCClient is a plugin-side implementation. Plugin can send requests through the client to host's gRPC server.
Expand All @@ -28,6 +31,20 @@ type GRPCClient struct {

var _ tflint.Runner = &GRPCClient{}

// GetOriginalwd gets the original working directory.
func (c *GRPCClient) GetOriginalwd() (string, error) {
resp, err := c.Client.GetOriginalwd(context.Background(), &proto.GetOriginalwd_Request{})
if err != nil {
if st, ok := status.FromError(err); ok && st.Code() == codes.Unimplemented {
// Originalwd is available in TFLint v0.44+
// Fallback to os.Getwd() because it equals the current directory in earlier versions.
return os.Getwd()
}
return "", fromproto.Error(err)
}
return resp.Path, err
}

// GetModulePath gets the current module path address.
func (c *GRPCClient) GetModulePath() (addrs.Module, error) {
resp, err := c.Client.GetModulePath(context.Background(), &proto.GetModulePath_Request{})
Expand Down
38 changes: 38 additions & 0 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type mockServer struct {
}

type mockServerImpl struct {
getOriginalwd func() string
getModulePath func() []string
getModuleContent func(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
getFile func(string) (*hcl.File, error)
Expand All @@ -48,6 +49,13 @@ func newMockServer(impl mockServerImpl) *mockServer {
return &mockServer{impl: impl}
}

func (s *mockServer) GetOriginalwd() string {
if s.impl.getOriginalwd != nil {
return s.impl.getOriginalwd()
}
return ""
}

func (s *mockServer) GetModulePath() []string {
if s.impl.getModulePath != nil {
return s.impl.getModulePath()
Expand Down Expand Up @@ -100,6 +108,36 @@ func (s *mockServer) EmitIssue(rule tflint.Rule, message string, location hcl.Ra
// @see https://github.com/google/go-cmp/issues/40
var allowAllUnexported = cmp.Exporter(func(reflect.Type) bool { return true })

func TestGetOriginalwd(t *testing.T) {
tests := []struct {
Name string
ServerImpl func() string
Want string
}{
{
Name: "get the original working directory",
ServerImpl: func() string {
return "/work"
},
Want: "/work",
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCServer(t, newMockServer(mockServerImpl{getOriginalwd: test.ServerImpl}))

got, err := client.GetOriginalwd()
if err != nil {
t.Fatalf("failed to call GetOriginalwd: %s", err)
}
if diff := cmp.Diff(got, test.Want); diff != "" {
t.Errorf("diff: %s", diff)
}
})
}
}

func TestGetModulePath(t *testing.T) {
tests := []struct {
Name string
Expand Down
6 changes: 6 additions & 0 deletions plugin/plugin2host/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var _ proto.RunnerServer = &GRPCServer{}

// Server is the interface that the host should implement when a plugin communicates with the host.
type Server interface {
GetOriginalwd() string
GetModulePath() []string
GetModuleContent(*hclext.BodySchema, tflint.GetModuleContentOption) (*hclext.BodyContent, hcl.Diagnostics)
GetFile(string) (*hcl.File, error)
Expand All @@ -38,6 +39,11 @@ type Server interface {
EmitIssue(rule tflint.Rule, message string, location hcl.Range) error
}

// GetOriginalwd gets the original working directory.
func (s *GRPCServer) GetOriginalwd(ctx context.Context, req *proto.GetOriginalwd_Request) (*proto.GetOriginalwd_Response, error) {
return &proto.GetOriginalwd_Response{Path: s.Impl.GetOriginalwd()}, nil
}

// GetModulePath gets the current module path address.
func (s *GRPCServer) GetModulePath(ctx context.Context, req *proto.GetModulePath_Request) (*proto.GetModulePath_Response, error) {
return &proto.GetModulePath_Response{Path: s.Impl.GetModulePath()}, nil
Expand Down
Loading

0 comments on commit 1733df8

Please sign in to comment.