Skip to content

Commit

Permalink
remove exposures of testing.T from other places in SDK
Browse files Browse the repository at this point in the history
move interface down a package to prevent a circular import
  • Loading branch information
appilon authored and kmoe committed Apr 23, 2020
1 parent 42eb444 commit acb365c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 33 deletions.
31 changes: 8 additions & 23 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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.
Expand All @@ -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")
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions helper/resource/testing/testing.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 5 additions & 4 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions helper/schema/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions helper/validation/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down

0 comments on commit acb365c

Please sign in to comment.