Skip to content

Commit

Permalink
sanity: support embedding tests multiple time in Ginkgo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Mar 19, 2020
1 parent 8f687ee commit ddf8b3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sanity/sanity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/sanity/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ddf8b3f

Please sign in to comment.