Documentation
¶
Overview ¶
Package hooks defines the testrig lifecycle hook interfaces and configuration.
Index ¶
- Variables
- func CatalogNames() []string
- func RegisterPersistentFlags(fs *pflag.FlagSet)
- func RunEntry(ctx context.Context, flags *pflag.FlagSet, opts RunOptions, shellCmd string, ...) error
- func RunGlobalSetup(ctx context.Context, hook Hook) error
- func RunGlobalSetups(ctx context.Context, flags *pflag.FlagSet, opts RunOptions) error
- func RunGlobalTeardown(ctx context.Context, hook Hook) error
- func RunGlobalTeardowns(ctx context.Context, flags *pflag.FlagSet, opts RunOptions) error
- func RunIterationSetup(ctx context.Context, hook Hook) error
- func RunIterationTeardown(ctx context.Context, hook Hook) error
- type Entry
- type Hook
- type IterationShellCommand
- type Option
- func GlobalSetup(h Hook) Option
- func GlobalTeardown(h Hook) Option
- func IterationSetup(h Hook) Option
- func IterationTeardown(h Hook) Option
- func WithCommand(cmd *cobra.Command) Option
- func WithResources(p ResourceProvider) Option
- func WithRootCommand(name string) Option
- func WithRootFlags(register func(*pflag.FlagSet)) Option
- type Phase
- type Resource
- type ResourceProvider
- type RunOptions
- type RunOrder
- type Scope
Constants ¶
This section is empty.
Variables ¶
var Catalog = []Entry{ { Name: "GlobalSetup", Flag: "global-setup", Scope: ScopeGlobal, Phase: PhaseSetup, FlagUsage: "Shell command to run before tests", }, { Name: "GlobalTeardown", Flag: "global-teardown", Scope: ScopeGlobal, Phase: PhaseTeardown, FlagUsage: "Shell command to run after tests", }, { Name: "IterationSetup", Flag: "iteration-setup", Scope: ScopeIteration, Phase: PhaseSetup, FlagUsage: "Shell command to run before each diagnose iteration", }, { Name: "IterationTeardown", Flag: "iteration-teardown", Scope: ScopeIteration, Phase: PhaseTeardown, FlagUsage: "Shell command to run after each diagnose iteration", }, }
Catalog is the single source of truth for lifecycle hooks. Add an Entry here to get a CLI flag, config binding (iteration scope), and docs row (via go generate).
Functions ¶
func CatalogNames ¶
func CatalogNames() []string
CatalogNames returns Option registrar names in catalog order.
func RegisterPersistentFlags ¶
RegisterPersistentFlags adds every catalog entry as a root persistent string flag.
func RunEntry ¶
func RunEntry( ctx context.Context, flags *pflag.FlagSet, opts RunOptions, shellCmd string, e Entry, order RunOrder, ) error
RunEntry runs native and/or shell hooks for one catalog entry.
func RunGlobalSetup ¶
RunGlobalSetup runs hook before tests execute. A nil hook is a no-op.
func RunGlobalSetups ¶
RunGlobalSetups runs all global setup hooks (native then shell per entry).
func RunGlobalTeardown ¶
RunGlobalTeardown runs hook after tests execute. A nil hook is a no-op.
func RunGlobalTeardowns ¶
RunGlobalTeardowns runs all global teardown hooks (shell then native per entry).
func RunIterationSetup ¶
RunIterationSetup runs hook before a single diagnose iteration. A nil hook is a no-op.
Types ¶
type Entry ¶
type Entry struct {
Name string // e.g. GlobalSetup; matches testrig.GlobalSetup and RunOptions field.
Flag string // e.g. global-setup (persistent root flag, no leading dashes).
Scope Scope
Phase Phase
FlagUsage string // Cobra flag description.
}
Entry describes one lifecycle hook in the catalog: public Option name, CLI flag, and scope.
func EntryByName ¶
EntryByName returns a catalog entry by Option/func name.
type Hook ¶
Hook is a lifecycle callback. The context carries cancellation from the test runner — hooks should respect it for long-running operations.
func BuildIterationHook ¶
func BuildIterationHook(opts RunOptions, shell IterationShellCommand, phase Phase) Hook
BuildIterationHook composes iteration setup or teardown hooks for diagnose. Returns nil when no native or shell hooks are configured for that phase.
func NewShellHook ¶
NewShellHook returns a Hook that runs cmd via the system shell (sh -c). The hook respects context cancellation. On non-zero exit, the combined stdout+stderr is included in the returned error so failing setup commands are diagnosable.
type IterationShellCommand ¶
IterationShellCommand resolves a shell command for an iteration-scoped entry from flag values.
type Option ¶
type Option func(*runnerOptions)
Option configures the testrig CLI runner.
func GlobalSetup ¶
GlobalSetup registers a hook to run once before any tests.
func GlobalTeardown ¶
GlobalTeardown registers a hook to run once after all tests finish.
func IterationSetup ¶
IterationSetup registers a hook to run before each diagnose iteration.
func IterationTeardown ¶
IterationTeardown registers a hook to run after each diagnose iteration.
func WithCommand ¶ added in v0.0.2
WithCommand registers an additional subcommand on the testrig root command, for project-specific utilities (e.g. persistent database management). May be passed multiple times.
func WithResources ¶ added in v0.0.2
func WithResources(p ResourceProvider) Option
WithResources registers a provider that supplies isolated infrastructure (e.g. databases) for the run. See ResourceProvider for count semantics and partial-failure behavior.
func WithRootCommand ¶ added in v0.0.2
WithRootCommand sets the CLI name used in help text, examples, and cobra.CommandPath(). Defaults to "testrig" when unset.
func WithRootFlags ¶ added in v0.0.2
WithRootFlags registers persistent flags on the root command, available to every subcommand. Use it to add consumer flags (e.g. --database-url) that a resource provider or custom command reads.
type Resource ¶ added in v0.0.2
type Resource struct {
Env []string
Reset func(context.Context) error
DumpDiagnostics func(ctx context.Context, dir string, iteration int) error
Cleanup func() error
}
Resource is one prepared, isolated piece of infrastructure for a test run (most commonly a database). It is the extension point consumers use to plug in setups that need per-worker isolation, reset between diagnose iterations, and post-iteration diagnostics — richer than the shell-style lifecycle hooks.
All fields are optional:
- Env is appended to the child go test environment (e.g. CL_DATABASE_URL=...).
- Reset restores clean state before a diagnose worker is reused.
- DumpDiagnostics writes post-iteration state into the results dir.
- Cleanup tears the resource down once, after the command finishes.
type ResourceProvider ¶ added in v0.0.2
ResourceProvider supplies count isolated resources for a run. It is called once, before any tests start, with a context that is canceled on SIGINT/SIGTERM.
count is the effective parallel-iteration count for diagnose (one resource per worker, capped by --iterations). For the default root go test invocation and gotestsum, count is always 1 — even when go test uses -p>1, only a single Env slice is applied to the child process.
The provider must return exactly count resources or an error. On error, testrig does not call Cleanup on any returned resources; roll back partial provisioning inside the provider before returning.
type RunOptions ¶
type RunOptions struct {
GlobalSetup Hook
GlobalTeardown Hook
IterationSetup Hook
IterationTeardown Hook
ResourceProvider ResourceProvider
Commands []*cobra.Command
RootFlags func(*pflag.FlagSet)
RootCommand string
}
RunOptions contains the evaluated configuration for the testrig CLI. It is exported for internal use by the CLI engine.
func BuildOptions ¶
func BuildOptions(opts ...Option) RunOptions
BuildOptions evaluates the functional options and returns the internal struct. It is exported for internal use by the CLI engine.
func (RunOptions) Hook ¶
func (o RunOptions) Hook(name string) Hook
Hook returns the native hook registered for name, or nil.