diff --git a/container/msg.go b/container/msg.go index 4be0fd2..1512a58 100644 --- a/container/msg.go +++ b/container/msg.go @@ -50,3 +50,18 @@ func (msg *DefaultMsg) Verbosef(format string, v ...any) { func (msg *DefaultMsg) Suspend() { msg.inactive.Store(true) } func (msg *DefaultMsg) Resume() bool { return msg.inactive.CompareAndSwap(true, false) } func (msg *DefaultMsg) BeforeExit() {} + +// msg is the [Msg] implemented used by all exported [container] functions. +var msg Msg = new(DefaultMsg) + +// GetOutput returns the current active [Msg] implementation. +func GetOutput() Msg { return msg } + +// SetOutput replaces the current active [Msg] implementation. +func SetOutput(v Msg) { + if v == nil { + msg = new(DefaultMsg) + } else { + msg = v + } +} diff --git a/container/msg_test.go b/container/msg_test.go index ba62e9d..1b36a1d 100644 --- a/container/msg_test.go +++ b/container/msg_test.go @@ -146,3 +146,39 @@ func (out *testOutput) Resume() bool { } func (out *testOutput) BeforeExit() { out.Verbose("beforeExit called") } + +func TestGetSetOutput(t *testing.T) { + { + out := container.GetOutput() + t.Cleanup(func() { container.SetOutput(out) }) + } + + t.Run("default", func(t *testing.T) { + container.SetOutput(new(stubOutput)) + if v, ok := container.GetOutput().(*container.DefaultMsg); ok { + t.Fatalf("SetOutput: got unexpected output %#v", v) + } + container.SetOutput(nil) + if _, ok := container.GetOutput().(*container.DefaultMsg); !ok { + t.Fatalf("SetOutput: got unexpected output %#v", container.GetOutput()) + } + }) + + t.Run("stub", func(t *testing.T) { + container.SetOutput(new(stubOutput)) + if _, ok := container.GetOutput().(*stubOutput); !ok { + t.Fatalf("SetOutput: got unexpected output %#v", container.GetOutput()) + } + }) +} + +type stubOutput struct { + wrapF func(error, ...any) error +} + +func (*stubOutput) IsVerbose() bool { panic("unreachable") } +func (*stubOutput) Verbose(...any) { panic("unreachable") } +func (*stubOutput) Verbosef(string, ...any) { panic("unreachable") } +func (*stubOutput) Suspend() { panic("unreachable") } +func (*stubOutput) Resume() bool { panic("unreachable") } +func (*stubOutput) BeforeExit() { panic("unreachable") } diff --git a/container/output.go b/container/output.go index 86f57c8..4d799eb 100644 --- a/container/output.go +++ b/container/output.go @@ -75,14 +75,3 @@ func (s *Suspendable) Resume() (resumed bool, dropped uintptr, n int64, err erro } return } - -var msg Msg = new(DefaultMsg) - -func GetOutput() Msg { return msg } -func SetOutput(v Msg) { - if v == nil { - msg = new(DefaultMsg) - } else { - msg = v - } -} diff --git a/container/output_test.go b/container/output_test.go index 54eba8f..cfa8e0d 100644 --- a/container/output_test.go +++ b/container/output_test.go @@ -1,4 +1,4 @@ -package container +package container_test import ( "bytes" @@ -8,10 +8,14 @@ import ( "syscall" "testing" + "hakurei.app/container" "hakurei.app/container/stub" ) func TestSuspendable(t *testing.T) { + // copied from output.go + const suspendBufMax = 1 << 24 + const ( // equivalent to len(want.pt) nSpecialPtEquiv = -iota - 1 @@ -70,7 +74,7 @@ func TestSuspendable(t *testing.T) { var dw expectWriter - w := Suspendable{Downstream: &dw} + w := container.Suspendable{Downstream: &dw} for _, tc := range testCases { // these share the same writer, so cannot be subtests t.Logf("writing step %q", tc.name) @@ -149,39 +153,3 @@ func (w *expectWriter) Write(p []byte) (n int, err error) { } return } - -func TestGetSetOutput(t *testing.T) { - { - out := GetOutput() - t.Cleanup(func() { SetOutput(out) }) - } - - t.Run("default", func(t *testing.T) { - SetOutput(new(stubOutput)) - if v, ok := GetOutput().(*DefaultMsg); ok { - t.Fatalf("SetOutput: got unexpected output %#v", v) - } - SetOutput(nil) - if _, ok := GetOutput().(*DefaultMsg); !ok { - t.Fatalf("SetOutput: got unexpected output %#v", GetOutput()) - } - }) - - t.Run("stub", func(t *testing.T) { - SetOutput(new(stubOutput)) - if _, ok := GetOutput().(*stubOutput); !ok { - t.Fatalf("SetOutput: got unexpected output %#v", GetOutput()) - } - }) -} - -type stubOutput struct { - wrapF func(error, ...any) error -} - -func (*stubOutput) IsVerbose() bool { panic("unreachable") } -func (*stubOutput) Verbose(...any) { panic("unreachable") } -func (*stubOutput) Verbosef(string, ...any) { panic("unreachable") } -func (*stubOutput) Suspend() { panic("unreachable") } -func (*stubOutput) Resume() bool { panic("unreachable") } -func (*stubOutput) BeforeExit() { panic("unreachable") }