From ddf8b3f0982c83ad64cc5ff403874b4eab1dd985 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Thu, 19 Mar 2020 11:17:13 +0100 Subject: [PATCH] sanity: support embedding tests multiple time in Ginkgo When using a sequence of DescribeSanity, RegisterTestsInGinkgo, DescribeSanity, RegisterTestsInGinkgo (i.e. embedding sanity tests in a Ginkgo suite more than once) the first test got registered multiple times, first by the initial RegisterTestsInGinkgo and again in the second RegisterTestsInGinkgo. Not only is this wrong, the locally bound variables also weren't initialized properly by the BeforeEach functions, which could lead to nil pointer accesses. --- pkg/sanity/sanity.go | 4 ++++ pkg/sanity/tests.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pkg/sanity/sanity.go b/pkg/sanity/sanity.go index 3b6fa6e6..913e0710 100644 --- a/pkg/sanity/sanity.go +++ b/pkg/sanity/sanity.go @@ -231,6 +231,10 @@ 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) registerTestsInGinkgo(sc) diff --git a/pkg/sanity/tests.go b/pkg/sanity/tests.go index 92f2fe57..e49aa7a4 100644 --- a/pkg/sanity/tests.go +++ b/pkg/sanity/tests.go @@ -53,4 +53,8 @@ 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 }