Skip to content

Commit

Permalink
tests: Wait for all jobs from didOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Aug 8, 2022
1 parent ee7b70d commit 7024141
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 14 deletions.
3 changes: 0 additions & 3 deletions internal/indexer/document_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func (idx *Indexer) DocumentOpened(modHandle document.DirHandle) (job.IDs, error
// Given that getting version may take time and we only use it to
// enhance the UX, we ignore the outcome (job ID) here
// to avoid delays when documents of new modules are open.

// TODO: This affects some completion data, so we'll need to either
// update the tests or find some way of synchronizing this in tests
}

parseId, err := idx.jobStore.EnqueueJob(job.Job{
Expand Down
3 changes: 3 additions & 0 deletions internal/langserver/handlers/code_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func TestLangServer_codeAction_basic(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/codeAction",
ReqParams: fmt.Sprintf(`{
Expand Down Expand Up @@ -351,6 +353,7 @@ func TestLangServer_codeAction_no_code_action_requested(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, tt.request, tt.want)
})
Expand Down
19 changes: 18 additions & 1 deletion internal/langserver/handlers/code_lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ func TestCodeLens_withoutOptIn(t *testing.T) {
t.Fatal(err)
}

ls := langserver.NewLangServerMock(t, NewMockSession(nil))
ss, err := state.NewStateStore()
if err != nil {
t.Fatal(err)
}
wc := walker.NewWalkerCollector()

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()

Expand All @@ -52,6 +61,8 @@ func TestCodeLens_withoutOptIn(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)

ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand All @@ -66,6 +77,8 @@ func TestCodeLens_withoutOptIn(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/codeLens",
ReqParams: fmt.Sprintf(`{
Expand Down Expand Up @@ -170,6 +183,8 @@ output "test" {
value = var.test
}
`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/codeLens",
ReqParams: fmt.Sprintf(`{
Expand Down Expand Up @@ -282,6 +297,8 @@ variable "instances" {
type = number
}
`, submodUri.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/codeLens",
ReqParams: fmt.Sprintf(`{
Expand Down
7 changes: 7 additions & 0 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestModuleCompletion_withValidData_basic(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
Expand Down Expand Up @@ -355,6 +356,7 @@ func TestModuleCompletion_withValidDataAndSnippets(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
Expand Down Expand Up @@ -669,6 +671,7 @@ func TestVarsCompletion_withValidData(t *testing.T) {
"uri": "%s/terraform.tfvars"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
Expand Down Expand Up @@ -821,6 +824,7 @@ output "test" {
"uri": "%s/main.tf"
}
}`, mainCfg, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
Expand Down Expand Up @@ -1089,6 +1093,7 @@ output "test" {
"uri": "%s/main.tf"
}
}`, mainCfg, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

// first module
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Expand Down Expand Up @@ -1429,6 +1434,8 @@ variable "ccc" {}
"uri": "%s/outputs.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
ReqParams: fmt.Sprintf(`{
Expand Down
2 changes: 2 additions & 0 deletions internal/langserver/handlers/did_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ module "app" {
"text": %q
}
}`, TempDir(t).URI, originalText)})
waitForAllPendingJobs(t, ss)

ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didChange",
ReqParams: fmt.Sprintf(`{
Expand Down
2 changes: 2 additions & 0 deletions internal/langserver/handlers/did_open_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func TestLangServer_didOpenLanguageIdStored(t *testing.T) {
"text": %q
}
}`, TempDir(t).URI, originalText)})
waitForAllPendingJobs(t, ss)

path := filepath.Join(TempDir(t).Path(), "main.tf")
dh := document.HandleFromPath(path)
doc, err := ss.DocumentStore.GetDocument(dh)
Expand Down
1 change: 1 addition & 0 deletions internal/langserver/handlers/document_link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestDocumentLink_withValidData(t *testing.T) {
"uri": "%s/main.tf"
}
}`, tmpDir.URI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/documentLink",
Expand Down
13 changes: 13 additions & 0 deletions internal/langserver/handlers/execute_command_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ func TestLangServer_workspaceExecuteCommand_init_argumentError(t *testing.T) {
tmpDir := TempDir(t)
testFileURI := fmt.Sprintf("%s/main.tf", tmpDir.URI)

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -36,6 +44,8 @@ func TestLangServer_workspaceExecuteCommand_init_argumentError(t *testing.T) {
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)

ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand All @@ -50,6 +60,7 @@ func TestLangServer_workspaceExecuteCommand_init_argumentError(t *testing.T) {
"uri": %q
}
}`, testFileURI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down Expand Up @@ -134,6 +145,7 @@ func TestLangServer_workspaceExecuteCommand_init_basic(t *testing.T) {
"uri": %q
}
}`, testFileURI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down Expand Up @@ -223,6 +235,7 @@ func TestLangServer_workspaceExecuteCommand_init_error(t *testing.T) {
"uri": %q
}
}`, testFileURI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,22 @@ import (
)

func TestLangServer_workspaceExecuteCommand_moduleCallers_argumentError(t *testing.T) {
rootDir := t.TempDir()
rootUri := uri.FromPath(rootDir)
rootDir := document.DirHandleFromPath(t.TempDir())

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
rootDir: validTfMockCalls(),
rootDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -37,7 +44,9 @@ func TestLangServer_workspaceExecuteCommand_moduleCallers_argumentError(t *testi
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, rootUri)})
}`, rootDir.URI)})
waitForWalkerPath(t, ss, wc, rootDir)

ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand All @@ -51,7 +60,8 @@ func TestLangServer_workspaceExecuteCommand_moduleCallers_argumentError(t *testi
"text": "provider \"github\" {}",
"uri": %q
}
}`, fmt.Sprintf("%s/main.tf", rootUri))})
}`, fmt.Sprintf("%s/main.tf", rootDir.URI))})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down Expand Up @@ -109,6 +119,7 @@ func TestLangServer_workspaceExecuteCommand_moduleCallers_basic(t *testing.T) {
"uri": %q
}
}`, fmt.Sprintf("%s/main.tf", baseDirUri))})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ import (
)

func TestLangServer_workspaceExecuteCommand_moduleProviders_argumentError(t *testing.T) {
rootDir := t.TempDir()
rootUri := uri.FromPath(rootDir)
rootDir := document.DirHandleFromPath(t.TempDir())

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
rootDir: validTfMockCalls(),
rootDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -38,7 +45,8 @@ func TestLangServer_workspaceExecuteCommand_moduleProviders_argumentError(t *tes
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, rootUri)})
}`, rootDir.URI)})
waitForWalkerPath(t, ss, wc, rootDir)
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand All @@ -52,7 +60,8 @@ func TestLangServer_workspaceExecuteCommand_moduleProviders_argumentError(t *tes
"text": "provider \"github\" {}",
"uri": %q
}
}`, fmt.Sprintf("%s/main.tf", rootUri))})
}`, fmt.Sprintf("%s/main.tf", rootDir.URI))})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestLangServer_workspaceExecuteCommand_modules_basic(t *testing.T) {
"uri": %q
}
}`, testFileURI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down
13 changes: 13 additions & 0 deletions internal/langserver/handlers/execute_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ import (

"github.com/creachadair/jrpc2/code"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/walker"
"github.com/stretchr/testify/mock"
)

func TestLangServer_workspaceExecuteCommand_noCommandHandlerError(t *testing.T) {
tmpDir := TempDir(t)
testFileURI := fmt.Sprintf("%s/main.tf", tmpDir.URI)

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

InitPluginCache(t, tmpDir.Path())

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
Expand All @@ -22,6 +30,8 @@ func TestLangServer_workspaceExecuteCommand_noCommandHandlerError(t *testing.T)
tmpDir.Path(): validTfMockCalls(),
},
},
StateStore: ss,
WalkerCollector: wc,
}))
stop := ls.Start(t)
defer stop()
Expand All @@ -33,6 +43,8 @@ func TestLangServer_workspaceExecuteCommand_noCommandHandlerError(t *testing.T)
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI)})
waitForWalkerPath(t, ss, wc, tmpDir)

ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
Expand All @@ -47,6 +59,7 @@ func TestLangServer_workspaceExecuteCommand_noCommandHandlerError(t *testing.T)
"uri": %q
}
}`, testFileURI)})
waitForAllPendingJobs(t, ss)

ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "workspace/executeCommand",
Expand Down
Loading

0 comments on commit 7024141

Please sign in to comment.