From 57f4855db92928385d50fd1a2ab68bdd3afee221 Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Mon, 20 Apr 2020 16:37:32 -0400 Subject: [PATCH 1/4] simulate exit of mockT.Fatal with panic/recover remove returns after t.Fatal --- helper/resource/testing.go | 1 - helper/resource/testing_test.go | 24 +++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 18d57edced8..dce9b114fc9 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -537,7 +537,6 @@ func Test(t TestT, c TestCase) { // We require verbose mode so that the user knows what is going on. if !testing.Verbose() && !c.IsUnitTest { t.Fatal("Acceptance tests must be run with the -v flag on tests") - return } // Run the PreCheck if we have it diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index eacc6ae52d2..223285c99bf 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -6,7 +6,6 @@ import ( "fmt" "os" "reflect" - "regexp" "sort" "strings" "testing" @@ -43,18 +42,19 @@ func TestTest_factoryError(t *testing.T) { factory := func() (*schema.Provider, error) { return nil, resourceFactoryError } - mt := new(mockT) - Test(mt, TestCase{ - ProviderFactories: map[string]func() (*schema.Provider, error){ - "test": factory, - }, - Steps: []TestStep{ - { - ExpectError: regexp.MustCompile("resource factory error"), + + func() { + defer func() { + recover() + }() + Test(mt, TestCase{ + ProviderFactories: map[string]func() (*schema.Provider, error){ + "test": factory, }, - }, - }) + IsUnitTest: true, + }) + }() if !mt.failed() { t.Fatal("test should've failed") @@ -161,6 +161,8 @@ func (t *mockT) Fatal(args ...interface{}) { t.FatalCalled = true t.FatalArgs = args t.f = true + + panic("mockT.Fatal") } func (t *mockT) Parallel() { From b41c06ece7e2c5599dd80d6bba9d17e25933b345 Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Wed, 22 Apr 2020 11:46:59 -0400 Subject: [PATCH 2/4] remove return after fatal --- helper/resource/testing.go | 1 - 1 file changed, 1 deletion(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index dce9b114fc9..14d694ab2e4 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -560,7 +560,6 @@ func Test(t TestT, c TestCase) { if acctest.TestHelper == nil { t.Fatal("Please configure the acctest binary driver") - return } RunNewTest(t.(*testing.T), c, providers) From 47777b2d8b0ff70435370608f36e41ba91bfb018 Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Wed, 22 Apr 2020 11:05:29 -0400 Subject: [PATCH 3/4] ensure panic comes from mockT --- helper/resource/testing_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 223285c99bf..c917f6110ec 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -46,7 +46,11 @@ func TestTest_factoryError(t *testing.T) { func() { defer func() { - recover() + if r := recover(); r != nil { + if !strings.HasPrefix(r.(string), "mockT") { + panic(r) + } + } }() Test(mt, TestCase{ ProviderFactories: map[string]func() (*schema.Provider, error){ From 5eca13b35b002b198b016574678cab370312e78c Mon Sep 17 00:00:00 2001 From: Alex Pilon Date: Wed, 22 Apr 2020 12:55:39 -0400 Subject: [PATCH 4/4] adjust tests for v2 test setup --- helper/resource/testing_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index c917f6110ec..bd264cc5fac 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -29,7 +29,18 @@ func init() { func TestParallelTest(t *testing.T) { mt := new(mockT) - ParallelTest(mt, TestCase{}) + + // the test will error because the binary driver is unconfigured + func() { + defer func() { + if r := recover(); r != nil { + if !strings.HasPrefix(r.(string), "mockT") { + panic(r) + } + } + }() + ParallelTest(mt, TestCase{IsUnitTest: true}) + }() if !mt.ParallelCalled { t.Fatal("Parallel() not called") @@ -60,8 +71,9 @@ func TestTest_factoryError(t *testing.T) { }) }() - if !mt.failed() { - t.Fatal("test should've failed") + fatalStr := fmt.Sprint(mt.FatalArgs) + if !strings.Contains(fatalStr, resourceFactoryError.Error()) { + t.Fatalf("test should've failed with %s, failed with %s", resourceFactoryError, fatalStr) } }