From d0141640fe261dc5370e1d631c7bfefe0e80e79a Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Wed, 6 Nov 2024 15:15:24 +0100 Subject: [PATCH] ktesting: print info and progress to /dev/tty The "received interrupt signal" is useful also when running with "go test" without -v because it shows that the shutdown has started. But more important is that a progress report gets shown because that feature is useful in particular when "go test" produces no output while it runs. --- test/utils/ktesting/signals.go | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/test/utils/ktesting/signals.go b/test/utils/ktesting/signals.go index f498c46259c..605045a1fe9 100644 --- a/test/utils/ktesting/signals.go +++ b/test/utils/ktesting/signals.go @@ -29,11 +29,7 @@ import ( var ( // defaultProgressReporter is inactive until init is called. - defaultProgressReporter = &progressReporter{ - // os.Stderr gets redirected by "go test". "go test -v" has to be - // used to see the output while a test runs. - out: os.Stderr, - } + defaultProgressReporter = &progressReporter{} ) const ginkgoSpecContextKey = "GINKGO_SPEC_CONTEXT" @@ -57,6 +53,7 @@ type progressReporter struct { reporterCounter int64 reporters map[int64]func() string out io.Writer + closeOut func() error } var _ ginkgoReporter = &progressReporter{} @@ -88,6 +85,24 @@ func (p *progressReporter) init(tb TB) context.Context { return p.interruptCtx } + // Might have been set for testing purposes. + if p.out == nil { + // os.Stderr gets redirected by "go test". "go test -v" has to be + // used to see that output while a test runs. + // + // Opening /dev/tty during init avoids the redirection. + // May fail, depending on the OS, in which case + // os.Stderr is used. + if console, err := os.OpenFile("/dev/tty", os.O_RDWR|os.O_APPEND, 0); err == nil { + p.out = console + p.closeOut = console.Close + + } else { + p.out = os.Stdout + p.closeOut = nil + } + } + p.signalCtx, p.signalCancel = signal.NotifyContext(context.Background(), os.Interrupt) cancelCtx, cancel := context.WithCancelCause(context.Background()) p.wg.Go(func() { @@ -126,6 +141,12 @@ func (p *progressReporter) finalize() { p.signalCancel() p.wg.Wait() + + // Now that all goroutines are stopped, we can clean up some more. + if p.closeOut != nil { + _ = p.closeOut() + p.out = nil + } } // AttachProgressReporter implements Gomega's contextWithAttachProgressReporter.