opentofu/internal/configs/configload/testing.go
Martin Atkins 31349a9c3a Move configs/ to internal/configs/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

44 lines
1.1 KiB
Go

package configload
import (
"io/ioutil"
"os"
"testing"
)
// NewLoaderForTests is a variant of NewLoader that is intended to be more
// convenient for unit tests.
//
// The loader's modules directory is a separate temporary directory created
// for each call. Along with the created loader, this function returns a
// cleanup function that should be called before the test completes in order
// to remove that temporary directory.
//
// In the case of any errors, t.Fatal (or similar) will be called to halt
// execution of the test, so the calling test does not need to handle errors
// itself.
func NewLoaderForTests(t *testing.T) (*Loader, func()) {
t.Helper()
modulesDir, err := ioutil.TempDir("", "tf-configs")
if err != nil {
t.Fatalf("failed to create temporary modules dir: %s", err)
return nil, func() {}
}
cleanup := func() {
os.RemoveAll(modulesDir)
}
loader, err := NewLoader(&Config{
ModulesDir: modulesDir,
})
if err != nil {
cleanup()
t.Fatalf("failed to create config loader: %s", err)
return nil, func() {}
}
return loader, cleanup
}