From acb365ca940775e5c60096722497ea7f69261900 Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Tue, 21 Apr 2020 16:53:07 -0400 Subject: [PATCH] remove exposures of testing.T from other places in SDK move interface down a package to prevent a circular import --- helper/resource/testing.go | 31 ++++++--------------- helper/resource/testing/testing.go | 24 ++++++++++++++++ helper/resource/testing_new.go | 9 +++--- helper/resource/testing_new_config.go | 3 +- helper/resource/testing_new_import_state.go | 3 +- helper/schema/testing.go | 4 +-- helper/validation/testing.go | 4 +-- 7 files changed, 45 insertions(+), 33 deletions(-) create mode 100644 helper/resource/testing/testing.go diff --git a/helper/resource/testing.go b/helper/resource/testing.go index dc3ee18831..081604f244 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -12,13 +12,14 @@ import ( "regexp" "strings" "syscall" - "testing" + gotesting "testing" "github.com/hashicorp/go-multierror" "github.com/hashicorp/logutils" "github.com/hashicorp/terraform-plugin-sdk/v2/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils" @@ -86,7 +87,7 @@ func AddTestSweepers(name string, s *Sweeper) { sweeperFuncs[name] = s } -func TestMain(m *testing.M) { +func TestMain(m testing.M) { flag.Parse() if *flagSweep != "" { // parse flagSweep contents for regions to run @@ -460,7 +461,7 @@ type TestStep struct { // Set to a file mask in sprintf format where %s is test name const envLogPathMask = "TF_LOG_PATH_MASK" -func logOutput(t TestT) (logOutput io.Writer, err error) { +func logOutput(t testing.T) (logOutput io.Writer, err error) { logOutput = ioutil.Discard logLevel := logging.LogLevel() @@ -506,7 +507,7 @@ func logOutput(t TestT) (logOutput io.Writer, err error) { // Tests will fail if they do not properly handle conditions to allow multiple // tests to occur against the same resource or service (e.g. random naming). // All other requirements of the Test function also apply to this function. -func ParallelTest(t TestT, c TestCase) { +func ParallelTest(t testing.T, c TestCase) { t.Parallel() Test(t, c) } @@ -521,7 +522,7 @@ func ParallelTest(t TestT, c TestCase) { // the "-test.v" flag) is set. Because some acceptance tests take quite // long, we require the verbose flag so users are able to see progress // output. -func Test(t TestT, c TestCase) { +func Test(t testing.T, c TestCase) { // We only run acceptance tests if an env var is set because they're // slow and generally require some outside configuration. You can opt out // of this with OverrideEnvVar on individual TestCases. @@ -539,7 +540,7 @@ func Test(t TestT, c TestCase) { log.SetOutput(logWriter) // We require verbose mode so that the user knows what is going on. - if !testing.Verbose() && !c.IsUnitTest { + if !gotesting.Verbose() && !c.IsUnitTest { t.Fatal("Acceptance tests must be run with the -v flag on tests") } @@ -594,7 +595,7 @@ func testProviderConfig(c TestCase) string { // UnitTest is a helper to force the acceptance testing harness to run in the // normal unit test suite. This should only be used for resource that don't // have any external dependencies. -func UnitTest(t TestT, c TestCase) { +func UnitTest(t testing.T, c TestCase) { c.IsUnitTest = true Test(t, c) } @@ -980,22 +981,6 @@ func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc { } } -// TestT is the interface used to handle the test lifecycle of a test. -// -// Users should just use a *testing.T object, which implements this. -type TestT interface { - Error(args ...interface{}) - FailNow() - Fatal(args ...interface{}) - Fatalf(format string, args ...interface{}) - Helper() - Log(args ...interface{}) - Name() string - Parallel() - Skip(args ...interface{}) - SkipNow() -} - // modulePrimaryInstanceState returns the instance state for the given resource // name in a ModuleState func modulePrimaryInstanceState(s *terraform.State, ms *terraform.ModuleState, name string) (*terraform.InstanceState, error) { diff --git a/helper/resource/testing/testing.go b/helper/resource/testing/testing.go new file mode 100644 index 0000000000..9fb5ac5383 --- /dev/null +++ b/helper/resource/testing/testing.go @@ -0,0 +1,24 @@ +// Package testing aims to expose interfaces that mirror go's testing package +// this prevents go's testing package from being imported or exposed to +// importers of the SDK +package testing + +// T is the interface used to handle the test lifecycle of a test. +// +// Users should just use a *testing.T object, which implements this. +type T interface { + Error(args ...interface{}) + FailNow() + Fatal(args ...interface{}) + Fatalf(format string, args ...interface{}) + Helper() + Log(args ...interface{}) + Name() string + Parallel() + Skip(args ...interface{}) + SkipNow() +} + +type M interface { + Run() int +} diff --git a/helper/resource/testing_new.go b/helper/resource/testing_new.go index a392356ec1..c2373aab0b 100644 --- a/helper/resource/testing_new.go +++ b/helper/resource/testing_new.go @@ -11,11 +11,12 @@ import ( tftest "github.com/hashicorp/terraform-plugin-test" "github.com/hashicorp/terraform-plugin-sdk/v2/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func runPostTestDestroy(t TestT, c TestCase, wd *tftest.WorkingDir) error { +func runPostTestDestroy(t testing.T, c TestCase, wd *tftest.WorkingDir) error { wd.RequireDestroy(t) if c.CheckDestroy != nil { @@ -29,7 +30,7 @@ func runPostTestDestroy(t TestT, c TestCase, wd *tftest.WorkingDir) error { return nil } -func RunNewTest(t TestT, c TestCase, providers map[string]*schema.Provider) { +func RunNewTest(t testing.T, c TestCase, providers map[string]*schema.Provider) { spewConf := spew.NewDefaultConfig() spewConf.SortKeys = true wd := acctest.TestHelper.RequireNewWorkingDir(t) @@ -101,7 +102,7 @@ func RunNewTest(t TestT, c TestCase, providers map[string]*schema.Provider) { } } -func getState(t TestT, wd *tftest.WorkingDir) *terraform.State { +func getState(t testing.T, wd *tftest.WorkingDir) *terraform.State { jsonState := wd.RequireState(t) state, err := shimStateFromJson(jsonState) if err != nil { @@ -131,7 +132,7 @@ func planIsEmpty(plan *tfjson.Plan) bool { return true } -func testIDRefresh(c TestCase, t TestT, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error { +func testIDRefresh(c TestCase, t testing.T, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error { spewConf := spew.NewDefaultConfig() spewConf.SortKeys = true diff --git a/helper/resource/testing_new_config.go b/helper/resource/testing_new_config.go index f6ddec81e3..37cf7baf94 100644 --- a/helper/resource/testing_new_config.go +++ b/helper/resource/testing_new_config.go @@ -4,10 +4,11 @@ import ( "github.com/davecgh/go-spew/spew" tftest "github.com/hashicorp/terraform-plugin-test" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func testStepNewConfig(t TestT, c TestCase, wd *tftest.WorkingDir, step TestStep) error { +func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep) error { spewConf := spew.NewDefaultConfig() spewConf.SortKeys = true diff --git a/helper/resource/testing_new_import_state.go b/helper/resource/testing_new_import_state.go index bf5b6df954..d0b4dfe216 100644 --- a/helper/resource/testing_new_import_state.go +++ b/helper/resource/testing_new_import_state.go @@ -8,12 +8,13 @@ import ( tftest "github.com/hashicorp/terraform-plugin-test" "github.com/hashicorp/terraform-plugin-sdk/v2/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func testStepNewImportState(t TestT, c TestCase, wd *tftest.WorkingDir, step TestStep, cfg string) error { +func testStepNewImportState(t testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep, cfg string) error { spewConf := spew.NewDefaultConfig() spewConf.SortKeys = true diff --git a/helper/schema/testing.go b/helper/schema/testing.go index 260e490364..9609537701 100644 --- a/helper/schema/testing.go +++ b/helper/schema/testing.go @@ -2,14 +2,14 @@ package schema import ( "context" - "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) // TestResourceDataRaw creates a ResourceData from a raw configuration map. func TestResourceDataRaw( - t *testing.T, schema map[string]*Schema, raw map[string]interface{}) *ResourceData { + t testing.T, schema map[string]*Schema, raw map[string]interface{}) *ResourceData { t.Helper() c := terraform.NewResourceConfigRaw(raw) diff --git a/helper/validation/testing.go b/helper/validation/testing.go index a33f450216..ebc2eb7c4a 100644 --- a/helper/validation/testing.go +++ b/helper/validation/testing.go @@ -2,8 +2,8 @@ package validation import ( "regexp" - "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -13,7 +13,7 @@ type testCase struct { expectedErr *regexp.Regexp } -func runTestCases(t *testing.T, cases []testCase) { +func runTestCases(t testing.T, cases []testCase) { matchErr := func(errs []error, r *regexp.Regexp) bool { // err must match one provided for _, err := range errs {