Skip to content

Support functional options in TestMain #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions test_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@ import (
"github.com/matrix-org/complement/ct"
)

var testPackage *TestPackage
var (
testPackage *TestPackage
customDeployer func(numServers int) Deployment
)

type complementOpts struct {
cleanup func()
customDeployment func(numServers int) Deployment
}
type opt func(*complementOpts)

// WithCleanup adds a cleanup function which is called prior to terminating the test suite.
// It is called BEFORE Complement containers are destroyed.
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
// child processes, etc.
func WithCleanup(fn func()) opt {
return func(co *complementOpts) {
co.cleanup = fn
}
}

// WithDeployment adds a custom mechanism to deploy homeservers.
func WithDeployment(fn func(numServers int) Deployment) opt {
return func(co *complementOpts) {
co.customDeployment = fn
}
}

// TestMain is the main entry point for Complement.
//
Expand All @@ -19,23 +45,26 @@ var testPackage *TestPackage
// The 'namespace' should be unique for this test package, among all test packages which may run in parallel, to avoid
// docker containers stepping on each other. For MSCs, use the MSC name. For versioned releases, use the version number
// along with any sub-directory name.
func TestMain(m *testing.M, namespace string) {
TestMainWithCleanup(m, namespace, nil)
}
//
// Functional options can be used to control how Complement processes deployments.
func TestMain(m *testing.M, namespace string, customOpts ...opt) {
opts := &complementOpts{}
for _, o := range customOpts {
o(opts)
}
if opts.customDeployment != nil {
customDeployer = opts.customDeployment
}

// TestMainWithCleanup is TestMain but with a cleanup function prior to terminating the test suite.
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
// child processes, etc.
func TestMainWithCleanup(m *testing.M, namespace string, cleanup func()) {
var err error
testPackage, err = NewTestPackage(namespace)
if err != nil {
fmt.Printf("Error: %s", err)
os.Exit(1)
}
exitCode := m.Run()
if cleanup != nil {
cleanup()
if opts.cleanup != nil {
opts.cleanup()
}
testPackage.Cleanup()
os.Exit(exitCode)
Expand All @@ -61,5 +90,8 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
if testPackage == nil {
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
}
if customDeployer != nil {
return customDeployer(numServers)
}
return testPackage.Deploy(t, numServers)
}
Loading