Skip to content

Commit

Permalink
Static schema support for Terraform Test and Mock files (#1782)
Browse files Browse the repository at this point in the history
This adds Terraform Tests support. It adds the new "feature", registers it in the service handler, and introduces two new language IDs: terraform-test & terraform-mock.

* Add Terraform Test feature which supports Terraform Test and Mock files
* chore: add links to tracking issues to todo comments
* chore: add changie entry
* Bump terraform-schema to `2bacbf6`
* fix: adjust code to comply with current main (did use some stacks specific change)
* Bump hcl-lang to `468c47e`

---------

Co-authored-by: Ansgar Mertens <ansgar@hashicorp.com>
  • Loading branch information
dbanck and ansgarm authored Sep 23, 2024
1 parent 6a45115 commit b397291
Show file tree
Hide file tree
Showing 26 changed files with 1,944 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20240912-120041.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: Static schema support for Terraform Test and Mock files
time: 2024-09-12T12:00:41.4902+02:00
custom:
Issue: "1782"
Repository: terraform-ls
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ require (
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.7.0
github.com/hashicorp/hc-install v0.9.0
github.com/hashicorp/hcl-lang v0.0.0-20240605150436-0e930f47b31b
github.com/hashicorp/hcl/v2 v2.21.0
github.com/hashicorp/hcl-lang v0.0.0-20240830144831-468c47ee72a9
github.com/hashicorp/hcl/v2 v2.22.0
github.com/hashicorp/terraform-exec v0.21.0
github.com/hashicorp/terraform-json v0.22.1
github.com/hashicorp/terraform-registry-address v0.2.3
github.com/hashicorp/terraform-schema v0.0.0-20240715103008-d7b11d826dc8
github.com/hashicorp/terraform-schema v0.0.0-20240920131432-2bacbf6cd0d0
github.com/mcuadros/go-defaults v1.2.0
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.5
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,18 @@ github.com/hashicorp/hc-install v0.9.0 h1:2dIk8LcvANwtv3QZLckxcjyF5w8KVtiMxu6G6e
github.com/hashicorp/hc-install v0.9.0/go.mod h1:+6vOP+mf3tuGgMApVYtmsnDoKWMDcFXeTxCACYZ8SFg=
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-20240605150436-0e930f47b31b h1:DRIJYSwLKoAfdndwIH1NbjLC3wm/RuyT7eEtm8aKw1U=
github.com/hashicorp/hcl-lang v0.0.0-20240605150436-0e930f47b31b/go.mod h1:/g6sedjVJX99knsqTKU9wSWBVtsyDKWJkseNV9Zx1aU=
github.com/hashicorp/hcl/v2 v2.21.0 h1:lve4q/o/2rqwYOgUg3y3V2YPyD1/zkCLGjIV74Jit14=
github.com/hashicorp/hcl/v2 v2.21.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/hcl-lang v0.0.0-20240830144831-468c47ee72a9 h1:+vOoWN3mmsPs/qdx0DhdgA0JO20uqIoibrti+VT9gb4=
github.com/hashicorp/hcl-lang v0.0.0-20240830144831-468c47ee72a9/go.mod h1:q2ps+/W6LMDEr2Y6Z92s0EX7jhMrftJEuwD6LXOv20A=
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/terraform-exec v0.21.0 h1:uNkLAe95ey5Uux6KJdua6+cv8asgILFVWkd/RG0D2XQ=
github.com/hashicorp/terraform-exec v0.21.0/go.mod h1:1PPeMYou+KDUSSeRE9szMZ/oHf4fYUmB923Wzbq1ICg=
github.com/hashicorp/terraform-json v0.22.1 h1:xft84GZR0QzjPVWs4lRUwvTcPnegqlyS7orfb5Ltvec=
github.com/hashicorp/terraform-json v0.22.1/go.mod h1:JbWSQCLFSXFFhg42T7l9iJwdGXBYV8fmmD6o/ML4p3A=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-schema v0.0.0-20240715103008-d7b11d826dc8 h1:5y1/KiaPi/Ib/ZAkaKS30EjARaKiK2isnTrpI5pe9lI=
github.com/hashicorp/terraform-schema v0.0.0-20240715103008-d7b11d826dc8/go.mod h1:ar787Bv/qD6tlnjtzH8fQ1Yi6c/B5LsnpFlO8c92Atg=
github.com/hashicorp/terraform-schema v0.0.0-20240920131432-2bacbf6cd0d0 h1:tozkcF+T0q75+fJ1ae5W0bh+o6fn3ySQWJjoIh78QGg=
github.com/hashicorp/terraform-schema v0.0.0-20240920131432-2bacbf6cd0d0/go.mod h1:RnKF3wkBHxu54QMGQV3wzHiqunvNwuf9/JE8jUtqQxk=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=
Expand Down
4 changes: 4 additions & 0 deletions internal/features/modules/modules_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,7 @@ func (f *ModulesFeature) MetadataReady(dir document.DirHandle) (<-chan struct{},

return f.Store.MetadataReady(dir)
}

func (s *ModulesFeature) LocalModuleMeta(modPath string) (*tfmod.Meta, error) {
return s.Store.LocalModuleMeta(modPath)
}
143 changes: 143 additions & 0 deletions internal/features/tests/ast/tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ast

import (
"strings"

"github.com/hashicorp/hcl/v2"
globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast"
)

type Filename interface {
String() string
IsJSON() bool
IsIgnored() bool
}

// TestFilename is a custom type for test configuration files
type TestFilename string

func (mf TestFilename) String() string {
return string(mf)
}

func (mf TestFilename) IsJSON() bool {
return strings.HasSuffix(string(mf), ".json")
}

func (mf TestFilename) IsIgnored() bool {
return globalAst.IsIgnoredFile(string(mf))
}

func IsTestFilename(name string) bool {
return strings.HasSuffix(name, ".tftest.hcl") ||
strings.HasSuffix(name, ".tftest.json")
}

// MockFilename is a custom type for mock configuration files
type MockFilename string

func (df MockFilename) String() string {
return string(df)
}

func (df MockFilename) IsJSON() bool {
return strings.HasSuffix(string(df), ".json")
}

func (df MockFilename) IsIgnored() bool {
return globalAst.IsIgnoredFile(string(df))
}

func IsMockFilename(name string) bool {
return strings.HasSuffix(name, ".tfmock.hcl") ||
strings.HasSuffix(name, ".tfmock.json")
}

// FilenameFromName returns either a TestFilename or MockFilename based
// on the name
func FilenameFromName(name string) Filename {
if IsTestFilename(name) {
return TestFilename(name)
}
if IsMockFilename(name) {
return MockFilename(name)
}

return nil
}

type Files map[Filename]*hcl.File

func (sf Files) Copy() Files {
m := make(Files, len(sf))
for name, file := range sf {
m[name] = file
}
return m
}

func (mf Files) AsMap() map[string]*hcl.File {
m := make(map[string]*hcl.File, len(mf))
for name, file := range mf {
m[name.String()] = file
}
return m
}

type Diagnostics map[Filename]hcl.Diagnostics

func DiagnosticsFromMap(m map[string]hcl.Diagnostics) Diagnostics {
mf := make(Diagnostics, len(m))
for name, file := range m {
mf[FilenameFromName(name)] = file
}
return mf
}

func (sd Diagnostics) Copy() Diagnostics {
m := make(Diagnostics, len(sd))
for name, diags := range sd {
m[name] = diags
}
return m
}

// AutoloadedOnly returns only diagnostics that are not from ignored files
func (sd Diagnostics) AutoloadedOnly() Diagnostics {
diags := make(Diagnostics)
for name, f := range sd {
if !name.IsIgnored() {
diags[name] = f
}
}
return diags
}

func (sd Diagnostics) AsMap() map[string]hcl.Diagnostics {
m := make(map[string]hcl.Diagnostics, len(sd))
for name, diags := range sd {
m[name.String()] = diags
}
return m
}

func (sd Diagnostics) Count() int {
count := 0
for _, diags := range sd {
count += len(diags)
}
return count
}

type SourceDiagnostics map[globalAst.DiagnosticSource]Diagnostics

func (svd SourceDiagnostics) Count() int {
count := 0
for _, diags := range svd {
count += diags.Count()
}
return count
}
Loading

0 comments on commit b397291

Please sign in to comment.