Skip to content

Commit d5f3a05

Browse files
authored
Merge pull request #402 from hashicorp/v2-mockT-fatal
Port mockT panic pattern to V2
2 parents 8f1c991 + 5eca13b commit d5f3a05

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

helper/resource/testing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ func Test(t TestT, c TestCase) {
537537
// We require verbose mode so that the user knows what is going on.
538538
if !testing.Verbose() && !c.IsUnitTest {
539539
t.Fatal("Acceptance tests must be run with the -v flag on tests")
540-
return
541540
}
542541

543542
// Run the PreCheck if we have it
@@ -561,7 +560,6 @@ func Test(t TestT, c TestCase) {
561560

562561
if acctest.TestHelper == nil {
563562
t.Fatal("Please configure the acctest binary driver")
564-
return
565563
}
566564

567565
RunNewTest(t.(*testing.T), c, providers)

helper/resource/testing_test.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"reflect"
9-
"regexp"
109
"sort"
1110
"strings"
1211
"testing"
@@ -30,7 +29,18 @@ func init() {
3029

3130
func TestParallelTest(t *testing.T) {
3231
mt := new(mockT)
33-
ParallelTest(mt, TestCase{})
32+
33+
// the test will error because the binary driver is unconfigured
34+
func() {
35+
defer func() {
36+
if r := recover(); r != nil {
37+
if !strings.HasPrefix(r.(string), "mockT") {
38+
panic(r)
39+
}
40+
}
41+
}()
42+
ParallelTest(mt, TestCase{IsUnitTest: true})
43+
}()
3444

3545
if !mt.ParallelCalled {
3646
t.Fatal("Parallel() not called")
@@ -43,21 +53,27 @@ func TestTest_factoryError(t *testing.T) {
4353
factory := func() (*schema.Provider, error) {
4454
return nil, resourceFactoryError
4555
}
46-
4756
mt := new(mockT)
48-
Test(mt, TestCase{
49-
ProviderFactories: map[string]func() (*schema.Provider, error){
50-
"test": factory,
51-
},
52-
Steps: []TestStep{
53-
{
54-
ExpectError: regexp.MustCompile("resource factory error"),
57+
58+
func() {
59+
defer func() {
60+
if r := recover(); r != nil {
61+
if !strings.HasPrefix(r.(string), "mockT") {
62+
panic(r)
63+
}
64+
}
65+
}()
66+
Test(mt, TestCase{
67+
ProviderFactories: map[string]func() (*schema.Provider, error){
68+
"test": factory,
5569
},
56-
},
57-
})
70+
IsUnitTest: true,
71+
})
72+
}()
5873

59-
if !mt.failed() {
60-
t.Fatal("test should've failed")
74+
fatalStr := fmt.Sprint(mt.FatalArgs)
75+
if !strings.Contains(fatalStr, resourceFactoryError.Error()) {
76+
t.Fatalf("test should've failed with %s, failed with %s", resourceFactoryError, fatalStr)
6177
}
6278
}
6379

@@ -161,6 +177,8 @@ func (t *mockT) Fatal(args ...interface{}) {
161177
t.FatalCalled = true
162178
t.FatalArgs = args
163179
t.f = true
180+
181+
panic("mockT.Fatal")
164182
}
165183

166184
func (t *mockT) Parallel() {

0 commit comments

Comments
 (0)