Separate show cancellation from rendering errors
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package ckconfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -67,3 +68,39 @@ func TestBufferedReportDoesNotRejectCancellationAfterDeliveryStarts(t *testing.T
|
||||
t.Fatalf("post-delivery cancellation changed the operation outcome: %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
type effectiveWriterFunc func(io.Writer) error
|
||||
|
||||
func (write effectiveWriterFunc) WriteEffective(w io.Writer) error { return write(w) }
|
||||
|
||||
func TestShowCancellationDuringBufferedPreparation(t *testing.T) {
|
||||
renderErr := errors.New("native rendering failed")
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
renderErr error
|
||||
}{{name: "cancellation"}, {name: "cancellation and rendering failure", renderErr: renderErr}} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
acquired, secondWrites := 0, 0
|
||||
scope := func(name string, writer EffectiveWriter) Scope[struct{}, struct{}] {
|
||||
return Scope[struct{}, struct{}]{Name: name, Operations: []Operation{Show}, Acquire: func(context.Context, Request, struct{}, struct{}) (commandkit.Acquired[View], error) {
|
||||
acquired++
|
||||
return commandkit.Acquired[View]{Value: View{Effective: writer}}, nil
|
||||
}}
|
||||
}
|
||||
app := newConfigTestApp(t, Command(Suite[struct{}, struct{}]{Scopes: []Scope[struct{}, struct{}]{
|
||||
scope("app", effectiveWriterFunc(func(w io.Writer) error { _, _ = io.WriteString(w, "name: app\n"); cancel(); return tc.renderErr })),
|
||||
scope("promptkit", effectiveWriterFunc(func(w io.Writer) error { secondWrites++; _, err := io.WriteString(w, "name: promptkit\n"); return err })),
|
||||
}}))
|
||||
var output bytes.Buffer
|
||||
got := app.Execute(ctx, []string{"config", "show", "--scope=all"}, commandkit.Streams{Out: &output, Err: io.Discard})
|
||||
if acquired != 2 || secondWrites != 0 || output.Len() != 0 || !errors.Is(got.Cause, context.Canceled) || got.Presentation.Output.WriteError != nil {
|
||||
t.Fatalf("acquired=%d second=%d output=%q outcome=%#v", acquired, secondWrites, output.String(), got)
|
||||
}
|
||||
if tc.renderErr == nil && got.Presentation.Output.RenderError != nil || tc.renderErr != nil && !errors.Is(got.Presentation.Output.RenderError, tc.renderErr) {
|
||||
t.Fatalf("render evidence = %#v", got.Presentation.Output)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,11 +66,13 @@ func runConfigShow[I, S any](invocation commandkit.Invocation, plan *configOpera
|
||||
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, err)
|
||||
}
|
||||
var value any
|
||||
var canceled error
|
||||
err := reporter.RenderOutput(func(io.Writer) error {
|
||||
values := make(map[string]any, len(documents))
|
||||
for _, document := range documents {
|
||||
if err := invocation.Context.Err(); err != nil {
|
||||
return err
|
||||
canceled = err
|
||||
return nil
|
||||
}
|
||||
var native bytes.Buffer
|
||||
if err := document.view.Effective.WriteEffective(&native); err != nil {
|
||||
@@ -89,6 +91,9 @@ func runConfigShow[I, S any](invocation commandkit.Invocation, plan *configOpera
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if canceled != nil {
|
||||
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, canceled)
|
||||
}
|
||||
if err != nil {
|
||||
if canceled := invocation.Context.Err(); canceled != nil {
|
||||
return configFailure("Configuration show was canceled.", commandkit.Explanation{}, canceled)
|
||||
|
||||
Reference in New Issue
Block a user