Skip to content

Commit

Permalink
Use mitchellh/go-testing-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng authored and appilon committed Apr 28, 2020
1 parent 7cfe5b7 commit 675949a
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 93 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mitchellh/copystructure v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/go-testing-interface v1.0.4
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/mitchellh/reflectwalk v1.0.1
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrk
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-testing-interface v1.0.4 h1:ZU1VNC02qyufSZsjjs7+khruk2fKvbQ3TwRV/IBCeFA=
github.com/mitchellh/go-testing-interface v1.0.4/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
Expand Down
6 changes: 4 additions & 2 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/logutils"
testing "github.com/mitchellh/go-testing-interface"

"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 +86,9 @@ func AddTestSweepers(name string, s *Sweeper) {
sweeperFuncs[name] = s
}

func TestMain(m testing.M) {
func TestMain(m interface {
Run() int
}) {
flag.Parse()
if *flagSweep != "" {
// parse flagSweep contents for regions to run
Expand Down
24 changes: 0 additions & 24 deletions helper/resource/testing/testing.go

This file was deleted.

2 changes: 1 addition & 1 deletion helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/davecgh/go-spew/spew"
tfjson "github.com/hashicorp/terraform-json"
tftest "github.com/hashicorp/terraform-plugin-test"
testing "github.com/mitchellh/go-testing-interface"

"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"
)
Expand Down
2 changes: 1 addition & 1 deletion helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resource
import (
"github.com/davecgh/go-spew/spew"
tftest "github.com/hashicorp/terraform-plugin-test"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down
2 changes: 1 addition & 1 deletion helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (

"github.com/davecgh/go-spew/spew"
tftest "github.com/hashicorp/terraform-plugin-test"
testing "github.com/mitchellh/go-testing-interface"

"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"
Expand Down
76 changes: 15 additions & 61 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"github.com/hashicorp/go-multierror"
testinginterface "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
Expand All @@ -33,10 +34,10 @@ func TestParallelTest(t *testing.T) {
// 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)
}
r := recover()
// this string is hardcoded in github.com/mitchellh/go-testing-interface
if s, ok := r.(string); !ok || !strings.HasPrefix(s, "testing.T failed, see logs for output") {
panic(r)
}
}()
ParallelTest(mt, TestCase{IsUnitTest: true})
Expand All @@ -54,14 +55,16 @@ func TestTest_factoryError(t *testing.T) {
return nil, resourceFactoryError
}
mt := new(mockT)
recovered := false

func() {
defer func() {
if r := recover(); r != nil {
if !strings.HasPrefix(r.(string), "mockT") {
panic(r)
}
r := recover()
// this string is hardcoded in github.com/mitchellh/go-testing-interface
if s, ok := r.(string); !ok || !strings.HasPrefix(s, "testing.T failed, see logs for output") {
panic(r)
}
recovered = true
}()
Test(mt, TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
Expand All @@ -71,9 +74,8 @@ func TestTest_factoryError(t *testing.T) {
})
}()

fatalStr := fmt.Sprint(mt.FatalArgs)
if !strings.Contains(fatalStr, resourceFactoryError.Error()) {
t.Fatalf("test should've failed with %s, failed with %s", resourceFactoryError, fatalStr)
if !recovered {
t.Fatalf("test should've failed fatally")
}
}

Expand Down Expand Up @@ -156,67 +158,19 @@ func TestComposeTestCheckFunc(t *testing.T) {

// mockT implements TestT for testing
type mockT struct {
ErrorCalled bool
ErrorArgs []interface{}
FatalCalled bool
FatalArgs []interface{}
ParallelCalled bool
SkipCalled bool
SkipArgs []interface{}

f bool
}

func (t *mockT) Error(args ...interface{}) {
t.ErrorCalled = true
t.ErrorArgs = args
t.f = true
}

func (t *mockT) FailNow() {
t.f = true

panic("mockT.FailNow")
}

func (t *mockT) Fatal(args ...interface{}) {
t.FatalCalled = true
t.FatalArgs = args
t.f = true
testinginterface.RuntimeT

panic("mockT.Fatal")
}

func (t *mockT) Fatalf(format string, args ...interface{}) {
t.Fatal(fmt.Sprintf(format, args...))
ParallelCalled bool
}

func (t *mockT) Helper() {}

func (t *mockT) Log(args ...interface{}) {}

func (t *mockT) Parallel() {
t.ParallelCalled = true
}

func (t *mockT) Skip(args ...interface{}) {
t.SkipCalled = true
t.SkipArgs = args
t.f = true
}

func (t *mockT) SkipNow() {
t.Skip()
}

func (t *mockT) Name() string {
return "MockedName"
}

func (t *mockT) failed() bool {
return t.f
}

func TestTest_Main(t *testing.T) {
flag.Parse()
if *flagSweep == "" {
Expand Down
3 changes: 2 additions & 1 deletion helper/schema/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package schema
import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

Expand Down
3 changes: 2 additions & 1 deletion helper/validation/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package validation
import (
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource/testing"
testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down

0 comments on commit 675949a

Please sign in to comment.