Treat nil diagnostic writers as no-output entries
This commit is contained in:
@@ -469,3 +469,39 @@ func TestExecuteNormalizesOutcomeDefaultsAndPreservesDomainValues(t *testing.T)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNilDiagnosticWritersHaveNoOutputAndDoNotHideCauses(t *testing.T) {
|
||||
cause := errors.New("operation failed")
|
||||
for _, base := range []commandkit.Outcome{commandkit.Success(), commandkit.Domain("needs action", 2, nil), commandkit.Failure(cause)} {
|
||||
base.Diagnostics = []commandkit.Diagnostic{{}, {Severity: configkit.SeverityWarning}, {Severity: configkit.SeverityError}}
|
||||
command, err := commandkit.NewCommand(commandkit.CommandSpec[struct{}]{Name: "run", Run: func(commandkit.Invocation, struct{}) commandkit.Outcome { return base }})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "example"}, command)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var diagnostics bytes.Buffer
|
||||
result := app.Execute(context.Background(), []string{"run"}, commandkit.Streams{Err: &diagnostics})
|
||||
want := commandkit.DeliveryNone
|
||||
if base.Cause != nil {
|
||||
want = commandkit.DeliveryComplete
|
||||
}
|
||||
if result.Kind != base.Kind || result.Status != base.Status || result.Domain != base.Domain || result.Cause != base.Cause || result.DeliveryError != nil || result.DiagnosticDelivery != want {
|
||||
t.Fatalf("result=%#v base=%#v", result, base)
|
||||
}
|
||||
if base.Cause == nil && diagnostics.Len() != 0 {
|
||||
t.Fatalf("unexpected output=%q", diagnostics.String())
|
||||
}
|
||||
if base.Cause != nil && strings.Count(diagnostics.String(), cause.Error()) != 1 {
|
||||
t.Fatalf("cause output=%q", diagnostics.String())
|
||||
}
|
||||
if base.Cause != nil {
|
||||
result = app.Execute(context.Background(), []string{"run"}, commandkit.Streams{Err: failingWriter{io.ErrClosedPipe}})
|
||||
if !errors.Is(result.Cause, cause) || !errors.Is(result.DeliveryError, io.ErrClosedPipe) || result.DiagnosticDelivery != commandkit.DeliveryIncomplete {
|
||||
t.Fatalf("failed delivery=%#v", result)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user