From 30fcff72d8c0bde73ad06634b171a188d4caa360 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 7 Feb 2025 13:29:55 -0800 Subject: [PATCH] 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 --- internal/configs/configload/testing.go | 2 +- internal/tofu/context_test.go | 10 +++++----- internal/tofu/opentf_test.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/configs/configload/testing.go b/internal/configs/configload/testing.go index 81b8324baa..3450d92512 100644 --- a/internal/configs/configload/testing.go +++ b/internal/configs/configload/testing.go @@ -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") diff --git a/internal/tofu/context_test.go b/internal/tofu/context_test.go index a1244ebfdb..efa1857d5b 100644 --- a/internal/tofu/context_test.go +++ b/internal/tofu/context_test.go @@ -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() diff --git a/internal/tofu/opentf_test.go b/internal/tofu/opentf_test.go index 750b18963e..150a6728c9 100644 --- a/internal/tofu/opentf_test.go +++ b/internal/tofu/opentf_test.go @@ -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()