Skip to content

Commit

Permalink
Merge pull request hashicorp#70 from hashicorp/mock-discovery
Browse files Browse the repository at this point in the history
terraform/discovery: Mock LookPath calls
  • Loading branch information
radeksimko authored Apr 30, 2020
2 parents b2cb055 + 3ef9e45 commit 8a9451b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion commands/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion internal/terraform/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions internal/terraform/discovery/discovery_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package discovery

type MockDiscovery struct {
Path string
}

func (d *MockDiscovery) LookPath() (string, error) {
return d.Path, nil
}
7 changes: 5 additions & 2 deletions langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion langserver/handlers/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 8a9451b

Please sign in to comment.