Skip to content

Commit

Permalink
add handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 16, 2021
1 parent 05db832 commit 07aa066
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 19 deletions.
118 changes: 100 additions & 18 deletions internal/langserver/handlers/go_to_ref_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package handlers

import (
"fmt"
"path/filepath"
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/stretchr/testify/mock"
)

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
Expand Down Expand Up @@ -46,13 +48,13 @@ func TestDefinition(t *testing.T) {
ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
Expand All @@ -69,7 +71,7 @@ func TestDefinition(t *testing.T) {
}
output "foo" {
value = var.test
value = var.test
}`)+`,
"uri": "%s/main.tf"
}
Expand Down Expand Up @@ -103,7 +105,87 @@ output "foo" {
}`, tmpDir.URI()))
}

func TestDeclaration(t *testing.T) {
func TestDefinition_moduleInputToVariable(t *testing.T) {
modPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
if err != nil {
t.Fatal(err)
}
modUri := lsp.FileHandlerFromDirPath(modPath)

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
modPath: validTfMockCalls(),
},
},
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
}`, modUri.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": `+fmt.Sprintf("%q",
`module "gorilla-app" {
source = "./application"
environment_name = "prod"
app_prefix = "protect-gorillas"
instances = 5
}
`)+`,
"uri": "%s/main.tf"
}
}`, modUri.URI())})
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/definition",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
},
"position": {
"line": 2,
"character": 6
}
}`, modUri.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": [
{
"uri": "%s/application/main.tf",
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 2,
"character": 1
}
}
}
]
}`, modUri.URI()))
}

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
Expand Down Expand Up @@ -139,13 +221,13 @@ func TestDeclaration(t *testing.T) {
ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
Expand All @@ -162,7 +244,7 @@ func TestDeclaration(t *testing.T) {
}
output "foo" {
value = var.test
value = var.test
}`)+`,
"uri": "%s/main.tf"
}
Expand Down
93 changes: 92 additions & 1 deletion internal/langserver/handlers/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ package handlers

import (
"fmt"
"path/filepath"
"testing"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/stretchr/testify/mock"
)

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

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
Expand Down Expand Up @@ -117,3 +119,92 @@ output "foo" {
]
}`, tmpDir.URI(), tmpDir.URI()))
}

func TestReferences_variableToModuleInput(t *testing.T) {
rootModPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
if err != nil {
t.Fatal(err)
}

submodPath := filepath.Join(rootModPath, "application")

rootModUri := lsp.FileHandlerFromDirPath(rootModPath)
submodUri := lsp.FileHandlerFromDirPath(submodPath)

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
submodPath: validTfMockCalls(),
},
},
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {
"definition": {
"linkSupport": true
}
},
"rootUri": %q,
"processId": 12345
}`, rootModUri.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})
ls.Call(t, &langserver.CallRequest{
Method: "textDocument/didOpen",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"version": 0,
"languageId": "terraform",
"text": `+fmt.Sprintf("%q",
`variable "environment_name" {
type = string
}
variable "app_prefix" {
type = string
}
variable "instances" {
type = number
}
`)+`,
"uri": "%s/main.tf"
}
}`, submodUri.URI())})
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/references",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/main.tf"
},
"position": {
"line": 0,
"character": 5
}
}`, submodUri.URI())}, fmt.Sprintf(`{
"jsonrpc": "2.0",
"id": 3,
"result": [
{
"uri": "%s/main.tf",
"range": {
"start": {
"line": 2,
"character": 2
},
"end": {
"line": 2,
"character": 18
}
}
}
]
}`, rootModUri.URI()))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"gorilla-app","Source":"./application","Dir":"application"}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
variable "environment_name" {
type = string
}

variable "app_prefix" {
type = string
}

variable "instances" {
type = number
}

resource "random_pet" "application" {
count = var.instances
keepers = {
unique = "${var.environment_name}-${var.app_prefix}"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "gorilla-app" {
source = "./application"
environment_name = "prod"
app_prefix = "protect-gorillas"
instances = 5
}

0 comments on commit 07aa066

Please sign in to comment.