Fix module source expr tests

Daniel Banck 1 week ago
parent ea9d8cc873
commit 512cf3be2f
No known key found for this signature in database

@ -16,13 +16,6 @@ import (
) )
func TestParseConfigResourceFromExpression(t *testing.T) { func TestParseConfigResourceFromExpression(t *testing.T) {
mustExpr := func(expr hcl.Expression, diags hcl.Diagnostics) hcl.Expression {
if diags != nil {
panic(diags.Error())
}
return expr
}
tests := []struct { tests := []struct {
expr hcl.Expression expr hcl.Expression
expect addrs.ConfigResource expect addrs.ConfigResource
@ -280,3 +273,10 @@ func mustAbsResourceInstanceAddr(str string) addrs.AbsResourceInstance {
} }
return addr return addr
} }
func mustExpr(expr hcl.Expression, diags hcl.Diagnostics) hcl.Expression {
if diags != nil {
panic(diags.Error())
}
return expr
}

@ -4,18 +4,19 @@
package configs package configs
import ( import (
"io/ioutil" "os"
"testing" "testing"
"github.com/go-test/deep" "github.com/go-test/deep"
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/getmodules/moduleaddrs" "github.com/hashicorp/terraform/internal/getmodules/moduleaddrs"
) )
func TestLoadModuleCall(t *testing.T) { func TestLoadModuleCall(t *testing.T) {
src, err := ioutil.ReadFile("testdata/invalid-files/module-calls.tf") src, err := os.ReadFile("testdata/invalid-files/module-calls.tf")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -32,7 +33,11 @@ func TestLoadModuleCall(t *testing.T) {
gotModules := file.ModuleCalls gotModules := file.ModuleCalls
wantModules := []*ModuleCall{ wantModules := []*ModuleCall{
{ {
Name: "foo", Name: "foo",
SourceExpr: mustExpr(hclsyntax.ParseExpression(
[]byte("\"./foo\""), "module-calls.tf",
hcl.Pos{Line: 3, Column: 12, Byte: 27},
)),
SourceAddr: addrs.ModuleSourceLocal("./foo"), SourceAddr: addrs.ModuleSourceLocal("./foo"),
SourceAddrRaw: "./foo", SourceAddrRaw: "./foo",
SourceSet: true, SourceSet: true,
@ -49,6 +54,10 @@ func TestLoadModuleCall(t *testing.T) {
}, },
{ {
Name: "bar", Name: "bar",
SourceExpr: mustExpr(hclsyntax.ParseExpression(
[]byte("\"hashicorp/bar/aws\""), "module-calls.tf",
hcl.Pos{Line: 8, Column: 12, Byte: 113},
)),
SourceAddr: addrs.ModuleSourceRegistry{ SourceAddr: addrs.ModuleSourceRegistry{
Package: addrs.ModuleRegistryPackage{ Package: addrs.ModuleRegistryPackage{
Host: addrs.DefaultModuleRegistryHost, Host: addrs.DefaultModuleRegistryHost,
@ -72,6 +81,10 @@ func TestLoadModuleCall(t *testing.T) {
}, },
{ {
Name: "baz", Name: "baz",
SourceExpr: mustExpr(hclsyntax.ParseExpression(
[]byte("\"git::https://example.com/\""), "module-calls.tf",
hcl.Pos{Line: 15, Column: 12, Byte: 193},
)),
SourceAddr: addrs.ModuleSourceRemote{ SourceAddr: addrs.ModuleSourceRemote{
Package: addrs.ModulePackage("git::https://example.com/"), Package: addrs.ModulePackage("git::https://example.com/"),
}, },

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty/cty" "github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/addrs"
@ -91,7 +92,11 @@ func TestModuleOverrideModule(t *testing.T) {
got := mod.ModuleCalls["example"] got := mod.ModuleCalls["example"]
want := &ModuleCall{ want := &ModuleCall{
Name: "example", Name: "example",
SourceExpr: mustExpr(hclsyntax.ParseExpression(
[]byte("\"./example2-a_override\""), "testdata/valid-modules/override-module/a_override.tf",
hcl.Pos{Line: 3, Column: 12, Byte: 31},
)),
SourceAddr: addrs.ModuleSourceLocal("./example2-a_override"), SourceAddr: addrs.ModuleSourceLocal("./example2-a_override"),
SourceAddrRaw: "./example2-a_override", SourceAddrRaw: "./example2-a_override",
SourceAddrRange: hcl.Range{ SourceAddrRange: hcl.Range{

Loading…
Cancel
Save