Skip to content

Commit

Permalink
fix: missing references for some blocks in a separate config file (#829)
Browse files Browse the repository at this point in the history
* add failing test for missing ref target

* bump github.com/hashicorp/hcl-lang to d770b42
  • Loading branch information
radeksimko authored Mar 14, 2022
1 parent 779e1e2 commit 50f67f0
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hc-install v0.3.1
github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44
github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/terraform-exec v0.16.0
github.com/hashicorp/terraform-json v0.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsK
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20211118124824-da3a292c5d7a/go.mod h1:0W3+VP07azoS+fCX5hWk1KxwHnqf1s9J7oBg2cFXm1c=
github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44 h1:R+ZKsdqgXle9KO2pLE6AwXg6IStjfM8wgQY4u0qPtx0=
github.com/hashicorp/hcl-lang v0.0.0-20220304112739-b2b314ea7c44/go.mod h1:vyszbX6YNHCKIaVUhbh3LIZljxwYOtgWCIkhT5zKfjc=
github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22 h1:u++Zu5hfJPSNuHh7cV1QfTItINnEGRMdOvT9KjaDQUQ=
github.com/hashicorp/hcl-lang v0.0.0-20220314150337-d770b425fb22/go.mod h1:vyszbX6YNHCKIaVUhbh3LIZljxwYOtgWCIkhT5zKfjc=
github.com/hashicorp/hcl/v2 v2.10.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
github.com/hashicorp/hcl/v2 v2.11.1 h1:yTyWcXcm9XB0TEkyU/JCRU6rYy4K+mgLtzn2wlrJbcc=
github.com/hashicorp/hcl/v2 v2.11.1/go.mod h1:FwWsfWEjyV/CMj8s/gqAuiviY72rJ1/oayI9WftqcKg=
Expand Down
166 changes: 166 additions & 0 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,172 @@ output "test" {
}`)
}

func TestVarReferenceCompletion_withValidData(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Path())

variableDecls := `variable "aaa" {}
variable "bbb" {}
variable "ccc" {}
`
f, err := os.Create(filepath.Join(tmpDir.Path(), "variables.tf"))
if err != nil {
t.Fatal(err)
}
_, err = f.WriteString(variableDecls)
if err != nil {
t.Fatal(err)
}
f.Close()

var testSchema tfjson.ProviderSchemas
err = json.Unmarshal([]byte(testModuleSchemaOutput), &testSchema)
if err != nil {
t.Fatal(err)
}

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
TerraformCalls: &exec.TerraformMockCalls{
PerWorkDir: map[string][]*mock.Call{
tmpDir.Path(): {
{
Method: "Version",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Method: "GetExecPath",
Repeatability: 1,
ReturnArguments: []interface{}{
"",
},
},
{
Method: "ProviderSchemas",
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
&testSchema,
nil,
},
},
},
},
}}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, tmpDir.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": "output \"test\" {\n value = var.\n}\n",
"uri": "%s/outputs.tf"
}
}`, tmpDir.URI)})
ls.CallAndExpectResponse(t, &langserver.CallRequest{
Method: "textDocument/completion",
ReqParams: fmt.Sprintf(`{
"textDocument": {
"uri": "%s/outputs.tf"
},
"position": {
"character": 14,
"line": 1
}
}`, tmpDir.URI)}, `{
"jsonrpc": "2.0",
"id": 3,
"result": {
"isIncomplete": false,
"items": [
{
"label": "var.aaa",
"labelDetails": {},
"kind": 6,
"detail": "dynamic",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 1,
"character": 10
},
"end": {
"line": 1,
"character": 14
}
},
"newText": "var.aaa"
}
},
{
"label": "var.bbb",
"labelDetails": {},
"kind": 6,
"detail": "dynamic",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 1,
"character": 10
},
"end": {
"line": 1,
"character": 14
}
},
"newText": "var.bbb"
}
},
{
"label": "var.ccc",
"labelDetails": {},
"kind": 6,
"detail": "dynamic",
"insertTextFormat": 1,
"textEdit": {
"range": {
"start": {
"line": 1,
"character": 10
},
"end": {
"line": 1,
"character": 14
}
},
"newText": "var.ccc"
}
}
]
}
}`)
}

func tfExecutor(t *testing.T, workdir, tfVersion string) exec.TerraformExecutor {
ctx := context.Background()
installDir := filepath.Join(t.TempDir(), "hcinstall")
Expand Down

0 comments on commit 50f67f0

Please sign in to comment.