From 512cf3be2f5ea04ea05af8746d89f119b32007f3 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Wed, 11 Feb 2026 17:30:43 +0100 Subject: [PATCH] Fix module source expr tests --- internal/configs/import_test.go | 14 +++++++------- internal/configs/module_call_test.go | 19 ++++++++++++++++--- internal/configs/module_merge_test.go | 7 ++++++- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/internal/configs/import_test.go b/internal/configs/import_test.go index 891aed2680..299ca45737 100644 --- a/internal/configs/import_test.go +++ b/internal/configs/import_test.go @@ -16,13 +16,6 @@ import ( ) 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 { expr hcl.Expression expect addrs.ConfigResource @@ -280,3 +273,10 @@ func mustAbsResourceInstanceAddr(str string) addrs.AbsResourceInstance { } return addr } + +func mustExpr(expr hcl.Expression, diags hcl.Diagnostics) hcl.Expression { + if diags != nil { + panic(diags.Error()) + } + return expr +} diff --git a/internal/configs/module_call_test.go b/internal/configs/module_call_test.go index 5f64f65506..bc8e01ad35 100644 --- a/internal/configs/module_call_test.go +++ b/internal/configs/module_call_test.go @@ -4,18 +4,19 @@ package configs import ( - "io/ioutil" + "os" "testing" "github.com/go-test/deep" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/getmodules/moduleaddrs" ) 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 { t.Fatal(err) } @@ -32,7 +33,11 @@ func TestLoadModuleCall(t *testing.T) { gotModules := file.ModuleCalls 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"), SourceAddrRaw: "./foo", SourceSet: true, @@ -49,6 +54,10 @@ func TestLoadModuleCall(t *testing.T) { }, { Name: "bar", + SourceExpr: mustExpr(hclsyntax.ParseExpression( + []byte("\"hashicorp/bar/aws\""), "module-calls.tf", + hcl.Pos{Line: 8, Column: 12, Byte: 113}, + )), SourceAddr: addrs.ModuleSourceRegistry{ Package: addrs.ModuleRegistryPackage{ Host: addrs.DefaultModuleRegistryHost, @@ -72,6 +81,10 @@ func TestLoadModuleCall(t *testing.T) { }, { 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{ Package: addrs.ModulePackage("git::https://example.com/"), }, diff --git a/internal/configs/module_merge_test.go b/internal/configs/module_merge_test.go index dee27a8b17..70c6a41ca2 100644 --- a/internal/configs/module_merge_test.go +++ b/internal/configs/module_merge_test.go @@ -9,6 +9,7 @@ import ( "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" + "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/zclconf/go-cty/cty" "github.com/hashicorp/terraform/internal/addrs" @@ -91,7 +92,11 @@ func TestModuleOverrideModule(t *testing.T) { got := mod.ModuleCalls["example"] 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"), SourceAddrRaw: "./example2-a_override", SourceAddrRange: hcl.Range{