Skip to content

Commit acb365c

Browse files
appilonkmoe
authored andcommitted
remove exposures of testing.T from other places in SDK
move interface down a package to prevent a circular import
1 parent 42eb444 commit acb365c

File tree

7 files changed

+45
-33
lines changed

7 files changed

+45
-33
lines changed

helper/resource/testing.go

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
"regexp"
1313
"strings"
1414
"syscall"
15-
"testing"
15+
gotesting "testing"
1616

1717
"github.com/hashicorp/go-multierror"
1818
"github.com/hashicorp/logutils"
1919

2020
"github.com/hashicorp/terraform-plugin-sdk/v2/acctest"
2121
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
2223
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2324
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
2425
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/diagutils"
@@ -86,7 +87,7 @@ func AddTestSweepers(name string, s *Sweeper) {
8687
sweeperFuncs[name] = s
8788
}
8889

89-
func TestMain(m *testing.M) {
90+
func TestMain(m testing.M) {
9091
flag.Parse()
9192
if *flagSweep != "" {
9293
// parse flagSweep contents for regions to run
@@ -460,7 +461,7 @@ type TestStep struct {
460461
// Set to a file mask in sprintf format where %s is test name
461462
const envLogPathMask = "TF_LOG_PATH_MASK"
462463

463-
func logOutput(t TestT) (logOutput io.Writer, err error) {
464+
func logOutput(t testing.T) (logOutput io.Writer, err error) {
464465
logOutput = ioutil.Discard
465466

466467
logLevel := logging.LogLevel()
@@ -506,7 +507,7 @@ func logOutput(t TestT) (logOutput io.Writer, err error) {
506507
// Tests will fail if they do not properly handle conditions to allow multiple
507508
// tests to occur against the same resource or service (e.g. random naming).
508509
// All other requirements of the Test function also apply to this function.
509-
func ParallelTest(t TestT, c TestCase) {
510+
func ParallelTest(t testing.T, c TestCase) {
510511
t.Parallel()
511512
Test(t, c)
512513
}
@@ -521,7 +522,7 @@ func ParallelTest(t TestT, c TestCase) {
521522
// the "-test.v" flag) is set. Because some acceptance tests take quite
522523
// long, we require the verbose flag so users are able to see progress
523524
// output.
524-
func Test(t TestT, c TestCase) {
525+
func Test(t testing.T, c TestCase) {
525526
// We only run acceptance tests if an env var is set because they're
526527
// slow and generally require some outside configuration. You can opt out
527528
// of this with OverrideEnvVar on individual TestCases.
@@ -539,7 +540,7 @@ func Test(t TestT, c TestCase) {
539540
log.SetOutput(logWriter)
540541

541542
// We require verbose mode so that the user knows what is going on.
542-
if !testing.Verbose() && !c.IsUnitTest {
543+
if !gotesting.Verbose() && !c.IsUnitTest {
543544
t.Fatal("Acceptance tests must be run with the -v flag on tests")
544545
}
545546

@@ -594,7 +595,7 @@ func testProviderConfig(c TestCase) string {
594595
// UnitTest is a helper to force the acceptance testing harness to run in the
595596
// normal unit test suite. This should only be used for resource that don't
596597
// have any external dependencies.
597-
func UnitTest(t TestT, c TestCase) {
598+
func UnitTest(t testing.T, c TestCase) {
598599
c.IsUnitTest = true
599600
Test(t, c)
600601
}
@@ -980,22 +981,6 @@ func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc {
980981
}
981982
}
982983

983-
// TestT is the interface used to handle the test lifecycle of a test.
984-
//
985-
// Users should just use a *testing.T object, which implements this.
986-
type TestT interface {
987-
Error(args ...interface{})
988-
FailNow()
989-
Fatal(args ...interface{})
990-
Fatalf(format string, args ...interface{})
991-
Helper()
992-
Log(args ...interface{})
993-
Name() string
994-
Parallel()
995-
Skip(args ...interface{})
996-
SkipNow()
997-
}
998-
999984
// modulePrimaryInstanceState returns the instance state for the given resource
1000985
// name in a ModuleState
1001986
func modulePrimaryInstanceState(s *terraform.State, ms *terraform.ModuleState, name string) (*terraform.InstanceState, error) {

helper/resource/testing/testing.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Package testing aims to expose interfaces that mirror go's testing package
2+
// this prevents go's testing package from being imported or exposed to
3+
// importers of the SDK
4+
package testing
5+
6+
// T is the interface used to handle the test lifecycle of a test.
7+
//
8+
// Users should just use a *testing.T object, which implements this.
9+
type T interface {
10+
Error(args ...interface{})
11+
FailNow()
12+
Fatal(args ...interface{})
13+
Fatalf(format string, args ...interface{})
14+
Helper()
15+
Log(args ...interface{})
16+
Name() string
17+
Parallel()
18+
Skip(args ...interface{})
19+
SkipNow()
20+
}
21+
22+
type M interface {
23+
Run() int
24+
}

helper/resource/testing_new.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ import (
1111
tftest "github.com/hashicorp/terraform-plugin-test"
1212

1313
"github.com/hashicorp/terraform-plugin-sdk/v2/acctest"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1617
)
1718

18-
func runPostTestDestroy(t TestT, c TestCase, wd *tftest.WorkingDir) error {
19+
func runPostTestDestroy(t testing.T, c TestCase, wd *tftest.WorkingDir) error {
1920
wd.RequireDestroy(t)
2021

2122
if c.CheckDestroy != nil {
@@ -29,7 +30,7 @@ func runPostTestDestroy(t TestT, c TestCase, wd *tftest.WorkingDir) error {
2930
return nil
3031
}
3132

32-
func RunNewTest(t TestT, c TestCase, providers map[string]*schema.Provider) {
33+
func RunNewTest(t testing.T, c TestCase, providers map[string]*schema.Provider) {
3334
spewConf := spew.NewDefaultConfig()
3435
spewConf.SortKeys = true
3536
wd := acctest.TestHelper.RequireNewWorkingDir(t)
@@ -101,7 +102,7 @@ func RunNewTest(t TestT, c TestCase, providers map[string]*schema.Provider) {
101102
}
102103
}
103104

104-
func getState(t TestT, wd *tftest.WorkingDir) *terraform.State {
105+
func getState(t testing.T, wd *tftest.WorkingDir) *terraform.State {
105106
jsonState := wd.RequireState(t)
106107
state, err := shimStateFromJson(jsonState)
107108
if err != nil {
@@ -131,7 +132,7 @@ func planIsEmpty(plan *tfjson.Plan) bool {
131132
return true
132133
}
133134

134-
func testIDRefresh(c TestCase, t TestT, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
135+
func testIDRefresh(c TestCase, t testing.T, wd *tftest.WorkingDir, step TestStep, r *terraform.ResourceState) error {
135136
spewConf := spew.NewDefaultConfig()
136137
spewConf.SortKeys = true
137138

helper/resource/testing_new_config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"github.com/davecgh/go-spew/spew"
55
tftest "github.com/hashicorp/terraform-plugin-test"
66

7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
78
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
89
)
910

10-
func testStepNewConfig(t TestT, c TestCase, wd *tftest.WorkingDir, step TestStep) error {
11+
func testStepNewConfig(t testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep) error {
1112
spewConf := spew.NewDefaultConfig()
1213
spewConf.SortKeys = true
1314

helper/resource/testing_new_import_state.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import (
88
tftest "github.com/hashicorp/terraform-plugin-test"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/addrs"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1415
)
1516

16-
func testStepNewImportState(t TestT, c TestCase, wd *tftest.WorkingDir, step TestStep, cfg string) error {
17+
func testStepNewImportState(t testing.T, c TestCase, wd *tftest.WorkingDir, step TestStep, cfg string) error {
1718
spewConf := spew.NewDefaultConfig()
1819
spewConf.SortKeys = true
1920

helper/schema/testing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package schema
22

33
import (
44
"context"
5-
"testing"
65

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
88
)
99

1010
// TestResourceDataRaw creates a ResourceData from a raw configuration map.
1111
func TestResourceDataRaw(
12-
t *testing.T, schema map[string]*Schema, raw map[string]interface{}) *ResourceData {
12+
t testing.T, schema map[string]*Schema, raw map[string]interface{}) *ResourceData {
1313
t.Helper()
1414

1515
c := terraform.NewResourceConfigRaw(raw)

helper/validation/testing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package validation
22

33
import (
44
"regexp"
5-
"testing"
65

6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
77
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
88
)
99

@@ -13,7 +13,7 @@ type testCase struct {
1313
expectedErr *regexp.Regexp
1414
}
1515

16-
func runTestCases(t *testing.T, cases []testCase) {
16+
func runTestCases(t testing.T, cases []testCase) {
1717
matchErr := func(errs []error, r *regexp.Regexp) bool {
1818
// err must match one provided
1919
for _, err := range errs {

0 commit comments

Comments
 (0)