Release reporter destination references after finalization
This commit is contained in:
@@ -2,6 +2,7 @@ package commandkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
@@ -710,3 +711,39 @@ func setReporterVisibility(reporter *Reporter, mode reportingMode, progress, pro
|
||||
}
|
||||
reporter.applySelection(selection)
|
||||
}
|
||||
|
||||
func TestReporterFinalizationReleasesBorrowedDestinations(t *testing.T) {
|
||||
cause := errors.New("operation failed")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
outcome Outcome
|
||||
cause error
|
||||
}{
|
||||
{name: "success", outcome: Success()},
|
||||
{name: "failure", outcome: Failure("failed").WithCause(cause), cause: cause},
|
||||
{name: "cancellation", outcome: Failure("canceled").WithCause(context.Canceled), cause: context.Canceled},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out, diagnostics := &callerOwnedWriter{}, &callerOwnedWriter{}
|
||||
reporter := newReporter(Streams{Out: out, Err: diagnostics})
|
||||
output, deferred := reporter.Output(), reporter.AtEnd()
|
||||
result := reporter.finalize(tc.outcome, nil)
|
||||
if reporter.state.output.writer != nil || reporter.state.diagnostics.writer != nil || out.closeCalls != 0 || diagnostics.closeCalls != 0 || tc.cause != nil && !errors.Is(result.Cause, tc.cause) {
|
||||
t.Fatalf("outcome=%#v state=%#v", result, reporter.state)
|
||||
}
|
||||
outLen, diagnosticLen := out.Len(), diagnostics.Len()
|
||||
if _, err := output.Write([]byte("late")); !errors.Is(err, ErrReporterClosed) {
|
||||
t.Errorf("output error=%v", err)
|
||||
}
|
||||
if err := reporter.Warn("late"); !errors.Is(err, ErrReporterClosed) {
|
||||
t.Errorf("reporter error=%v", err)
|
||||
}
|
||||
if err := deferred.Warn("late"); !errors.Is(err, ErrReporterClosed) {
|
||||
t.Errorf("deferred error=%v", err)
|
||||
}
|
||||
if out.Len() != outLen || diagnostics.Len() != diagnosticLen {
|
||||
t.Fatal("late operation touched destinations")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user