tofu+configload: Test utilities take testing.TB

A lot of our testing utilities were written before the introduction of the
testing.TB interface that represents what *testing.T and *testing.B have
in common, and those that weren't have followed precedent from those that
were in directly expecting *testing.T.

Using testing.TB instead makes these helpers usable from both normal tests
and testing benchmark functions, since they only use methods that are
available in both contexts.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins 2025-02-07 13:29:55 -08:00
parent de95b65faa
commit 30fcff72d8
3 changed files with 9 additions and 9 deletions

View File

@ -21,7 +21,7 @@ import (
// 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()) {
func NewLoaderForTests(t testing.TB) (*Loader, func()) {
t.Helper()
modulesDir, err := os.MkdirTemp("", "tf-configs")

View File

@ -254,7 +254,7 @@ resource "implicit_thing" "b" {
}
}
func testContext2(t *testing.T, opts *ContextOpts) *Context {
func testContext2(t testing.TB, opts *ContextOpts) *Context {
t.Helper()
ctx, diags := NewContext(opts)
@ -952,7 +952,7 @@ func legacyDiffComparisonString(changes *plans.Changes) string {
// assertNoDiagnostics fails the test in progress (using t.Fatal) if the given
// diagnostics is non-empty.
func assertNoDiagnostics(t *testing.T, diags tfdiags.Diagnostics) {
func assertNoDiagnostics(t testing.TB, diags tfdiags.Diagnostics) {
t.Helper()
if len(diags) == 0 {
return
@ -963,7 +963,7 @@ func assertNoDiagnostics(t *testing.T, diags tfdiags.Diagnostics) {
// assertNoDiagnostics fails the test in progress (using t.Fatal) if the given
// diagnostics has any errors.
func assertNoErrors(t *testing.T, diags tfdiags.Diagnostics) {
func assertNoErrors(t testing.TB, diags tfdiags.Diagnostics) {
t.Helper()
if !diags.HasErrors() {
return
@ -980,7 +980,7 @@ func assertNoErrors(t *testing.T, diags tfdiags.Diagnostics) {
// assertDiagnosticsMatch sorts the two sets of diagnostics in the usual way
// before comparing them, though diagnostics only have a partial order so that
// will not totally normalize the ordering of all diagnostics sets.
func assertDiagnosticsMatch(t *testing.T, got, want tfdiags.Diagnostics) {
func assertDiagnosticsMatch(t testing.TB, got, want tfdiags.Diagnostics) {
got = got.ForRPC()
want = want.ForRPC()
got.Sort()
@ -995,7 +995,7 @@ func assertDiagnosticsMatch(t *testing.T, got, want tfdiags.Diagnostics) {
// a test. It does not generate any errors or fail the test. See
// assertNoDiagnostics and assertNoErrors for more specific helpers that can
// also fail the test.
func logDiagnostics(t *testing.T, diags tfdiags.Diagnostics) {
func logDiagnostics(t testing.TB, diags tfdiags.Diagnostics) {
t.Helper()
for _, diag := range diags {
desc := diag.Description()

View File

@ -47,13 +47,13 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}
func testModule(t *testing.T, name string) *configs.Config {
func testModule(t testing.TB, name string) *configs.Config {
t.Helper()
c, _ := testModuleWithSnapshot(t, name)
return c
}
func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *configload.Snapshot) {
func testModuleWithSnapshot(t testing.TB, name string) (*configs.Config, *configload.Snapshot) {
t.Helper()
dir := filepath.Join(fixtureDir, name)
@ -90,7 +90,7 @@ func testModuleWithSnapshot(t *testing.T, name string) (*configs.Config, *config
// testModuleInline takes a map of path -> config strings and yields a config
// structure with those files loaded from disk
func testModuleInline(t *testing.T, sources map[string]string) *configs.Config {
func testModuleInline(t testing.TB, sources map[string]string) *configs.Config {
t.Helper()
cfgPath := t.TempDir()