Skip to content

Commit

Permalink
Support functional options in TestMain
Browse files Browse the repository at this point in the history
Rather than having `TestMainWithCleanup` and `TestMain`, use functional
options. Add support for a custom deployment mechanism at the same time.
  • Loading branch information
kegsay committed Dec 17, 2024
1 parent fc63446 commit 54956f9
Showing 1 changed file with 42 additions and 10 deletions.
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()
customDeplyoment 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.customDeplyoment = 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.customDeplyoment != nil {
customDeployer = opts.customDeplyoment
}

// 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)
}

0 comments on commit 54956f9

Please sign in to comment.