From 75cde0c24279e482d3c1e45b69e9fd236ae90592 Mon Sep 17 00:00:00 2001 From: Aleksander Zaruczewski Date: Wed, 10 Jan 2024 13:22:02 +0200 Subject: [PATCH] refactor(acctest): skip option support (#1520) --- internal/acctest/acctest.go | 19 +++++++++++++++++-- .../organization_application_user_test.go | 4 +--- .../organization_group_project_test.go | 4 +--- .../organization_user_group_member_test.go | 9 ++------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index 06e1442d6..957328c2d 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -55,6 +55,9 @@ func GetTestAivenClient() *aiven.Client { // commonTestDependencies is a struct that contains common dependencies that are used by acceptance tests. type commonTestDependencies struct { + // t is the testing.T instance that is used for acceptance tests. + t *testing.T + // isBeta is a flag that indicates whether the provider is in beta mode. isBeta bool // organizationName is the name of the organization that is used for acceptance tests. @@ -64,7 +67,12 @@ type commonTestDependencies struct { } // IsBeta returns a flag that indicates whether the provider is in beta mode. -func (d *commonTestDependencies) IsBeta() bool { +// If skip is true, then this function will skip the test if the provider is not in beta mode. +func (d *commonTestDependencies) IsBeta(skip bool) bool { + if skip && !d.isBeta { + d.t.Skip(ErrMustSetBetaEnvVar) + } + return d.isBeta } @@ -74,7 +82,12 @@ func (d *commonTestDependencies) OrganizationName() string { } // OrganizationUserID returns the ID of the organization user that is used for acceptance tests. -func (d *commonTestDependencies) OrganizationUserID() *string { +// If skip is true, then this function will skip the test if the organization user ID is not set. +func (d *commonTestDependencies) OrganizationUserID(skip bool) *string { + if skip && d.organizationUserID == nil { + d.t.Skip(ErrMustSetOrganizationUserIDEnvVar) + } + return d.organizationUserID } @@ -95,6 +108,8 @@ func CommonTestDependencies(t *testing.T) *commonTestDependencies { } deps := &commonTestDependencies{ + t: t, + isBeta: util.IsBeta(), } diff --git a/internal/plugin/service/organization/organization_application_user_test.go b/internal/plugin/service/organization/organization_application_user_test.go index 03bef1c65..6a2c705fb 100644 --- a/internal/plugin/service/organization/organization_application_user_test.go +++ b/internal/plugin/service/organization/organization_application_user_test.go @@ -17,9 +17,7 @@ import ( func TestAccOrganizationApplicationUserResourceDataSource(t *testing.T) { deps := acc.CommonTestDependencies(t) - if !deps.IsBeta() { - t.Skip(acc.ErrMustSetBetaEnvVar) - } + deps.IsBeta(true) name := "aiven_organization_application_user.foo" dname := "data.aiven_organization_application_user.foo" diff --git a/internal/plugin/service/organization/organization_group_project_test.go b/internal/plugin/service/organization/organization_group_project_test.go index 2903d94e3..5cb82fb9e 100644 --- a/internal/plugin/service/organization/organization_group_project_test.go +++ b/internal/plugin/service/organization/organization_group_project_test.go @@ -16,9 +16,7 @@ import ( func TestAccOrganizationGroupProject(t *testing.T) { deps := acc.CommonTestDependencies(t) - if !deps.IsBeta() { - t.Skip(acc.ErrMustSetBetaEnvVar) - } + _ = deps.IsBeta(true) name := "aiven_organization_group_project.foo" diff --git a/internal/plugin/service/organization/organization_user_group_member_test.go b/internal/plugin/service/organization/organization_user_group_member_test.go index c139087cc..8854a35bd 100644 --- a/internal/plugin/service/organization/organization_user_group_member_test.go +++ b/internal/plugin/service/organization/organization_user_group_member_test.go @@ -16,14 +16,9 @@ import ( func TestAccOrganizationUserGroupMember(t *testing.T) { deps := acc.CommonTestDependencies(t) - if !deps.IsBeta() { - t.Skip(acc.ErrMustSetBetaEnvVar) - } + _ = deps.IsBeta(true) - userID := deps.OrganizationUserID() - if userID == nil { - t.Skip(acc.ErrMustSetOrganizationUserIDEnvVar) - } + userID := deps.OrganizationUserID(true) name := "aiven_organization_user_group_member.foo"