full unit testing and fixes

- mapToString is ordered according to setup
- setupSingle & teardownSingle no longer return "ok" states
- teardown functions support errors
- setup and teardown functions receive module
This commit is contained in:
Elijah Duffy
2025-06-03 14:49:00 -07:00
parent 5378c53537
commit ed8137647d
4 changed files with 695 additions and 65 deletions

View File

@@ -1,17 +1,24 @@
package app
import (
"errors"
"fmt"
"log/slog"
)
// ErrModuleNotFound is returned when a module is not found in the lifecycle.
var ErrModuleNotFound = errors.New("module not found")
// moduleFn is a function type for module setup and teardown functions.
type moduleFn func(*Module) error
// Module represents a sub-system of the application, with its setup and
// teardown functions and dependencies.
type Module struct {
logger *slog.Logger
name string
setup setupFn
teardown teardownFn
setup moduleFn
teardown moduleFn
depends []string
loaded bool // loaded indicates if the module has been set up
}
@@ -19,9 +26,9 @@ type Module struct {
// ModuleOpts contains user-exposed options when defining a module.
type ModuleOpts struct {
// Setup is the setup function for the module.
Setup setupFn
Setup moduleFn
// Teardown is the teardown function for the module.
Teardown teardownFn
Teardown moduleFn
// Depends is a list of modules that this module depends on. Each entry must
// be the exact name of a module registered in the lifecycle.
Depends []string