All checks were successful
Test / Create distribution (push) Successful in 33s
Test / Sandbox (push) Successful in 2m7s
Test / Hakurei (push) Successful in 3m8s
Test / Hpkg (push) Successful in 3m59s
Test / Sandbox (race detector) (push) Successful in 4m26s
Test / Hakurei (race detector) (push) Successful in 5m6s
Test / Flake checks (push) Successful in 1m26s
This allows tests to stub all kernel behaviour, enabling measurement of all function call arguments and error injection. Signed-off-by: Ophestra <cat@gensokyo.uk>
69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"reflect"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
type Msg interface {
|
|
IsVerbose() bool
|
|
Verbose(v ...any)
|
|
Verbosef(format string, v ...any)
|
|
WrapErr(err error, a ...any) error
|
|
PrintBaseErr(err error, fallback string)
|
|
|
|
Suspend()
|
|
Resume() bool
|
|
|
|
BeforeExit()
|
|
}
|
|
|
|
type DefaultMsg struct{ inactive atomic.Bool }
|
|
|
|
func (msg *DefaultMsg) IsVerbose() bool { return true }
|
|
func (msg *DefaultMsg) Verbose(v ...any) {
|
|
if !msg.inactive.Load() {
|
|
log.Println(v...)
|
|
}
|
|
}
|
|
func (msg *DefaultMsg) Verbosef(format string, v ...any) {
|
|
if !msg.inactive.Load() {
|
|
log.Printf(format, v...)
|
|
}
|
|
}
|
|
|
|
// checkedWrappedErr implements error with strict checks for wrapped values.
|
|
type checkedWrappedErr struct {
|
|
err error
|
|
a []any
|
|
}
|
|
|
|
func (c *checkedWrappedErr) Error() string { return fmt.Sprintf("%v, a = %s", c.err, c.a) }
|
|
func (c *checkedWrappedErr) Is(err error) bool {
|
|
var concreteErr *checkedWrappedErr
|
|
if !errors.As(err, &concreteErr) {
|
|
return false
|
|
}
|
|
return reflect.DeepEqual(c, concreteErr)
|
|
}
|
|
|
|
func (msg *DefaultMsg) WrapErr(err error, a ...any) error {
|
|
// provide a mostly bulletproof path to bypass this behaviour in tests
|
|
if testing.Testing() && os.Getenv("GOPATH") != Nonexistent {
|
|
return &checkedWrappedErr{err, a}
|
|
}
|
|
|
|
log.Println(a...)
|
|
return err
|
|
}
|
|
func (msg *DefaultMsg) PrintBaseErr(err error, fallback string) { log.Println(fallback, err) }
|
|
|
|
func (msg *DefaultMsg) Suspend() { msg.inactive.Store(true) }
|
|
func (msg *DefaultMsg) Resume() bool { return msg.inactive.CompareAndSwap(true, false) }
|
|
func (msg *DefaultMsg) BeforeExit() {}
|