Repository: etcd-io/etcd
File: tests/framework/integration/testing.go (Go)

Code before:

type TestOption func(opt *testOptions)

// WithoutGoLeakDetection disables checking whether a testcase leaked a goroutine.
func WithoutGoLeakDetection() TestOption {
	return func(opt *testOptions) { opt.goLeakDetection = false }
}

func WithoutSkipInShort() TestOption {
	return func(opt *testOptions) { opt.skipInShort = false }
}

// WithFailpoint registers a go fail point
func WithFailpoint(name, payload string) TestOption {
	return func(opt *testOptions) { opt.failpoint = &failpoint{name: name, payload: payload} }
}

// BeforeTestExternal initializes test context and is targeted for external APIs.
// In general the `integration` package is not targeted to be used outside of
// etcd project, but till the dedicated package is developed, this is
// the best entry point so far (without backward compatibility promise).
func BeforeTestExternal(t testutil.TB) {
	BeforeTest(t, WithoutSkipInShort(), WithoutGoLeakDetection())
}

func GofailSkipCheck(t testutil.TB) {
	if len(gofail.List()) == 0 {
		t.Skip("please run 'make gofail-enable' before running the test")
	}
}

func BeforeTest(t testutil.TB, opts ...TestOption) {
	t.Helper()
	options := newTestOptions(opts...)

	if insideTestContext {
		t.Fatal("already in test context. BeforeTest was likely already called")
	}

	if options.skipInShort {
		testutil.SkipTestIfShortMode(t, "Cannot create clusters in --short tests")
	}

	if options.goLeakDetection {
		testutil.RegisterLeakDetection(t)
	}

	if options.failpoint != nil && len(options.failpoint.name) != 0 {
		GofailSkipCheck(t)
		require.NoError(t, gofail.Enable(options.failpoint.name, options.failpoint.payload))
		t.Cleanup(func() {

Review comment (bug):
Name here is doesn't explain what the function does. Would `SkipIfGofailNotEnabled` make more sense?

Code after:

type TestOption func(opt *testOptions)

// WithoutGoLeakDetection disables checking whether a testcase leaked a goroutine.
func WithoutGoLeakDetection() TestOption {
	return func(opt *testOptions) { opt.goLeakDetection = false }
}

func WithoutSkipInShort() TestOption {
	return func(opt *testOptions) { opt.skipInShort = false }
}

// WithFailpoint registers a go fail point
func WithFailpoint(name, payload string) TestOption {
	return func(opt *testOptions) { opt.failpoint = &failpoint{name: name, payload: payload} }
}

// BeforeTestExternal initializes test context and is targeted for external APIs.
// In general the `integration` package is not targeted to be used outside of
// etcd project, but till the dedicated package is developed, this is
// the best entry point so far (without backward compatibility promise).
func BeforeTestExternal(t testutil.TB) {
	BeforeTest(t, WithoutSkipInShort(), WithoutGoLeakDetection())
}

func SkipIfNoGoFail(t testutil.TB) {
	if len(gofail.List()) == 0 {
		t.Skip("please run 'make gofail-enable' before running the test")
	}
}

func BeforeTest(t testutil.TB, opts ...TestOption) {
	t.Helper()
	options := newTestOptions(opts...)

	if insideTestContext {
		t.Fatal("already in test context. BeforeTest was likely already called")
	}

	if options.skipInShort {
		testutil.SkipTestIfShortMode(t, "Cannot create clusters in --short tests")
	}

	if options.goLeakDetection {
		testutil.RegisterLeakDetection(t)
	}

	if options.failpoint != nil && len(options.failpoint.name) != 0 {
		SkipIfNoGoFail(t)
		require.NoError(t, gofail.Enable(options.failpoint.name, options.failpoint.payload))
		t.Cleanup(func() {