From 8fd8a48a06af33d3a3614b4b48768bd1516632fe Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 2 Mar 2026 16:39:42 +0100 Subject: [PATCH] use common testing helper in configs --- internal/checks/state_test.go | 18 ++++- internal/initwd/testing.go | 77 ---------------------- internal/lang/globalref/analyzer_test.go | 18 ++++- internal/refactoring/move_validate_test.go | 17 ++++- 4 files changed, 47 insertions(+), 83 deletions(-) delete mode 100644 internal/initwd/testing.go diff --git a/internal/checks/state_test.go b/internal/checks/state_test.go index 276f6dc62c..7c2b0c73a8 100644 --- a/internal/checks/state_test.go +++ b/internal/checks/state_test.go @@ -10,6 +10,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/configs/configload" "github.com/hashicorp/terraform/internal/initwd" ) @@ -29,9 +30,22 @@ func TestChecksHappyPath(t *testing.T) { ///////////////////////////////////////////////////////////////////////// - cfg, hclDiags := loader.LoadStaticConfig(fixtureDir) + // Note: This test uses BuildConfig instead of + // terraform.BuildConfigWithGraph to avoid an import cycle (terraform + // imports the checks package). Since this test only needs basic config + // structure without expression evaluation, the static loader is appropriate. + rootMod, hclDiags := loader.LoadRootModule(fixtureDir) if hclDiags.HasErrors() { - t.Fatalf("invalid configuration: %s", hclDiags.Error()) + t.Fatalf("invalid root module: %s", hclDiags.Error()) + } + + cfg, buildDiags := configs.BuildConfig( + rootMod, + loader.ModuleWalker(), + configs.MockDataLoaderFunc(loader.LoadExternalMockData), + ) + if buildDiags.HasErrors() { + t.Fatalf("invalid configuration: %s", buildDiags.Error()) } resourceA := addrs.Resource{ diff --git a/internal/initwd/testing.go b/internal/initwd/testing.go deleted file mode 100644 index 6566ce4e67..0000000000 --- a/internal/initwd/testing.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright IBM Corp. 2014, 2026 -// SPDX-License-Identifier: BUSL-1.1 - -package initwd - -import ( - "context" - "testing" - - "github.com/hashicorp/terraform/internal/configs" - "github.com/hashicorp/terraform/internal/configs/configload" - "github.com/hashicorp/terraform/internal/registry" - "github.com/hashicorp/terraform/internal/tfdiags" -) - -// LoadConfigForTests is a convenience wrapper around configload.NewLoaderForTests, -// ModuleInstaller.InstallModules and configload.Loader.LoadConfig that allows -// a test configuration to be loaded in a single step. -// -// If module installation fails, t.Fatal (or similar) is called to halt -// execution of the test, under the assumption that installation failures are -// not expected. If installation failures _are_ expected then use -// NewLoaderForTests and work with the loader object directly. If module -// installation succeeds but generates warnings, these warnings are discarded. -// -// If installation succeeds but errors are detected during loading then a -// possibly-incomplete config is returned along with error diagnostics. The -// test run is not aborted in this case, so that the caller can make assertions -// against the returned diagnostics. -// -// As with NewLoaderForTests, a cleanup function is returned which must be -// called before the test completes in order to remove the temporary -// modules directory. -func LoadConfigForTests(t *testing.T, rootDir string, testsDir string) (*configs.Config, *configload.Loader, func(), tfdiags.Diagnostics) { - t.Helper() - - var diags tfdiags.Diagnostics - - loader, cleanup := configload.NewLoaderForTests(t) - inst := NewModuleInstaller(loader.ModulesDir(), loader, registry.NewClient(nil, nil), nil) - - _, moreDiags := inst.InstallModules(context.Background(), rootDir, testsDir, true, false, ModuleInstallHooksImpl{}) - diags = diags.Append(moreDiags) - if diags.HasErrors() { - cleanup() - t.Fatal(diags.Err()) - return nil, nil, func() {}, diags - } - - // Since module installer has modified the module manifest on disk, we need - // to refresh the cache of it in the loader. - if err := loader.RefreshModules(); err != nil { - t.Fatalf("failed to refresh modules after installation: %s", err) - } - - config, hclDiags := loader.LoadStaticConfigWithTests(rootDir, testsDir) - diags = diags.Append(hclDiags) - return config, loader, cleanup, diags -} - -// MustLoadConfigForTests is a variant of LoadConfigForTests which calls -// t.Fatal (or similar) if there are any errors during loading, and thus -// does not return diagnostics at all. -// -// This is useful for concisely writing tests that don't expect errors at -// all. For tests that expect errors and need to assert against them, use -// LoadConfigForTests instead. -func MustLoadConfigForTests(t *testing.T, rootDir, testsDir string) (*configs.Config, *configload.Loader, func()) { - t.Helper() - - config, loader, cleanup, diags := LoadConfigForTests(t, rootDir, testsDir) - if diags.HasErrors() { - cleanup() - t.Fatal(diags.Err()) - } - return config, loader, cleanup -} diff --git a/internal/lang/globalref/analyzer_test.go b/internal/lang/globalref/analyzer_test.go index 416cccce48..76a1190959 100644 --- a/internal/lang/globalref/analyzer_test.go +++ b/internal/lang/globalref/analyzer_test.go @@ -11,6 +11,7 @@ import ( "github.com/zclconf/go-cty/cty" "github.com/hashicorp/terraform/internal/addrs" + "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/configs/configload" "github.com/hashicorp/terraform/internal/configs/configschema" "github.com/hashicorp/terraform/internal/initwd" @@ -33,9 +34,22 @@ func testAnalyzer(t *testing.T, fixtureName string) *Analyzer { t.Fatalf("failed to refresh modules after install: %s", err) } - cfg, loadDiags := loader.LoadStaticConfig(configDir) + // Note: This test uses BuildConfig instead of + // terraform.BuildConfigWithGraph to avoid an import cycle (terraform + // imports the lang package). Since this test only needs basic config + // structure without expression evaluation, the static loader is appropriate. + rootMod, loadDiags := loader.LoadRootModule(configDir) if loadDiags.HasErrors() { - t.Fatalf("unexpected configuration errors: %s", loadDiags.Error()) + t.Fatalf("invalid root module: %s", loadDiags.Error()) + } + + cfg, buildDiags := configs.BuildConfig( + rootMod, + loader.ModuleWalker(), + configs.MockDataLoaderFunc(loader.LoadExternalMockData), + ) + if buildDiags.HasErrors() { + t.Fatalf("invalid configuration: %s", buildDiags.Error()) } resourceTypeSchema := &configschema.Block{ diff --git a/internal/refactoring/move_validate_test.go b/internal/refactoring/move_validate_test.go index bc9b66c2db..e0b2ca1e30 100644 --- a/internal/refactoring/move_validate_test.go +++ b/internal/refactoring/move_validate_test.go @@ -533,9 +533,22 @@ func loadRefactoringFixture(t *testing.T, dir string) (*configs.Config, instance t.Fatalf("failed to refresh modules after installation: %s", err) } - rootCfg, diags := loader.LoadStaticConfig(dir) + // Note: This test uses BuildConfig instead of + // terraform.BuildConfigWithGraph to avoid an import cycle (terraform + // imports the refactoring package). Since this test only needs basic config + // structure without expression evaluation, the static loader is appropriate. + rootMod, diags := loader.LoadRootModule(dir) if diags.HasErrors() { - t.Fatalf("failed to load root module: %s", diags.Error()) + t.Fatalf("invalid root module: %s", diags.Error()) + } + + rootCfg, buildDiags := configs.BuildConfig( + rootMod, + loader.ModuleWalker(), + configs.MockDataLoaderFunc(loader.LoadExternalMockData), + ) + if buildDiags.HasErrors() { + t.Fatalf("invalid configuration: %s", buildDiags.Error()) } expander := instances.NewExpander(nil)