Skip to content

Commit

Permalink
Refactor tfversion to testingiface
Browse files Browse the repository at this point in the history
  • Loading branch information
bbasata committed Dec 19, 2024
1 parent 4b45ede commit a19f315
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 250 deletions.
3 changes: 1 addition & 2 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"strings"
"time"

"github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

Expand All @@ -31,6 +29,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/internal/addrs"
"github.com/hashicorp/terraform-plugin-testing/internal/logging"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
testing "github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
)

// flagSweep is a flag available when running tests on the command line. It
Expand Down
4 changes: 2 additions & 2 deletions internal/logging/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/hashicorp/terraform-plugin-log/tfsdklog"
helperlogging "github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
testing "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
)

// InitContext creates SDK logger contexts when the provider is running in
Expand All @@ -34,7 +34,7 @@ func InitContext(ctx context.Context) context.Context {
// The standard library log package handling is important as provider code
// under test may be using that package or another logging library outside of
// terraform-plugin-log.
func InitTestContext(ctx context.Context, t testing.T) context.Context {
func InitTestContext(ctx context.Context, t testingiface.T) context.Context {
helperlogging.SetOutput(t)

ctx = tfsdklog.RegisterTestSink(ctx, t)
Expand Down
2 changes: 1 addition & 1 deletion internal/plugintest/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func CopyDir(src, dest, baseDirName string) error {
//
// Since we do not want the wrapping test to fail when an expected test error
// occurs, it is required that the testLogic passed in uses
// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real
// github.com/mitchellh/go-testing-interface.RuntimeT instead of the real **
// *testing.T.
//
// If Fatal() or Fatalf() is not called in the logic, the real (*testing.T).Fatal() will
Expand Down
7 changes: 3 additions & 4 deletions tfversion/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (

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

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

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

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down
7 changes: 3 additions & 4 deletions tfversion/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
testinginterface "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
Expand Down Expand Up @@ -73,8 +72,8 @@ func Test_Any_Error(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down
15 changes: 7 additions & 8 deletions tfversion/require_above_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

testinginterface "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
)

func Test_RequireAbove_Equal(t *testing.T) { //nolint:paralleltest
Expand Down Expand Up @@ -68,8 +67,8 @@ func Test_RequireAbove_Lower(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.0.7")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down Expand Up @@ -139,8 +138,8 @@ func Test_RequireAbove_Prerelease_HigherCoreVersion(t *testing.T) { //nolint:par
// 1.8.0 core version. This intentionally verifies that the logic does not
// ignore the core version of the prerelease version when compared against
// the core version of the check.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand All @@ -162,8 +161,8 @@ func Test_RequireAbove_Prerelease_HigherPrerelease(t *testing.T) { //nolint:para

// The 1.7.0-rc1 prerelease should always be considered to be
// below the 1.7.0-rc2 prerelease.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down
23 changes: 11 additions & 12 deletions tfversion/require_below_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

testinginterface "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
)

func Test_RequireBelow_Equal(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.7.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down Expand Up @@ -69,8 +68,8 @@ func Test_RequireBelow_Higher(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.4.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down Expand Up @@ -98,8 +97,8 @@ func Test_RequireBelow_Prerelease_EqualCoreVersion(t *testing.T) { //nolint:para
// core versions.
//
// Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down Expand Up @@ -165,8 +164,8 @@ func Test_RequireBelow_Prerelease_LowerCoreVersion(t *testing.T) { //nolint:para

// The 1.8.0-rc1 prerelease should always be considered to be
// above the 1.7.0 core version.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand All @@ -188,8 +187,8 @@ func Test_RequireBelow_Prerelease_LowerPrerelease(t *testing.T) { //nolint:paral

// The 1.8.0-rc1 prerelease should always be considered to be
// above the 1.8.0-beta1 prerelease.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down
31 changes: 15 additions & 16 deletions tfversion/require_between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

testinginterface "github.com/mitchellh/go-testing-interface"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"
)

func Test_RequireBetween(t *testing.T) { //nolint:paralleltest
Expand Down Expand Up @@ -60,8 +59,8 @@ func Test_RequireBetween_Error_BelowMin(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand All @@ -82,8 +81,8 @@ func Test_RequireBetween_Error_BelowMin(t *testing.T) { //nolint:paralleltest
func Test_RequireBetween_Error_EqToMax(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.3.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down Expand Up @@ -111,8 +110,8 @@ func Test_RequireBetween_Prerelease_MaxEqualCoreVersion(t *testing.T) { //nolint
// core versions.
//
// Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down Expand Up @@ -184,8 +183,8 @@ func Test_RequireBetween_Prerelease_MinHigherCoreVersion(t *testing.T) { //nolin
// 1.8.0 core version. This intentionally verifies that the logic does not
// ignore the core version of the prerelease version when compared against
// the core version of the check.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down Expand Up @@ -228,8 +227,8 @@ func Test_RequireBetween_Prerelease_MinHigherPrerelease(t *testing.T) { //nolint

// The 1.7.0-rc1 prerelease should always be considered to be
// below the 1.7.0-rc2 prerelease.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand All @@ -251,8 +250,8 @@ func Test_RequireBetween_Prerelease_MaxLowerCoreVersion(t *testing.T) { //nolint

// The 1.8.0-rc1 prerelease should always be considered to be
// above the 1.7.0 core version.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down Expand Up @@ -295,8 +294,8 @@ func Test_RequireBetween_Prerelease_MaxLowerPrerelease(t *testing.T) { //nolint:

// The 1.8.0-rc1 prerelease should always be considered to be
// above the 1.8.0-beta1 prerelease.
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down
12 changes: 5 additions & 7 deletions tfversion/require_not_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-testing/internal/testingiface"

r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

testinginterface "github.com/mitchellh/go-testing-interface"
)

func Test_RequireNot(t *testing.T) { //nolint:paralleltest
Expand All @@ -41,8 +39,8 @@ func Test_RequireNot_Error(t *testing.T) { //nolint:paralleltest
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.1.0")

plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
Expand Down Expand Up @@ -70,8 +68,8 @@ func Test_RequireNot_Prerelease_EqualCoreVersion(t *testing.T) { //nolint:parall
// core versions.
//
// Reference: https://github.com/hashicorp/terraform-plugin-testing/issues/303
plugintest.TestExpectTFatal(t, func() {
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
testingiface.ExpectFail(t, func(mockT *testingiface.MockT) {
r.UnitTest(mockT, r.TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Expand Down
Loading

0 comments on commit a19f315

Please sign in to comment.