Skip to content

Commit 27ef1bd

Browse files
committed
refactor mockT to testingiface.MockT
1 parent 4b45ede commit 27ef1bd

File tree

12 files changed

+88
-158
lines changed

12 files changed

+88
-158
lines changed

helper/resource/testcase_providers_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1717

18-
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
1918
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
2019
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
20+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
2121
"github.com/hashicorp/terraform-plugin-testing/tfversion"
2222
)
2323

@@ -344,8 +344,8 @@ func TestTest_TestCase_ExternalProvidersAndProviderFactories_NonHashiCorpNamespa
344344
func TestTest_TestCase_ExternalProviders_Error(t *testing.T) {
345345
t.Parallel()
346346

347-
plugintest.TestExpectTFatal(t, func() {
348-
Test(&mockT{}, TestCase{
347+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
348+
Test(mockT, TestCase{
349349
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
350350
tfversion.SkipBelow(tfversion.Version0_13_0), // ExternalProvider.Source
351351
},
@@ -366,7 +366,7 @@ func TestTest_TestCase_ExternalProviders_Error(t *testing.T) {
366366
func TestTest_TestCase_ProtoV5ProviderFactories(t *testing.T) {
367367
t.Parallel()
368368

369-
Test(&mockT{}, TestCase{
369+
Test(t, TestCase{
370370
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
371371
"test": providerserver.NewProtov5ProviderServer(testprovider.Protov5Provider{}),
372372
},
@@ -381,8 +381,8 @@ func TestTest_TestCase_ProtoV5ProviderFactories(t *testing.T) {
381381
func TestTest_TestCase_ProtoV5ProviderFactories_Error(t *testing.T) {
382382
t.Parallel()
383383

384-
plugintest.TestExpectTFatal(t, func() {
385-
Test(&mockT{}, TestCase{
384+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
385+
Test(mockT, TestCase{
386386
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
387387
"test": func() (tfprotov5.ProviderServer, error) { //nolint:unparam // required signature
388388
return nil, fmt.Errorf("test")
@@ -400,7 +400,7 @@ func TestTest_TestCase_ProtoV5ProviderFactories_Error(t *testing.T) {
400400
func TestTest_TestCase_ProtoV6ProviderFactories(t *testing.T) {
401401
t.Parallel()
402402

403-
Test(&mockT{}, TestCase{
403+
Test(t, TestCase{
404404
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
405405
"test": providerserver.NewProviderServer(testprovider.Provider{}),
406406
},
@@ -415,8 +415,8 @@ func TestTest_TestCase_ProtoV6ProviderFactories(t *testing.T) {
415415
func TestTest_TestCase_ProtoV6ProviderFactories_Error(t *testing.T) {
416416
t.Parallel()
417417

418-
plugintest.TestExpectTFatal(t, func() {
419-
Test(&mockT{}, TestCase{
418+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
419+
Test(mockT, TestCase{
420420
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
421421
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
422422
return nil, fmt.Errorf("test")
@@ -434,7 +434,7 @@ func TestTest_TestCase_ProtoV6ProviderFactories_Error(t *testing.T) {
434434
func TestTest_TestCase_ProviderFactories(t *testing.T) {
435435
t.Parallel()
436436

437-
Test(&mockT{}, TestCase{
437+
Test(t, TestCase{
438438
ProviderFactories: map[string]func() (*schema.Provider, error){
439439
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
440440
return &schema.Provider{}, nil
@@ -451,8 +451,8 @@ func TestTest_TestCase_ProviderFactories(t *testing.T) {
451451
func TestTest_TestCase_ProviderFactories_Error(t *testing.T) {
452452
t.Parallel()
453453

454-
plugintest.TestExpectTFatal(t, func() {
455-
Test(&mockT{}, TestCase{
454+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
455+
Test(mockT, TestCase{
456456
ProviderFactories: map[string]func() (*schema.Provider, error){
457457
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
458458
return nil, fmt.Errorf("test")
@@ -470,7 +470,7 @@ func TestTest_TestCase_ProviderFactories_Error(t *testing.T) {
470470
func TestTest_TestCase_Providers(t *testing.T) {
471471
t.Parallel()
472472

473-
Test(&mockT{}, TestCase{
473+
Test(t, TestCase{
474474
Providers: map[string]*schema.Provider{
475475
"test": {},
476476
},

helper/resource/testing_test.go

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
"testing"
1515

1616
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17-
testinginterface "github.com/mitchellh/go-testing-interface"
1817

18+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
1919
"github.com/hashicorp/terraform-plugin-testing/terraform"
2020
)
2121

@@ -28,25 +28,21 @@ func init() {
2828
func TestParallelTest(t *testing.T) {
2929
t.Parallel()
3030

31-
mt := new(mockT)
32-
33-
ParallelTest(mt, TestCase{
34-
IsUnitTest: true,
35-
ProviderFactories: map[string]func() (*schema.Provider, error){
36-
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
37-
return &schema.Provider{}, nil
31+
testingiface.ExpectParallel(t, func(mockT *testingiface.MockT) {
32+
ParallelTest(mockt, TestCase{
33+
IsUnitTest: true,
34+
ProviderFactories: map[string]func() (*schema.Provider, error){
35+
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
36+
return &schema.Provider{}, nil
37+
},
3838
},
39-
},
40-
Steps: []TestStep{
41-
{
42-
Config: "# not empty",
39+
Steps: []TestStep{
40+
{
41+
Config: "# not empty",
42+
},
4343
},
44-
},
44+
})
4545
})
46-
47-
if !mt.ParallelCalled {
48-
t.Fatal("Parallel() not called")
49-
}
5046
}
5147

5248
func TestComposeAggregateTestCheckFunc(t *testing.T) {
@@ -131,21 +127,6 @@ func TestComposeTestCheckFunc(t *testing.T) {
131127
}
132128
}
133129

134-
// mockT implements TestT for testing
135-
type mockT struct {
136-
testinginterface.RuntimeT
137-
138-
ParallelCalled bool
139-
}
140-
141-
func (t *mockT) Parallel() {
142-
t.ParallelCalled = true
143-
}
144-
145-
func (t *mockT) Name() string {
146-
return "MockedName"
147-
}
148-
149130
func TestTest_Main(t *testing.T) {
150131
t.Parallel()
151132

helper/resource/teststep_providers_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
3030
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
3131
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
32+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
3233
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
3334
"github.com/hashicorp/terraform-plugin-testing/terraform"
3435
"github.com/hashicorp/terraform-plugin-testing/tfversion"
@@ -1946,8 +1947,8 @@ func TestTest_TestStep_ExternalProviders_DifferentVersions(t *testing.T) {
19461947
func TestTest_TestStep_ExternalProviders_Error(t *testing.T) {
19471948
t.Parallel()
19481949

1949-
plugintest.TestExpectTFatal(t, func() {
1950-
Test(&mockT{}, TestCase{
1950+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
1951+
Test(mockT, TestCase{
19511952
Steps: []TestStep{
19521953
{
19531954
Config: "# not empty",
@@ -2334,7 +2335,7 @@ func extractResourceAttr(resourceName string, attributeName string, attributeVal
23342335
func TestTest_TestStep_ProtoV5ProviderFactories(t *testing.T) {
23352336
t.Parallel()
23362337

2337-
UnitTest(&mockT{}, TestCase{
2338+
UnitTest(t, TestCase{
23382339
Steps: []TestStep{
23392340
{
23402341
Config: "# not empty",
@@ -2349,8 +2350,8 @@ func TestTest_TestStep_ProtoV5ProviderFactories(t *testing.T) {
23492350
func TestTest_TestStep_ProtoV5ProviderFactories_Error(t *testing.T) {
23502351
t.Parallel()
23512352

2352-
plugintest.TestExpectTFatal(t, func() {
2353-
UnitTest(&mockT{}, TestCase{
2353+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
2354+
UnitTest(mockT, TestCase{
23542355
Steps: []TestStep{
23552356
{
23562357
Config: "# not empty",
@@ -2368,7 +2369,7 @@ func TestTest_TestStep_ProtoV5ProviderFactories_Error(t *testing.T) {
23682369
func TestTest_TestStep_ProtoV6ProviderFactories(t *testing.T) {
23692370
t.Parallel()
23702371

2371-
UnitTest(&mockT{}, TestCase{
2372+
UnitTest(t, TestCase{
23722373
Steps: []TestStep{
23732374
{
23742375
Config: "# not empty",
@@ -2383,8 +2384,8 @@ func TestTest_TestStep_ProtoV6ProviderFactories(t *testing.T) {
23832384
func TestTest_TestStep_ProtoV6ProviderFactories_Error(t *testing.T) {
23842385
t.Parallel()
23852386

2386-
plugintest.TestExpectTFatal(t, func() {
2387-
UnitTest(&mockT{}, TestCase{
2387+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
2388+
UnitTest(mockT, TestCase{
23882389
Steps: []TestStep{
23892390
{
23902391
Config: "# not empty",
@@ -2465,7 +2466,7 @@ func TestTest_TestStep_ProtoV6ProviderFactories_To_ExternalProviders(t *testing.
24652466
func TestTest_TestStep_ProviderFactories(t *testing.T) {
24662467
t.Parallel()
24672468

2468-
UnitTest(&mockT{}, TestCase{
2469+
UnitTest(t, TestCase{
24692470
Steps: []TestStep{
24702471
{
24712472
Config: "# not empty",
@@ -2482,8 +2483,8 @@ func TestTest_TestStep_ProviderFactories(t *testing.T) {
24822483
func TestTest_TestStep_ProviderFactories_Error(t *testing.T) {
24832484
t.Parallel()
24842485

2485-
plugintest.TestExpectTFatal(t, func() {
2486-
UnitTest(&mockT{}, TestCase{
2486+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
2487+
UnitTest(mockT, TestCase{
24872488
Steps: []TestStep{
24882489
{
24892490
Config: "# not empty",

helper/resource/teststep_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
1010
"github.com/hashicorp/terraform-plugin-go/tftypes"
11-
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
1211
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
1312
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
1413
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource"
14+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
1515
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1616
)
1717

@@ -111,8 +111,8 @@ func TestTestStep_ImportStateVerifyIdentifierAttribute(t *testing.T) {
111111
func TestTestStep_ImportStateVerifyIdentifierAttribute_Error(t *testing.T) {
112112
t.Parallel()
113113

114-
plugintest.TestExpectTFatal(t, func() {
115-
Test(&mockT{}, TestCase{
114+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
115+
Test(mockT, TestCase{
116116
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
117117
tfversion.SkipBelow(tfversion.Version1_0_0), // ProtoV6ProviderFactories
118118
},

helper/resource/tfversion_checks_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99

1010
"github.com/hashicorp/go-version"
1111

12-
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
1313
"github.com/hashicorp/terraform-plugin-testing/tfversion"
14-
15-
testinginterface "github.com/mitchellh/go-testing-interface"
1614
)
1715

1816
func TestRunTFVersionChecks(t *testing.T) {
@@ -54,8 +52,8 @@ func TestRunTFVersionChecks(t *testing.T) {
5452
t.Parallel()
5553

5654
if test.expectError {
57-
plugintest.TestExpectTFatal(t, func() {
58-
runTFVersionChecks(context.Background(), &testinginterface.RuntimeT{}, test.tfVersion, test.versionChecks)
55+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
56+
runTFVersionChecks(context.Background(), mockT, test.tfVersion, test.versionChecks)
5957
})
6058
} else {
6159
runTFVersionChecks(context.Background(), t, test.tfVersion, test.versionChecks)

internal/plugintest/util.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"path"
1212
"path/filepath"
1313
"strings"
14-
"testing"
1514
)
1615

1716
func symlinkFile(src string, dest string) error {
@@ -145,42 +144,3 @@ func CopyDir(src, dest, baseDirName string) error {
145144

146145
return nil
147146
}
148-
149-
// TestExpectTFatal provides a wrapper for logic which should call
150-
// (*testing.T).Fatal() or (*testing.T).Fatalf().
151-
//
152-
// Since we do not want the wrapping test to fail when an expected test error
153-
// occurs, it is required that the testLogic passed in uses
154-
// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real
155-
// *testing.T.
156-
//
157-
// If Fatal() or Fatalf() is not called in the logic, the real (*testing.T).Fatal() will
158-
// be called to fail the test.
159-
func TestExpectTFatal(t *testing.T, testLogic func()) {
160-
t.Helper()
161-
162-
var recoverIface interface{}
163-
164-
func() {
165-
defer func() {
166-
recoverIface = recover()
167-
}()
168-
169-
testLogic()
170-
}()
171-
172-
if recoverIface == nil {
173-
t.Fatalf("expected t.Fatal(), got none")
174-
}
175-
176-
recoverStr, ok := recoverIface.(string)
177-
178-
if !ok {
179-
t.Fatalf("expected string from recover(), got: %v (%T)", recoverIface, recoverIface)
180-
}
181-
182-
// this string is hardcoded in github.com/mitchellh/go-testing-interface
183-
if !strings.HasPrefix(recoverStr, "testing.T failed, see logs for output") {
184-
t.Fatalf("expected t.Fatal(), got: %s", recoverStr)
185-
}
186-
}

tfversion/all_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88

99
"github.com/hashicorp/go-version"
1010
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11-
testinginterface "github.com/mitchellh/go-testing-interface"
1211

1312
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
14-
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
1513
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
1614
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
15+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
1716
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1817
)
1918

@@ -80,8 +79,8 @@ func Test_All_Error(t *testing.T) { //nolint:paralleltest
8079
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
8180
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0")
8281

83-
plugintest.TestExpectTFatal(t, func() {
84-
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
82+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
83+
r.UnitTest(mockT, r.TestCase{
8584
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
8685
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
8786
return nil, nil

tfversion/any_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import (
88

99
"github.com/hashicorp/go-version"
1010
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11-
testinginterface "github.com/mitchellh/go-testing-interface"
1211

1312
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
14-
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
1513
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
1614
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
15+
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
1716
"github.com/hashicorp/terraform-plugin-testing/tfversion"
1817
)
1918

@@ -73,8 +72,8 @@ func Test_Any_Error(t *testing.T) { //nolint:paralleltest
7372
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
7473
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0")
7574

76-
plugintest.TestExpectTFatal(t, func() {
77-
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
75+
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
76+
r.UnitTest(mockT, r.TestCase{
7877
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
7978
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
8079
return nil, nil

0 commit comments

Comments
 (0)