mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
39e609d5fd
Previously we were using the experimental HCL 2 repository, but now we'll shift over to the v2 import path within the main HCL repository as part of actually releasing HCL 2.0 as stable. This is a mechanical search/replace to the new import paths. It also switches to the v2.0.0 release of HCL, which includes some new code that Terraform didn't previously have but should not change any behavior that matters for Terraform's purposes. For the moment the experimental HCL2 repository is still an indirect dependency via terraform-config-inspect, so it remains in our go.sum and vendor directories for the moment. Because terraform-config-inspect uses a much smaller subset of the HCL2 functionality, this does still manage to prune the vendor directory a little. A subsequent release of terraform-config-inspect should allow us to completely remove that old repository in a future commit.
117 lines
3.5 KiB
Go
117 lines
3.5 KiB
Go
package configs
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
"github.com/hashicorp/hcl/v2"
|
|
)
|
|
|
|
func TestBuildConfig(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
mod, diags := parser.LoadConfigDir("testdata/config-build")
|
|
assertNoDiagnostics(t, diags)
|
|
if mod == nil {
|
|
t.Fatal("got nil root module; want non-nil")
|
|
}
|
|
|
|
versionI := 0
|
|
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
|
|
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
|
|
// For the sake of this test we're going to just treat our
|
|
// SourceAddr as a path relative to our fixture directory.
|
|
// A "real" implementation of ModuleWalker should accept the
|
|
// various different source address syntaxes Terraform supports.
|
|
sourcePath := filepath.Join("testdata/config-build", req.SourceAddr)
|
|
|
|
mod, diags := parser.LoadConfigDir(sourcePath)
|
|
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
|
|
versionI++
|
|
return mod, version, diags
|
|
},
|
|
))
|
|
assertNoDiagnostics(t, diags)
|
|
if cfg == nil {
|
|
t.Fatal("got nil config; want non-nil")
|
|
}
|
|
|
|
var got []string
|
|
cfg.DeepEach(func(c *Config) {
|
|
got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version))
|
|
})
|
|
sort.Strings(got)
|
|
want := []string{
|
|
" <nil>",
|
|
"child_a 1.0.0",
|
|
"child_a.child_c 1.0.1",
|
|
"child_b 1.0.2",
|
|
"child_b.child_c 1.0.3",
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
|
|
}
|
|
|
|
if _, exists := cfg.Children["child_a"].Children["child_c"].Module.Outputs["hello"]; !exists {
|
|
t.Fatalf("missing output 'hello' in child_a.child_c")
|
|
}
|
|
if _, exists := cfg.Children["child_b"].Children["child_c"].Module.Outputs["hello"]; !exists {
|
|
t.Fatalf("missing output 'hello' in child_b.child_c")
|
|
}
|
|
if cfg.Children["child_a"].Children["child_c"].Module == cfg.Children["child_b"].Children["child_c"].Module {
|
|
t.Fatalf("child_a.child_c is same object as child_b.child_c; should not be")
|
|
}
|
|
}
|
|
|
|
func TestBuildConfigDiags(t *testing.T) {
|
|
parser := NewParser(nil)
|
|
mod, diags := parser.LoadConfigDir("testdata/nested-errors")
|
|
assertNoDiagnostics(t, diags)
|
|
if mod == nil {
|
|
t.Fatal("got nil root module; want non-nil")
|
|
}
|
|
|
|
versionI := 0
|
|
cfg, diags := BuildConfig(mod, ModuleWalkerFunc(
|
|
func(req *ModuleRequest) (*Module, *version.Version, hcl.Diagnostics) {
|
|
// For the sake of this test we're going to just treat our
|
|
// SourceAddr as a path relative to our fixture directory.
|
|
// A "real" implementation of ModuleWalker should accept the
|
|
// various different source address syntaxes Terraform supports.
|
|
sourcePath := filepath.Join("testdata/nested-errors", req.SourceAddr)
|
|
|
|
mod, diags := parser.LoadConfigDir(sourcePath)
|
|
version, _ := version.NewVersion(fmt.Sprintf("1.0.%d", versionI))
|
|
versionI++
|
|
return mod, version, diags
|
|
},
|
|
))
|
|
|
|
wantDiag := `testdata/nested-errors/child_c/child_c.tf:5,1-8: ` +
|
|
`Unsupported block type; Blocks of type "invalid" are not expected here.`
|
|
assertExactDiagnostics(t, diags, []string{wantDiag})
|
|
|
|
// we should still have module structure loaded
|
|
var got []string
|
|
cfg.DeepEach(func(c *Config) {
|
|
got = append(got, fmt.Sprintf("%s %s", strings.Join(c.Path, "."), c.Version))
|
|
})
|
|
sort.Strings(got)
|
|
want := []string{
|
|
" <nil>",
|
|
"child_a 1.0.0",
|
|
"child_a.child_c 1.0.1",
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("wrong result\ngot: %swant: %s", spew.Sdump(got), spew.Sdump(want))
|
|
}
|
|
}
|