mirror of
https://github.com/safing/portmaster
synced 2025-09-04 19:49:15 +00:00
Merge pull request #26 from safing/feature/testing-hooks
Add support for start and stop hooks during test setup
This commit is contained in:
commit
c659c3748a
1 changed files with 28 additions and 2 deletions
|
@ -41,8 +41,17 @@ func init() {
|
||||||
flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down")
|
flag.BoolVar(&printStackOnExit, "print-stack-on-exit", false, "prints the stack before of shutting down")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestHookFunc func() error
|
||||||
|
|
||||||
// TestMain provides a simple unit test setup routine.
|
// TestMain provides a simple unit test setup routine.
|
||||||
func TestMain(m *testing.M, module *modules.Module) {
|
func TestMain(m *testing.M, module *modules.Module) {
|
||||||
|
TestMainWithHooks(m, module, nil, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMainWithHooks provides a simple unit test setup routine and calls
|
||||||
|
// afterStartFn after modules have started and beforeStopFn before modules
|
||||||
|
// are shutdown.
|
||||||
|
func TestMainWithHooks(m *testing.M, module *modules.Module, afterStartFn, beforeStopFn TestHookFunc) {
|
||||||
// enable module for testing
|
// enable module for testing
|
||||||
module.Enable()
|
module.Enable()
|
||||||
|
|
||||||
|
@ -72,9 +81,26 @@ func TestMain(m *testing.M, module *modules.Module) {
|
||||||
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err)
|
fmt.Fprintf(os.Stderr, "failed to setup test: %s\n", err)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
} else {
|
} else {
|
||||||
|
runTests := true
|
||||||
|
if afterStartFn != nil {
|
||||||
|
if err := afterStartFn(); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to run test start hook: %s\n", err)
|
||||||
|
runTests = false
|
||||||
|
exitCode = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if runTests {
|
||||||
// run tests
|
// run tests
|
||||||
exitCode = m.Run()
|
exitCode = m.Run()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if beforeStopFn != nil {
|
||||||
|
if err := beforeStopFn(); err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to run test shutdown hook: %s\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// shutdown
|
// shutdown
|
||||||
_ = modules.Shutdown()
|
_ = modules.Shutdown()
|
||||||
|
|
Loading…
Add table
Reference in a new issue