Skip to content

Commit

Permalink
langserver/handlers: update tests to introduce synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Apr 5, 2022
1 parent a1a9bbe commit a30349f
Show file tree
Hide file tree
Showing 26 changed files with 495 additions and 87 deletions.
24 changes: 22 additions & 2 deletions internal/cmd/inspect_module_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/hashicorp/go-multierror"
ictx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/logging"
"github.com/hashicorp/terraform-ls/internal/state"
Expand Down Expand Up @@ -92,16 +93,35 @@ func (c *InspectModuleCommand) inspect(rootPath string) error {

fs := filesystem.NewFilesystem(ss.DocumentStore)

dir := document.DirHandleFromPath(rootPath)

ctx := context.Background()

walker := module.SyncWalker(fs, ss.DocumentStore, ss.Modules, ss.ProviderSchemas, ss.JobStore, exec.NewExecutor)
pa := state.NewPathAwaiter(ss.WalkerPaths, false)
walker := module.NewWalker(fs, pa, ss.Modules, ss.ProviderSchemas, ss.JobStore, exec.NewExecutor)
walker.Collector = module.NewWalkerCollector()
walker.SetLogger(c.logger)
err = ss.WalkerPaths.WaitForDirs(ctx, []document.DirHandle{dir})
if err != nil {
return err
}
err = ss.JobStore.WaitForJobs(ctx, walker.Collector.JobIds()...)
if err != nil {
return err
}
err = walker.Collector.ErrorOrNil()
if err != nil {
return err
}

ctx, cancel := ictx.WithSignalCancel(context.Background(),
c.logger, os.Interrupt, syscall.SIGTERM)
defer cancel()

walker.EnqueuePath(rootPath)
err = ss.WalkerPaths.EnqueueDir(dir)
if err != nil {
return err
}
err = walker.StartWalking(ctx)
if err != nil {
return err
Expand Down
13 changes: 0 additions & 13 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var (
ctxTfExecLogPath = &contextKey{"terraform executor log path"}
ctxTfExecTimeout = &contextKey{"terraform execution timeout"}
ctxWatcher = &contextKey{"watcher"}
ctxModuleWalker = &contextKey{"module walker"}
ctxRootDir = &contextKey{"root directory"}
ctxCommandPrefix = &contextKey{"command prefix"}
ctxDiagsNotifier = &contextKey{"diagnostics notifier"}
Expand Down Expand Up @@ -119,18 +118,6 @@ func CommandPrefix(ctx context.Context) (string, bool) {
return *commandPrefix, true
}

func WithModuleWalker(ctx context.Context, w *module.Walker) context.Context {
return context.WithValue(ctx, ctxModuleWalker, w)
}

func ModuleWalker(ctx context.Context) (*module.Walker, error) {
w, ok := ctx.Value(ctxModuleWalker).(*module.Walker)
if !ok {
return nil, missingContextErr(ctxModuleWalker)
}
return w, nil
}

func WithDiagnosticsNotifier(ctx context.Context, diags *diagnostics.Notifier) context.Context {
return context.WithValue(ctx, ctxDiagsNotifier, diags)
}
Expand Down
11 changes: 11 additions & 0 deletions internal/langserver/handlers/cancel_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import (
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
)

func TestLangServer_cancelRequest(t *testing.T) {
tmpDir := TempDir(t)

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
AdditionalHandlers: map[string]handler.Func{
"$/sleepTicker": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
Expand Down Expand Up @@ -43,6 +51,8 @@ func TestLangServer_cancelRequest(t *testing.T) {
return nil, ctx.Err()
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -54,6 +64,7 @@ func TestLangServer_cancelRequest(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
20 changes: 20 additions & 0 deletions internal/langserver/handlers/code_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/stretchr/testify/mock"
)

Expand All @@ -31,6 +33,12 @@ func TestLangServer_codeActionWithoutInitialization(t *testing.T) {
func TestLangServer_codeAction_basic(t *testing.T) {
tmpDir := TempDir(t)

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -68,6 +76,8 @@ func TestLangServer_codeAction_basic(t *testing.T) {
}},
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -79,6 +89,7 @@ func TestLangServer_codeAction_basic(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down Expand Up @@ -269,6 +280,12 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -306,6 +323,8 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {
}},
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -317,6 +336,7 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {
"rootUri": %q,
"processId": 123456
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
25 changes: 23 additions & 2 deletions internal/langserver/handlers/code_lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
"github.com/stretchr/testify/mock"
)

Expand Down Expand Up @@ -88,6 +90,12 @@ func TestCodeLens_referenceCount(t *testing.T) {
t.Fatal(err)
}

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
Expand Down Expand Up @@ -124,7 +132,10 @@ func TestCodeLens_referenceCount(t *testing.T) {
},
},
},
}}))
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

Expand All @@ -139,6 +150,7 @@ func TestCodeLens_referenceCount(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down Expand Up @@ -215,13 +227,21 @@ func TestCodeLens_referenceCount_crossModule(t *testing.T) {
t.Fatal(err)
}

ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := module.NewWalkerCollector()
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
submodPath: validTfMockCalls(),
rootModPath: validTfMockCalls(),
},
}}))
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

Expand All @@ -236,6 +256,7 @@ func TestCodeLens_referenceCount_crossModule(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, rootModUri.URI)})
waitForWalkerPath(t, ss, wc, rootModUri)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand Down
Loading

0 comments on commit a30349f

Please sign in to comment.