Skip to content

Commit f6b6033

Browse files
committedNov 18, 2021
handlers: add tests for definition + references
1 parent 5cb7717 commit f6b6033

File tree

5 files changed

+217
-19
lines changed

5 files changed

+217
-19
lines changed
 

Diff for: ‎internal/langserver/handlers/go_to_ref_target_test.go

+100-18
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package handlers
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"testing"
67

78
"github.com/hashicorp/go-version"
89
"github.com/hashicorp/terraform-ls/internal/langserver"
10+
"github.com/hashicorp/terraform-ls/internal/lsp"
911
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
1012
"github.com/stretchr/testify/mock"
1113
)
1214

13-
func TestDefinition(t *testing.T) {
15+
func TestDefinition_basic(t *testing.T) {
1416
tmpDir := TempDir(t)
1517

1618
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
@@ -46,13 +48,13 @@ func TestDefinition(t *testing.T) {
4648
ls.Call(t, &langserver.CallRequest{
4749
Method: "initialize",
4850
ReqParams: fmt.Sprintf(`{
49-
"capabilities": {
50-
"definition": {
51-
"linkSupport": true
52-
}
53-
},
54-
"rootUri": %q,
55-
"processId": 12345
51+
"capabilities": {
52+
"definition": {
53+
"linkSupport": true
54+
}
55+
},
56+
"rootUri": %q,
57+
"processId": 12345
5658
}`, tmpDir.URI())})
5759
ls.Notify(t, &langserver.CallRequest{
5860
Method: "initialized",
@@ -69,7 +71,7 @@ func TestDefinition(t *testing.T) {
6971
}
7072
7173
output "foo" {
72-
value = var.test
74+
value = var.test
7375
}`)+`,
7476
"uri": "%s/main.tf"
7577
}
@@ -103,7 +105,87 @@ output "foo" {
103105
}`, tmpDir.URI()))
104106
}
105107

106-
func TestDeclaration(t *testing.T) {
108+
func TestDefinition_moduleInputToVariable(t *testing.T) {
109+
modPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
110+
if err != nil {
111+
t.Fatal(err)
112+
}
113+
modUri := lsp.FileHandlerFromDirPath(modPath)
114+
115+
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
116+
TerraformCalls: &exec.TerraformMockCalls{
117+
PerWorkDir: map[string][]*mock.Call{
118+
modPath: validTfMockCalls(),
119+
},
120+
},
121+
}))
122+
stop := ls.Start(t)
123+
defer stop()
124+
125+
ls.Call(t, &langserver.CallRequest{
126+
Method: "initialize",
127+
ReqParams: fmt.Sprintf(`{
128+
"capabilities": {
129+
"definition": {
130+
"linkSupport": true
131+
}
132+
},
133+
"rootUri": %q,
134+
"processId": 12345
135+
}`, modUri.URI())})
136+
ls.Notify(t, &langserver.CallRequest{
137+
Method: "initialized",
138+
ReqParams: "{}",
139+
})
140+
ls.Call(t, &langserver.CallRequest{
141+
Method: "textDocument/didOpen",
142+
ReqParams: fmt.Sprintf(`{
143+
"textDocument": {
144+
"version": 0,
145+
"languageId": "terraform",
146+
"text": `+fmt.Sprintf("%q",
147+
`module "gorilla-app" {
148+
source = "./application"
149+
environment_name = "prod"
150+
app_prefix = "protect-gorillas"
151+
instances = 5
152+
}
153+
`)+`,
154+
"uri": "%s/main.tf"
155+
}
156+
}`, modUri.URI())})
157+
ls.CallAndExpectResponse(t, &langserver.CallRequest{
158+
Method: "textDocument/definition",
159+
ReqParams: fmt.Sprintf(`{
160+
"textDocument": {
161+
"uri": "%s/main.tf"
162+
},
163+
"position": {
164+
"line": 2,
165+
"character": 6
166+
}
167+
}`, modUri.URI())}, fmt.Sprintf(`{
168+
"jsonrpc": "2.0",
169+
"id": 3,
170+
"result": [
171+
{
172+
"uri": "%s/application/main.tf",
173+
"range": {
174+
"start": {
175+
"line": 0,
176+
"character": 0
177+
},
178+
"end": {
179+
"line": 2,
180+
"character": 1
181+
}
182+
}
183+
}
184+
]
185+
}`, modUri.URI()))
186+
}
187+
188+
func TestDeclaration_basic(t *testing.T) {
107189
tmpDir := TempDir(t)
108190

109191
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
@@ -139,13 +221,13 @@ func TestDeclaration(t *testing.T) {
139221
ls.Call(t, &langserver.CallRequest{
140222
Method: "initialize",
141223
ReqParams: fmt.Sprintf(`{
142-
"capabilities": {
143-
"definition": {
144-
"linkSupport": true
145-
}
146-
},
147-
"rootUri": %q,
148-
"processId": 12345
224+
"capabilities": {
225+
"definition": {
226+
"linkSupport": true
227+
}
228+
},
229+
"rootUri": %q,
230+
"processId": 12345
149231
}`, tmpDir.URI())})
150232
ls.Notify(t, &langserver.CallRequest{
151233
Method: "initialized",
@@ -162,7 +244,7 @@ func TestDeclaration(t *testing.T) {
162244
}
163245
164246
output "foo" {
165-
value = var.test
247+
value = var.test
166248
}`)+`,
167249
"uri": "%s/main.tf"
168250
}

Diff for: ‎internal/langserver/handlers/references_test.go

+92-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ package handlers
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"testing"
67

78
"github.com/hashicorp/go-version"
89
"github.com/hashicorp/terraform-ls/internal/langserver"
10+
"github.com/hashicorp/terraform-ls/internal/lsp"
911
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
1012
"github.com/stretchr/testify/mock"
1113
)
1214

13-
func TestReferences(t *testing.T) {
15+
func TestReferences_basic(t *testing.T) {
1416
tmpDir := TempDir(t)
1517

1618
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
@@ -117,3 +119,92 @@ output "foo" {
117119
]
118120
}`, tmpDir.URI(), tmpDir.URI()))
119121
}
122+
123+
func TestReferences_variableToModuleInput(t *testing.T) {
124+
rootModPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
125+
if err != nil {
126+
t.Fatal(err)
127+
}
128+
129+
submodPath := filepath.Join(rootModPath, "application")
130+
131+
rootModUri := lsp.FileHandlerFromDirPath(rootModPath)
132+
submodUri := lsp.FileHandlerFromDirPath(submodPath)
133+
134+
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
135+
TerraformCalls: &exec.TerraformMockCalls{
136+
PerWorkDir: map[string][]*mock.Call{
137+
submodPath: validTfMockCalls(),
138+
},
139+
},
140+
}))
141+
stop := ls.Start(t)
142+
defer stop()
143+
144+
ls.Call(t, &langserver.CallRequest{
145+
Method: "initialize",
146+
ReqParams: fmt.Sprintf(`{
147+
"capabilities": {
148+
"definition": {
149+
"linkSupport": true
150+
}
151+
},
152+
"rootUri": %q,
153+
"processId": 12345
154+
}`, rootModUri.URI())})
155+
ls.Notify(t, &langserver.CallRequest{
156+
Method: "initialized",
157+
ReqParams: "{}",
158+
})
159+
ls.Call(t, &langserver.CallRequest{
160+
Method: "textDocument/didOpen",
161+
ReqParams: fmt.Sprintf(`{
162+
"textDocument": {
163+
"version": 0,
164+
"languageId": "terraform",
165+
"text": `+fmt.Sprintf("%q",
166+
`variable "environment_name" {
167+
type = string
168+
}
169+
170+
variable "app_prefix" {
171+
type = string
172+
}
173+
174+
variable "instances" {
175+
type = number
176+
}
177+
`)+`,
178+
"uri": "%s/main.tf"
179+
}
180+
}`, submodUri.URI())})
181+
ls.CallAndExpectResponse(t, &langserver.CallRequest{
182+
Method: "textDocument/references",
183+
ReqParams: fmt.Sprintf(`{
184+
"textDocument": {
185+
"uri": "%s/main.tf"
186+
},
187+
"position": {
188+
"line": 0,
189+
"character": 5
190+
}
191+
}`, submodUri.URI())}, fmt.Sprintf(`{
192+
"jsonrpc": "2.0",
193+
"id": 3,
194+
"result": [
195+
{
196+
"uri": "%s/main.tf",
197+
"range": {
198+
"start": {
199+
"line": 2,
200+
"character": 2
201+
},
202+
"end": {
203+
"line": 2,
204+
"character": 18
205+
}
206+
}
207+
}
208+
]
209+
}`, rootModUri.URI()))
210+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"gorilla-app","Source":"./application","Dir":"application"}]}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "environment_name" {
2+
type = string
3+
}
4+
5+
variable "app_prefix" {
6+
type = string
7+
}
8+
9+
variable "instances" {
10+
type = number
11+
}
12+
13+
resource "random_pet" "application" {
14+
count = var.instances
15+
keepers = {
16+
unique = "${var.environment_name}-${var.app_prefix}"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module "gorilla-app" {
2+
source = "./application"
3+
environment_name = "prod"
4+
app_prefix = "protect-gorillas"
5+
instances = 5
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.