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.
This commit is contained in:
Patrick Ohly 2024-11-06 15:15:24 +01:00
parent 0d1b02490b
commit d0141640fe

View file

@ -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.