Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static schema support for Terraform Test and Mock files #1782

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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