mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 01:41:48 -06:00
ebcf7455eb
* Rename module name from "github.com/hashicorp/terraform" to "github.com/placeholderplaceholderplaceholder/opentf". Signed-off-by: Jakub Martin <kubam@spacelift.io> * Gofmt. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Regenerate protobuf. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Fix comments. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Undo issue and pull request link changes. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Undo comment changes. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Fix comment. Signed-off-by: Jakub Martin <kubam@spacelift.io> * Undo some link changes. Signed-off-by: Jakub Martin <kubam@spacelift.io> * make generate && make protobuf Signed-off-by: Jakub Martin <kubam@spacelift.io> --------- Signed-off-by: Jakub Martin <kubam@spacelift.io>
78 lines
3.0 KiB
Go
78 lines
3.0 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package initwd
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/configs"
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/configs/configload"
|
|
"github.com/placeholderplaceholderplaceholder/opentf/internal/registry"
|
|
"github.com/placeholderplaceholderplaceholder/opentf/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))
|
|
|
|
_, 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.LoadConfig(rootDir)
|
|
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
|
|
}
|