From 4fe00d00fa120704fe73e89c75f29512cf6c8b09 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Mon, 23 Mar 2020 10:38:47 +0100 Subject: [PATCH] sanity: support embedding tests multiple time in Ginkgo, II This reverts commit ddf8b3f0982c83ad64cc5ff403874b4eab1dd985. It was meant to enable the call sequence DescribeSanity, RegisterTestsInGinkgo, DescribeSanity, RegisterTestsInGinkgo, but that call sequence is invalid: DescribeSanity must be called during the init phase, then RegisterTestsInGinkgo may be called multiple times with different configs. That usage got broken by the commit above because the second RegisterTestsInGinkgo then didn't add any of the tests defined by the sanity package. Users who want to add their own sanity tests can do it like this: Describe( ... func() { config := &sanity.TestConfig{...} Describe(..., func() { sc := NewTestContext(config) BeforeEach(func() { sc.Setup() }) AfterEach(func() { sc.Teardown() }) Describe(..., func() { ... }) }) sanity.GinkgoTest(config) }) NewTestContext must be made public for this to work. Otherwise TestContext has be initialized manually, which isn't as nice. --- pkg/sanity/sanity.go | 10 +++------- pkg/sanity/tests.go | 4 ---- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 913e0710..06047e14 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -200,10 +200,10 @@ func NewTestConfig() TestConfig { } } -// newContext sets up sanity testing with a config supplied by the +// NewContext sets up sanity testing with a config supplied by the // user of the sanity package. Ownership of that config is shared // between the sanity package and the caller. -func newTestContext(config *TestConfig) *TestContext { +func NewTestContext(config *TestConfig) *TestContext { return &TestContext{ Config: config, } @@ -231,12 +231,8 @@ func Test(t GinkgoTestingT, config TestConfig) { // GinkgoTest for use when the tests run. Therefore its content can // still be modified in a BeforeEach. The sanity package itself treats // it as read-only. -// -// Only tests defined with DescribeSanity after the last invocation with -// GinkgoTest (if there has be one) will be added, i.e. each test only -// gets added at most once. func GinkgoTest(config *TestConfig) *TestContext { - sc := newTestContext(config) + sc := NewTestContext(config) registerTestsInGinkgo(sc) return sc } diff --git a/pkg/sanity/tests.go b/pkg/sanity/tests.go index e49aa7a4..92f2fe57 100644 --- a/pkg/sanity/tests.go +++ b/pkg/sanity/tests.go @@ -53,8 +53,4 @@ func registerTestsInGinkgo(sc *TestContext) { }) }) } - // Don't register tests more than once! More tests might - // be added later in a different context, followed by - // another registerTestsInGinkgo call. - tests = nil }