From 3ef9e45b360f48ba113b85b38bee0b1eeb85d729 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 27 Apr 2020 10:20:35 +0100 Subject: [PATCH] terraform/discovery: Mock LookPath calls --- commands/completion_command.go | 3 ++- internal/terraform/discovery/discovery.go | 6 +++++- internal/terraform/discovery/discovery_mock.go | 9 +++++++++ langserver/handlers/service.go | 7 +++++-- langserver/handlers/service_mock.go | 5 ++++- 5 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 internal/terraform/discovery/discovery_mock.go diff --git a/commands/completion_command.go b/commands/completion_command.go index 447b323b0..e195b3266 100644 --- a/commands/completion_command.go +++ b/commands/completion_command.go @@ -84,7 +84,8 @@ func (c *CompletionCommand) Run(args []string) int { Version: 0, })) - tfPath, err := discovery.LookPath() + d := &discovery.Discovery{} + tfPath, err := d.LookPath() if err != nil { c.Ui.Error(err.Error()) return 1 diff --git a/internal/terraform/discovery/discovery.go b/internal/terraform/discovery/discovery.go index d7de72211..06f58a1de 100644 --- a/internal/terraform/discovery/discovery.go +++ b/internal/terraform/discovery/discovery.go @@ -5,7 +5,11 @@ import ( "os/exec" ) -func LookPath() (string, error) { +type DiscoveryFunc func() (string, error) + +type Discovery struct{} + +func (d *Discovery) LookPath() (string, error) { path, err := exec.LookPath("terraform") if err != nil { return "", fmt.Errorf("unable to find terraform: %s", err) diff --git a/internal/terraform/discovery/discovery_mock.go b/internal/terraform/discovery/discovery_mock.go new file mode 100644 index 000000000..de3700b19 --- /dev/null +++ b/internal/terraform/discovery/discovery_mock.go @@ -0,0 +1,9 @@ +package discovery + +type MockDiscovery struct { + Path string +} + +func (d *MockDiscovery) LookPath() (string, error) { + return d.Path, nil +} diff --git a/langserver/handlers/service.go b/langserver/handlers/service.go index 91d76a29a..bb7939ada 100644 --- a/langserver/handlers/service.go +++ b/langserver/handlers/service.go @@ -28,6 +28,7 @@ type service struct { sessCtx context.Context stopSession context.CancelFunc + tfPath discovery.DiscoveryFunc ss *schema.Storage executorFunc func(ctx context.Context, execPath string) *exec.Executor } @@ -36,12 +37,14 @@ var discardLogs = log.New(ioutil.Discard, "", 0) func NewSession(srvCtx context.Context) session.Session { sessCtx, stopSession := context.WithCancel(srvCtx) + d := &discovery.Discovery{} return &service{ logger: discardLogs, srvCtx: srvCtx, sessCtx: sessCtx, stopSession: stopSession, executorFunc: exec.NewExecutor, + tfPath: d.LookPath, ss: schema.NewStorage(), } } @@ -78,7 +81,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) { ctx = lsctx.WithFilesystem(fs, ctx) ctx = lsctx.WithClientCapabilitiesSetter(cc, ctx) - tfPath, err := discovery.LookPath() + tfPath, err := svc.tfPath() if err != nil { return nil, err } @@ -154,7 +157,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) { ctx = lsctx.WithFilesystem(fs, ctx) - tfPath, err := discovery.LookPath() + tfPath, err := svc.tfPath() if err != nil { return nil, err } diff --git a/langserver/handlers/service_mock.go b/langserver/handlers/service_mock.go index dba1769d6..fab706e81 100644 --- a/langserver/handlers/service_mock.go +++ b/langserver/handlers/service_mock.go @@ -3,6 +3,7 @@ package handlers import ( "context" + "github.com/hashicorp/terraform-ls/internal/terraform/discovery" "github.com/hashicorp/terraform-ls/internal/terraform/exec" "github.com/hashicorp/terraform-ls/internal/terraform/schema" "github.com/hashicorp/terraform-ls/langserver/session" @@ -18,6 +19,7 @@ type mockSession struct { func (ms *mockSession) new(srvCtx context.Context) session.Session { sessCtx, stopSession := context.WithCancel(srvCtx) ms.stopFunc = stopSession + d := discovery.MockDiscovery{Path: "mock-tf"} svc := &service{ logger: discardLogs, @@ -27,7 +29,8 @@ func (ms *mockSession) new(srvCtx context.Context) session.Session { executorFunc: func(context.Context, string) *exec.Executor { return exec.MockExecutor(ms.mid) }, - ss: schema.MockStorage(nil), + tfPath: d.LookPath, + ss: schema.MockStorage(nil), } return svc