From 4b45edeac1b93596b3cf1cef1d86a0ac99a2d412 Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Wed, 18 Dec 2024 19:28:48 -0500 Subject: [PATCH 1/3] Add testingiface package --- internal/testingiface/doc.go | 28 +++++ internal/testingiface/expect.go | 143 ++++++++++++++++++++++++ internal/testingiface/expect_test.go | 102 ++++++++++++++++++ internal/testingiface/mockt.go | 156 +++++++++++++++++++++++++++ internal/testingiface/t.go | 47 ++++++++ 5 files changed, 476 insertions(+) create mode 100644 internal/testingiface/doc.go create mode 100644 internal/testingiface/expect.go create mode 100644 internal/testingiface/expect_test.go create mode 100644 internal/testingiface/mockt.go create mode 100644 internal/testingiface/t.go diff --git a/internal/testingiface/doc.go b/internal/testingiface/doc.go new file mode 100644 index 00000000..8ff49970 --- /dev/null +++ b/internal/testingiface/doc.go @@ -0,0 +1,28 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +// Package testingiface provides wrapper types compatible with the Go standard +// library [testing] package. These wrappers are necessary for implementing +// [testing] package helpers, since the Go standard library implementation is +// not extensible and the existing code in this Go module is built on directly +// interacting with [testing] functionality, such as calling [testing.T.Fatal]. +// +// The [T] interface has all methods of the [testing.T] type and the [MockT] +// type is a lightweight mock implementation of the [T] interface. There are a +// collection of assertion helper functions such as: +// - [ExpectFail]: That the test logic called the equivalent of +// [testing.T.Error] or [testing.T.Fatal]. +// - [ExpectParallel]: That the test logic called the equivalent of +// [testing.T.Parallel] and passed. +// - [ExpectPass]: That the test logic did not call the equivalent of +// [testing.T.Skip], since [testing] marks these tests as passing. +// - [ExpectSkip]: That the test logic called the equivalent of +// [testing.T.Skip]. +// +// This code in this package is intentionally internal and should not be exposed +// in the Go module API. It is compatible with the Go 1.17 [testing] package. +// It replaces the archived github.com/mitchellh/go-testing-interface Go module, +// but is implemented with different approaches that enable calls to behave +// more closely to the Go standard library, such as calling [runtime.Goexit] +// when skipping, and preserving any error/skip messaging. +package testingiface diff --git a/internal/testingiface/expect.go b/internal/testingiface/expect.go new file mode 100644 index 00000000..66aa66a5 --- /dev/null +++ b/internal/testingiface/expect.go @@ -0,0 +1,143 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testingiface + +import ( + "sync" +) + +// ExpectFailed provides a wrapper for test logic which should call any of the +// following: +// - [testing.T.Error] +// - [testing.T.Errorf] +// - [testing.T.Fatal] +// - [testing.T.Fatalf] +// +// If none of those were called, the real [testing.T.Fatal] is called to fail +// the test. +func ExpectFail(t T, logic func(*MockT)) { + t.Helper() + + mockT := &MockT{} + + var wg sync.WaitGroup + wg.Add(1) + + go func() { + defer wg.Done() + + logic(mockT) + }() + + wg.Wait() + + if mockT.Failed() { + return + } + + t.Fatal("expected test failure") +} + +// ExpectParallel provides a wrapper for test logic which should call the +// [testing.T.Parallel] method. If it doesn't, the real [testing.T.Fatal] is +// called. +func ExpectParallel(t T, logic func(*MockT)) { + t.Helper() + + mockT := &MockT{} + + var wg sync.WaitGroup + wg.Add(1) + + go func() { + defer wg.Done() + + logic(mockT) + }() + + wg.Wait() + + if mockT.Failed() { + t.Fatalf("unexpected test failure: %s", mockT.LastError()) + } + + if mockT.Skipped() { + t.Fatalf("unexpected test skip: %s", mockT.LastSkipped()) + } + + if mockT.IsParallel() { + return + } + + t.Fatal("expected test parallel") +} + +// ExpectPass provides a wrapper for test logic which should not call any of the +// following, which would mark the real test as passing: +// - [testing.T.Skip] +// - [testing.T.Skipf] +// +// If one of those were called, the real [testing.T.Fatal] is called to fail +// the test. This is only necessary to check for false positives with skipped +// tests. +func ExpectPass(t T, logic func(*MockT)) { + t.Helper() + + mockT := &MockT{} + + var wg sync.WaitGroup + wg.Add(1) + + go func() { + defer wg.Done() + + logic(mockT) + }() + + wg.Wait() + + if mockT.Failed() { + t.Fatalf("unexpected test failure: %s", mockT.LastError()) + } + + if mockT.Skipped() { + t.Fatalf("unexpected test skip: %s", mockT.LastSkipped()) + } + + // test passed as expected +} + +// ExpectSkip provides a wrapper for test logic which should call any of the +// following: +// - [testing.T.Skip] +// - [testing.T.Skipf] +// +// If none of those were called, the real [testing.T.Fatal] is called to fail +// the test. +func ExpectSkip(t T, logic func(*MockT)) { + t.Helper() + + mockT := &MockT{} + + var wg sync.WaitGroup + wg.Add(1) + + go func() { + defer wg.Done() + + logic(mockT) + }() + + wg.Wait() + + if mockT.Failed() { + t.Fatalf("unexpected test failure: %s", mockT.LastError()) + } + + if mockT.Skipped() { + return + } + + t.Fatal("test passed, expected test skip") +} diff --git a/internal/testingiface/expect_test.go b/internal/testingiface/expect_test.go new file mode 100644 index 00000000..3f0f3690 --- /dev/null +++ b/internal/testingiface/expect_test.go @@ -0,0 +1,102 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testingiface_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" +) + +func TestExpectFail(t *testing.T) { + t.Parallel() + + testingiface.ExpectPass(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectFail(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Fatal("test fatal") + }) + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectFail(mockT1, func(_ *testingiface.MockT) { + // intentionally no test error or test skip + }) + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectFail(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Skip("test skip") + }) + }) +} + +func TestExpectParallel(t *testing.T) { + t.Parallel() + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectParallel(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Fatal("test fatal") + }) + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectParallel(mockT1, func(_ *testingiface.MockT) { + // intentionally no test error or test skip + }) + }) + + testingiface.ExpectPass(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectParallel(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Parallel() + }) + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectParallel(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Skip("test skip") + }) + }) +} + +func TestExpectPass(t *testing.T) { + t.Parallel() + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectPass(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Fatal("test fatal") + }) + }) + + testingiface.ExpectPass(t, func(_ *testingiface.MockT) { + // intentionally no test error or test skip + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectPass(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Skip("test skip") + }) + }) +} + +func TestExpectSkip(t *testing.T) { + t.Parallel() + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectSkip(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Fatal("test fatal") + }) + }) + + testingiface.ExpectFail(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectSkip(mockT1, func(_ *testingiface.MockT) { + // intentionally no test error or test skip + }) + }) + + testingiface.ExpectPass(t, func(mockT1 *testingiface.MockT) { + testingiface.ExpectSkip(mockT1, func(mockT2 *testingiface.MockT) { + mockT2.Skip("test skip") + }) + }) +} diff --git a/internal/testingiface/mockt.go b/internal/testingiface/mockt.go new file mode 100644 index 00000000..9800fb23 --- /dev/null +++ b/internal/testingiface/mockt.go @@ -0,0 +1,156 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testingiface + +import ( + "fmt" + "os" + "runtime" + "testing" + "time" +) + +var _ T = (*MockT)(nil) + +// MockT is a lightweight, mock implementation of the non-extensible Go +// standard library [*testing.T] implementation. This type should only be used +// in the unit testing of functionality within this Go module and never exposed +// in the Go module API. +// +// This type intentionally is not feature-complete and only includes the methods +// necessary for the existing code in this Go module. +type MockT struct { + // TestName can be set to the name of the test, such as calling the real + // [testing.T.Name]. This is returned in the [Name] method. + TestName string + + isHelper bool + isFailed bool + isSkipped bool + isParallel bool + lastError string + lastSkipped string +} + +// T interface implementations + +func (t *MockT) Cleanup(func()) { + panic("not implemented") +} + +func (t *MockT) Deadline() (deadline time.Time, ok bool) { + panic("not implemented") +} + +func (t *MockT) Error(args ...any) { + t.lastError = fmt.Sprintln(args...) + t.Log(args...) + t.Fail() +} + +func (t *MockT) Errorf(format string, args ...any) { + t.lastError = fmt.Sprintf(format, args...) + t.Logf(format, args...) + t.Fail() +} + +func (t *MockT) Fail() { + t.isFailed = true +} + +func (t *MockT) Failed() bool { + return t.isFailed +} + +func (t *MockT) FailNow() { + t.Fail() + runtime.Goexit() +} + +func (t *MockT) Fatal(args ...any) { + t.lastError = fmt.Sprintln(args...) + t.Log(args...) + t.FailNow() +} + +func (t *MockT) Fatalf(format string, args ...any) { + t.lastError = fmt.Sprintf(format, args...) + t.Log(args...) + t.FailNow() +} + +func (t *MockT) Helper() { + t.isHelper = true +} + +func (t *MockT) Log(args ...any) { + if args == nil { + return + } + + fmt.Fprintln(os.Stdout, args...) +} + +func (t *MockT) Logf(format string, args ...any) { + if format == "" { + return + } + + fmt.Fprintf(os.Stdout, format, args...) +} + +func (t *MockT) Name() string { + return t.TestName +} + +func (t *MockT) Parallel() { + t.isParallel = true +} + +func (t *MockT) Run(name string, f func(t *testing.T)) bool { + panic("not implemented") +} + +func (t *MockT) Setenv(key string, value string) { + panic("not implemented") +} + +func (t *MockT) Skip(args ...any) { + t.lastSkipped = fmt.Sprintln(args...) + t.Log(args...) + t.SkipNow() +} + +func (t *MockT) Skipf(format string, args ...any) { + t.lastSkipped = fmt.Sprintf(format, args...) + t.Logf(format, args...) + t.SkipNow() +} + +func (t *MockT) SkipNow() { + t.isSkipped = true + runtime.Goexit() +} + +func (t *MockT) Skipped() bool { + return t.isSkipped +} + +func (t *MockT) TempDir() string { + panic("not implemented") +} + +// Custom methods + +func (t *MockT) IsParallel() bool { + return t.isParallel +} + +func (t *MockT) LastError() string { + return t.lastError +} + +func (t *MockT) LastSkipped() string { + return t.lastSkipped +} diff --git a/internal/testingiface/t.go b/internal/testingiface/t.go new file mode 100644 index 00000000..8858bf3a --- /dev/null +++ b/internal/testingiface/t.go @@ -0,0 +1,47 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package testingiface + +// T is the interface that contains all the methods of the Go standard library +// [*testing.T] type as of Go 1.17. +// +// For complete backwards compatibility, it explicitly does not include the +// Deadline and Run methods to match the prior +// github.com/mitchellh/go-testing-interface.T interface. If either of those +// methods are needed, it should be relatively safe to add to this interface +// under the guise that this internal interface should match the [*testing.T] +// implementation. +type T interface { + Cleanup(func()) + + // Excluded to match the prior github.com/mitchellh/go-testing-interface.T + // interface for complete backwards compatibility. It is relatively safe to + // introduce if necessary in the future though. + // Deadline() (deadline time.Time, ok bool) + + Error(args ...any) + Errorf(format string, args ...any) + Fail() + Failed() bool + FailNow() + Fatal(args ...any) + Fatalf(format string, args ...any) + Helper() + Log(args ...any) + Logf(format string, args ...any) + Name() string + Parallel() + + // Excluded to match the prior github.com/mitchellh/go-testing-interface.T + // interface for complete backwards compatibility. It is relatively safe to + // introduce if necessary in the future though. + // Run(name string, f func(*testing.T)) bool + + Setenv(key string, value string) + Skip(args ...any) + Skipf(format string, args ...any) + SkipNow() + Skipped() bool + TempDir() string +} From a19f3159f04ad872994d91a52997a55cb2c49ea1 Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Wed, 18 Dec 2024 20:08:52 -0500 Subject: [PATCH 2/3] Refactor tfversion to testingiface --- helper/resource/testing.go | 3 +- internal/logging/context.go | 4 +- internal/plugintest/util.go | 2 +- tfversion/all_test.go | 7 +- tfversion/any_test.go | 7 +- tfversion/require_above_test.go | 15 +- tfversion/require_below_test.go | 23 ++- tfversion/require_between_test.go | 31 ++- tfversion/require_not_test.go | 12 +- tfversion/skip_above_test.go | 241 ++++++++++++----------- tfversion/skip_between_test.go | 50 ++--- tfversion/skip_if_not_alpha_test.go | 26 +-- tfversion/skip_if_not_prerelease_test.go | 74 +++---- tfversion/skip_if_test.go | 26 +-- 14 files changed, 271 insertions(+), 250 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 9e1961a4..8eea0e57 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -15,8 +15,6 @@ import ( "strings" "time" - "github.com/mitchellh/go-testing-interface" - "github.com/hashicorp/terraform-plugin-go/tfprotov5" "github.com/hashicorp/terraform-plugin-go/tfprotov6" @@ -31,6 +29,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/internal/addrs" "github.com/hashicorp/terraform-plugin-testing/internal/logging" "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" + testing "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) // flagSweep is a flag available when running tests on the command line. It diff --git a/internal/logging/context.go b/internal/logging/context.go index 0fe8002a..aadec229 100644 --- a/internal/logging/context.go +++ b/internal/logging/context.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-log/tfsdklog" helperlogging "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" - testing "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) // InitContext creates SDK logger contexts when the provider is running in @@ -34,7 +34,7 @@ func InitContext(ctx context.Context) context.Context { // The standard library log package handling is important as provider code // under test may be using that package or another logging library outside of // terraform-plugin-log. -func InitTestContext(ctx context.Context, t testing.T) context.Context { +func InitTestContext(ctx context.Context, t testingiface.T) context.Context { helperlogging.SetOutput(t) ctx = tfsdklog.RegisterTestSink(ctx, t) diff --git a/internal/plugintest/util.go b/internal/plugintest/util.go index be187a01..8f220dd1 100644 --- a/internal/plugintest/util.go +++ b/internal/plugintest/util.go @@ -151,7 +151,7 @@ func CopyDir(src, dest, baseDirName string) error { // // Since we do not want the wrapping test to fail when an expected test error // occurs, it is required that the testLogic passed in uses -// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real +// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real ** // *testing.T. // // If Fatal() or Fatalf() is not called in the logic, the real (*testing.T).Fatal() will diff --git a/tfversion/all_test.go b/tfversion/all_test.go index aa326d6b..db9cc545 100644 --- a/tfversion/all_test.go +++ b/tfversion/all_test.go @@ -8,12 +8,11 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-go/tfprotov6" - testinginterface "github.com/mitchellh/go-testing-interface" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -80,8 +79,8 @@ func Test_All_Error(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil diff --git a/tfversion/any_test.go b/tfversion/any_test.go index 76eacaa0..b5f58b79 100644 --- a/tfversion/any_test.go +++ b/tfversion/any_test.go @@ -8,10 +8,9 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-go/tfprotov6" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" @@ -73,8 +72,8 @@ func Test_Any_Error(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil diff --git a/tfversion/require_above_test.go b/tfversion/require_above_test.go index 8e8c3ea8..753bfb28 100644 --- a/tfversion/require_above_test.go +++ b/tfversion/require_above_test.go @@ -10,12 +10,11 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_RequireAbove_Equal(t *testing.T) { //nolint:paralleltest @@ -68,8 +67,8 @@ func Test_RequireAbove_Lower(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.0.7") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil @@ -139,8 +138,8 @@ func Test_RequireAbove_Prerelease_HigherCoreVersion(t *testing.T) { //nolint:par // 1.8.0 core version. This intentionally verifies that the logic does not // ignore the core version of the prerelease version when compared against // the core version of the check. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -162,8 +161,8 @@ func Test_RequireAbove_Prerelease_HigherPrerelease(t *testing.T) { //nolint:para // The 1.7.0-rc1 prerelease should always be considered to be // below the 1.7.0-rc2 prerelease. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, diff --git a/tfversion/require_below_test.go b/tfversion/require_below_test.go index 01d09d6b..5b7f5d04 100644 --- a/tfversion/require_below_test.go +++ b/tfversion/require_below_test.go @@ -10,20 +10,19 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_RequireBelow_Equal(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.7.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -69,8 +68,8 @@ func Test_RequireBelow_Higher(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.4.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil @@ -98,8 +97,8 @@ func Test_RequireBelow_Prerelease_EqualCoreVersion(t *testing.T) { //nolint:para // core versions. // // Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303 - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -165,8 +164,8 @@ func Test_RequireBelow_Prerelease_LowerCoreVersion(t *testing.T) { //nolint:para // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.7.0 core version. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -188,8 +187,8 @@ func Test_RequireBelow_Prerelease_LowerPrerelease(t *testing.T) { //nolint:paral // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.8.0-beta1 prerelease. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, diff --git a/tfversion/require_between_test.go b/tfversion/require_between_test.go index 9508ec2a..179b3e31 100644 --- a/tfversion/require_between_test.go +++ b/tfversion/require_between_test.go @@ -10,12 +10,11 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_RequireBetween(t *testing.T) { //nolint:paralleltest @@ -60,8 +59,8 @@ func Test_RequireBetween_Error_BelowMin(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil @@ -82,8 +81,8 @@ func Test_RequireBetween_Error_BelowMin(t *testing.T) { //nolint:paralleltest func Test_RequireBetween_Error_EqToMax(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.3.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil @@ -111,8 +110,8 @@ func Test_RequireBetween_Prerelease_MaxEqualCoreVersion(t *testing.T) { //nolint // core versions. // // Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303 - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -184,8 +183,8 @@ func Test_RequireBetween_Prerelease_MinHigherCoreVersion(t *testing.T) { //nolin // 1.8.0 core version. This intentionally verifies that the logic does not // ignore the core version of the prerelease version when compared against // the core version of the check. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -228,8 +227,8 @@ func Test_RequireBetween_Prerelease_MinHigherPrerelease(t *testing.T) { //nolint // The 1.7.0-rc1 prerelease should always be considered to be // below the 1.7.0-rc2 prerelease. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -251,8 +250,8 @@ func Test_RequireBetween_Prerelease_MaxLowerCoreVersion(t *testing.T) { //nolint // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.7.0 core version. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, @@ -295,8 +294,8 @@ func Test_RequireBetween_Prerelease_MaxLowerPrerelease(t *testing.T) { //nolint: // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.8.0-beta1 prerelease. - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, diff --git a/tfversion/require_not_test.go b/tfversion/require_not_test.go index 6eba3e7b..084d7205 100644 --- a/tfversion/require_not_test.go +++ b/tfversion/require_not_test.go @@ -8,14 +8,12 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/terraform-plugin-go/tfprotov6" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" r "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - - testinginterface "github.com/mitchellh/go-testing-interface" ) func Test_RequireNot(t *testing.T) { //nolint:paralleltest @@ -41,8 +39,8 @@ func Test_RequireNot_Error(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0") - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature return nil, nil @@ -70,8 +68,8 @@ func Test_RequireNot_Prerelease_EqualCoreVersion(t *testing.T) { //nolint:parall // core versions. // // Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303 - plugintest.TestExpectTFatal(t, func() { - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ + testingiface.ExpectFail(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ "test": providerserver.NewProviderServer(testprovider.Provider{}), }, diff --git a/tfversion/skip_above_test.go b/tfversion/skip_above_test.go index db9898c3..9c798134 100644 --- a/tfversion/skip_above_test.go +++ b/tfversion/skip_above_test.go @@ -12,6 +12,7 @@ import ( r "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" "github.com/hashicorp/terraform-plugin-testing/tfversion" ) @@ -19,25 +20,25 @@ func Test_SkipAbove_Lower(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.3.0") - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature - return nil, nil - }, - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.2.9"))), - }, - Steps: []r.TestStep{ - { - //module_variable_optional_attrs experiment is deprecated in TF v1.3.0 - Config: ` + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.2.9"))), + }, + Steps: []r.TestStep{ + { + //module_variable_optional_attrs experiment is deprecated in TF v1.3.0 + Config: ` terraform { - experiments = [module_variable_optional_attrs] + experiments = [module_variable_optional_attrs] } `, + }, }, - }, + }) }) } @@ -45,25 +46,25 @@ func Test_SkipAbove_Equal(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.2.9") - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature - return nil, nil - }, - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.2.9"))), - }, - Steps: []r.TestStep{ - { - //module_variable_optional_attrs experiment is deprecated in TF v1.3.0 - Config: ` + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.2.9"))), + }, + Steps: []r.TestStep{ + { + //module_variable_optional_attrs experiment is deprecated in TF v1.3.0 + Config: ` terraform { experiments = [module_variable_optional_attrs] } `, + }, }, - }, + }) }) } @@ -71,18 +72,20 @@ func Test_SkipAbove_Higher(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.7.1") - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -96,18 +99,20 @@ func Test_SkipAbove_Prerelease_EqualCoreVersion(t *testing.T) { //nolint:paralle // core versions. // // Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303 - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -115,18 +120,20 @@ func Test_SkipAbove_Prerelease_EqualPrerelease(t *testing.T) { //nolint:parallel t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0-rc1") - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0-rc1"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0-rc1"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -138,18 +145,20 @@ func Test_SkipAbove_Prerelease_HigherCoreVersion(t *testing.T) { //nolint:parall // 1.8.0 core version. This intentionally verifies that the logic does not // ignore the core version of the prerelease version when compared against // the core version of the check. - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -159,18 +168,20 @@ func Test_SkipAbove_Prerelease_HigherPrerelease(t *testing.T) { //nolint:paralle // The 1.7.0-rc1 prerelease should always be considered to be // below the 1.7.0-rc2 prerelease. - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0-rc2"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0-rc2"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -180,18 +191,20 @@ func Test_SkipAbove_Prerelease_LowerCoreVersion(t *testing.T) { //nolint:paralle // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.7.0 core version. - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.7.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -201,17 +214,19 @@ func Test_SkipAbove_Prerelease_LowerPrerelease(t *testing.T) { //nolint:parallel // The 1.8.0-rc1 prerelease should always be considered to be // above the 1.8.0-beta1 prerelease. - r.UnitTest(t, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0-beta1"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, - }, - }, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipAbove(version.Must(version.NewVersion("1.8.0-beta1"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } diff --git a/tfversion/skip_between_test.go b/tfversion/skip_between_test.go index 35ced527..564cb14d 100644 --- a/tfversion/skip_between_test.go +++ b/tfversion/skip_between_test.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_SkipBetween_SkipTest(t *testing.T) { //nolint:paralleltest @@ -59,18 +59,20 @@ func Test_SkipBetween_RunTest_AboveMax(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.3.0") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipBetween(version.Must(version.NewVersion("1.2.0")), version.Must(version.NewVersion("1.3.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBetween(version.Must(version.NewVersion("1.2.0")), version.Must(version.NewVersion("1.3.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -78,18 +80,20 @@ func Test_SkipBetween_RunTest_EqToMin(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.2.0") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipBetween(version.Must(version.NewVersion("1.2.0")), version.Must(version.NewVersion("1.3.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBetween(version.Must(version.NewVersion("1.2.0")), version.Must(version.NewVersion("1.3.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } diff --git a/tfversion/skip_if_not_alpha_test.go b/tfversion/skip_if_not_alpha_test.go index f778968a..9017c983 100644 --- a/tfversion/skip_if_not_alpha_test.go +++ b/tfversion/skip_if_not_alpha_test.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_SkipIfNotAlpha_SkipTest_Stable(t *testing.T) { //nolint:paralleltest @@ -78,17 +78,19 @@ func Test_SkipIfNotAlpha_RunTest_Alpha(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.9.0-alpha20240501") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipIfNotAlpha(), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipIfNotAlpha(), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } diff --git a/tfversion/skip_if_not_prerelease_test.go b/tfversion/skip_if_not_prerelease_test.go index 545017ed..eda8f4ce 100644 --- a/tfversion/skip_if_not_prerelease_test.go +++ b/tfversion/skip_if_not_prerelease_test.go @@ -13,7 +13,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_SkipIfNotPrerelease_SkipTest_Stable(t *testing.T) { //nolint:paralleltest @@ -41,18 +41,20 @@ func Test_SkipIfNotPrerelease_RunTest_Alpha(t *testing.T) { //nolint:paralleltes t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.9.0-alpha20240501") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipIfNotPrerelease(), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipIfNotPrerelease(), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } @@ -60,35 +62,39 @@ func Test_SkipIfNotPrerelease_RunTest_Beta1(t *testing.T) { //nolint:paralleltes t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0-beta1") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipIfNotPrerelease(), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipIfNotPrerelease(), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } func Test_SkipIfNotPrerelease_RunTest_RC(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0-rc2") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipIfNotPrerelease(), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipIfNotPrerelease(), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } diff --git a/tfversion/skip_if_test.go b/tfversion/skip_if_test.go index 5b645b95..ca9d3834 100644 --- a/tfversion/skip_if_test.go +++ b/tfversion/skip_if_test.go @@ -14,7 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" "github.com/hashicorp/terraform-plugin-testing/tfversion" - testinginterface "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) func Test_SkipIf_SkipTest(t *testing.T) { //nolint:paralleltest @@ -42,18 +42,20 @@ func Test_SkipIf_RunTest(t *testing.T) { //nolint:paralleltest t.Setenv("TF_ACC_TERRAFORM_PATH", "") t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0") - r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{ - ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ - "test": providerserver.NewProviderServer(testprovider.Provider{}), - }, - TerraformVersionChecks: []tfversion.TerraformVersionCheck{ - tfversion.SkipIf(version.Must(version.NewVersion("1.2.0"))), - }, - Steps: []r.TestStep{ - { - Config: `//non-empty config`, + testingiface.ExpectSkip(t, func(mockT *testingiface.MockT) { + r.UnitTest(mockT, r.TestCase{ + ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ + "test": providerserver.NewProviderServer(testprovider.Provider{}), }, - }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipIf(version.Must(version.NewVersion("1.2.0"))), + }, + Steps: []r.TestStep{ + { + Config: `//non-empty config`, + }, + }, + }) }) } From 36b802fe3278b060050e10410d5a28fb3fa2a71b Mon Sep 17 00:00:00 2001 From: Baraa Basata Date: Thu, 19 Dec 2024 09:08:02 -0500 Subject: [PATCH 3/3] wip --- helper/logging/logging.go | 6 ++--- helper/resource/plan_checks.go | 4 ++-- helper/resource/plugin.go | 4 ++-- helper/resource/state_checks.go | 4 ++-- internal/plugintest/util.go | 40 --------------------------------- 5 files changed, 9 insertions(+), 49 deletions(-) diff --git a/helper/logging/logging.go b/helper/logging/logging.go index c13453e4..c462eb0f 100644 --- a/helper/logging/logging.go +++ b/helper/logging/logging.go @@ -12,7 +12,7 @@ import ( "syscall" "github.com/hashicorp/logutils" - "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) // These are the environmental variables that determine if we log, and if @@ -33,7 +33,7 @@ var ValidLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"} // logging controlled by Terraform itself and managed with the TF_ACC_LOG_PATH // environment variable. Calls to tflog.* will have their output managed by the // tfsdklog sink. -func LogOutput(t testing.T) (logOutput io.Writer, err error) { +func LogOutput(t testingiface.T) (logOutput io.Writer, err error) { logOutput = io.Discard logLevel := LogLevel() @@ -91,7 +91,7 @@ func LogOutput(t testing.T) (logOutput io.Writer, err error) { // SetOutput checks for a log destination with LogOutput, and calls // log.SetOutput with the result. If LogOutput returns nil, SetOutput uses // io.Discard. Any error from LogOutout is fatal. -func SetOutput(t testing.T) { +func SetOutput(t testingiface.T) { out, err := LogOutput(t) if err != nil { log.Fatal(err) diff --git a/helper/resource/plan_checks.go b/helper/resource/plan_checks.go index c64c02ba..f6a0d52c 100644 --- a/helper/resource/plan_checks.go +++ b/helper/resource/plan_checks.go @@ -8,11 +8,11 @@ import ( "errors" tfjson "github.com/hashicorp/terraform-json" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" "github.com/hashicorp/terraform-plugin-testing/plancheck" - "github.com/mitchellh/go-testing-interface" ) -func runPlanChecks(ctx context.Context, t testing.T, plan *tfjson.Plan, planChecks []plancheck.PlanCheck) error { +func runPlanChecks(ctx context.Context, t testingiface.T, plan *tfjson.Plan, planChecks []plancheck.PlanCheck) error { t.Helper() var result []error diff --git a/helper/resource/plugin.go b/helper/resource/plugin.go index 5c92f3ab..6e7ac997 100644 --- a/helper/resource/plugin.go +++ b/helper/resource/plugin.go @@ -17,10 +17,10 @@ import ( "github.com/hashicorp/terraform-plugin-go/tfprotov6" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" - "github.com/mitchellh/go-testing-interface" "github.com/hashicorp/terraform-plugin-testing/internal/logging" "github.com/hashicorp/terraform-plugin-testing/internal/plugintest" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" ) // protov5ProviderFactory is a function which is called to start a protocol @@ -113,7 +113,7 @@ type providerFactories struct { protov6 protov6ProviderFactories } -func runProviderCommand(ctx context.Context, t testing.T, f func() error, wd *plugintest.WorkingDir, factories *providerFactories) error { +func runProviderCommand(ctx context.Context, t testingiface.T, f func() error, wd *plugintest.WorkingDir, factories *providerFactories) error { // don't point to this as a test failure location // point to whatever called it t.Helper() diff --git a/helper/resource/state_checks.go b/helper/resource/state_checks.go index 66c850ea..70f12d1e 100644 --- a/helper/resource/state_checks.go +++ b/helper/resource/state_checks.go @@ -8,12 +8,12 @@ import ( "errors" tfjson "github.com/hashicorp/terraform-json" - "github.com/mitchellh/go-testing-interface" + "github.com/hashicorp/terraform-plugin-testing/internal/testingiface" "github.com/hashicorp/terraform-plugin-testing/statecheck" ) -func runStateChecks(ctx context.Context, t testing.T, state *tfjson.State, stateChecks []statecheck.StateCheck) error { +func runStateChecks(ctx context.Context, t testingiface.T, state *tfjson.State, stateChecks []statecheck.StateCheck) error { t.Helper() var result []error diff --git a/internal/plugintest/util.go b/internal/plugintest/util.go index 8f220dd1..830be3e4 100644 --- a/internal/plugintest/util.go +++ b/internal/plugintest/util.go @@ -11,7 +11,6 @@ import ( "path" "path/filepath" "strings" - "testing" ) func symlinkFile(src string, dest string) error { @@ -145,42 +144,3 @@ func CopyDir(src, dest, baseDirName string) error { return nil } - -// TestExpectTFatal provides a wrapper for logic which should call -// (*testing.T).Fatal() or (*testing.T).Fatalf(). -// -// Since we do not want the wrapping test to fail when an expected test error -// occurs, it is required that the testLogic passed in uses -// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real ** -// *testing.T. -// -// If Fatal() or Fatalf() is not called in the logic, the real (*testing.T).Fatal() will -// be called to fail the test. -func TestExpectTFatal(t *testing.T, testLogic func()) { - t.Helper() - - var recoverIface interface{} - - func() { - defer func() { - recoverIface = recover() - }() - - testLogic() - }() - - if recoverIface == nil { - t.Fatalf("expected t.Fatal(), got none") - } - - recoverStr, ok := recoverIface.(string) - - if !ok { - t.Fatalf("expected string from recover(), got: %v (%T)", recoverIface, recoverIface) - } - - // this string is hardcoded in github.com/mitchellh/go-testing-interface - if !strings.HasPrefix(recoverStr, "testing.T failed, see logs for output") { - t.Fatalf("expected t.Fatal(), got: %s", recoverStr) - } -}