From 3295c8fa37cb4138567b74ac82dcc685f8f3d78e Mon Sep 17 00:00:00 2001 From: David Tapiador Date: Wed, 17 Jun 2020 13:39:21 +1000 Subject: [PATCH] add `extensions/globals` package (#692) this package can be used to reset the global state of ginkgo --- extensions/globals/globals.go | 26 ++++++++++++++++++ extensions/globals/globals_test.go | 43 ++++++++++++++++++++++++++++++ internal/global/init.go | 4 +++ 3 files changed, 73 insertions(+) create mode 100644 extensions/globals/globals.go create mode 100644 extensions/globals/globals_test.go diff --git a/extensions/globals/globals.go b/extensions/globals/globals.go new file mode 100644 index 000000000..838d8e097 --- /dev/null +++ b/extensions/globals/globals.go @@ -0,0 +1,26 @@ +// Package `globals` provides an interface to alter the global state of ginkgo suite. +// +// ginkgo currently registers a few singleton global vars that hold all the +// test blocks and failure management. These vars are global per package, which means +// that only one Suite definition can coexist in one package. +// +// However, there can be some use cases where applications using ginkgo may want to +// have a bit more control about this. For instance, a package may be using ginkgo +// to dynamically generate different tests and groups depending on some configuration. +// In this particular case, if the application wants to test how these different groups +// are generated, they will need access to change these global variables, so they +// can re-generate this global state, and ensure that different configuration generate +// indeed different tests. +// +// Note that this package is not intended to be used as part of normal ginkgo setups, and +// usually, you will never need to worry about the global state of ginkgo +package globals + +import "github.com/onsi/ginkgo/internal/global" + +// Reset calls `global.InitializeGlobals()` which will basically create a new instance +// of Suite, and therefore, will effectively reset the global variables to the init state. +// This will effectively remove all groups, tests and blocks that were added to the Suite. +func Reset() { + global.InitializeGlobals() +} diff --git a/extensions/globals/globals_test.go b/extensions/globals/globals_test.go new file mode 100644 index 000000000..762e46aa3 --- /dev/null +++ b/extensions/globals/globals_test.go @@ -0,0 +1,43 @@ +package globals_test + +import ( + "testing" + + . "github.com/onsi/ginkgo" + "github.com/onsi/ginkgo/extensions/globals" + . "github.com/onsi/gomega" +) + +func TestGlobals(t *testing.T) { + RegisterFailHandler(Fail) + + // define some vars to store how many times a test has been run + var ( + testI = 0 + testII = 0 + ) + + // Define a simple gingko test I + var _ = Describe("ginkgo test I", func() { + It("build tests I", func() { + testI++ + Ω(testI).Should(Equal(1)) + }) + }) + + RunSpecs(t, "Test Runner Suite I") + + // reset the global state of ginkgo. test I should now be removed, and it + // won't run twice. + globals.Reset() + + // Define a simple gingko test II + var _ = Describe("ginkgo test II", func() { + It("build tests II", func() { + testII++ + Ω(testII).Should(Equal(1)) + }) + }) + + RunSpecs(t, "Test Runner Suite II") +} diff --git a/internal/global/init.go b/internal/global/init.go index 711443200..109f617a5 100644 --- a/internal/global/init.go +++ b/internal/global/init.go @@ -13,6 +13,10 @@ var Suite *suite.Suite var Failer *failer.Failer func init() { + InitializeGlobals() +} + +func InitializeGlobals() { Failer = failer.New() Suite = suite.New(Failer) }