Skip to content

Commit

Permalink
add extensions/globals package (#692)
Browse files Browse the repository at this point in the history
this package can be used to reset the global state of ginkgo
  • Loading branch information
nogates committed Jun 17, 2020
1 parent 4943fe1 commit 3295c8f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 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()
}
43 changes: 43 additions & 0 deletions 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")
}
4 changes: 4 additions & 0 deletions internal/global/init.go
Expand Up @@ -13,6 +13,10 @@ var Suite *suite.Suite
var Failer *failer.Failer

func init() {
InitializeGlobals()
}

func InitializeGlobals() {
Failer = failer.New()
Suite = suite.New(Failer)
}

0 comments on commit 3295c8f

Please sign in to comment.