Run invocation features and render final reports
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package commandkit
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
internalcobra "go.maximumdirect.net/commandkit/internal/cobra"
|
||||
commandmeta "go.maximumdirect.net/commandkit/internal/commandmeta"
|
||||
@@ -106,8 +110,7 @@ func TestInvocationFeatureFlagsParseAndDiscover(t *testing.T) {
|
||||
NewInput: func() featureInput { constructed++; return featureInput{} },
|
||||
Flags: []Flag[featureInput]{BoolFlag(FlagMeta{Name: "observe", Short: "o"}, false, func(i *featureInput, v Supplied[bool]) { i.selected = v })},
|
||||
Start: func(inv Invocation, _ featureInput) (context.Context, func() Report) {
|
||||
t.Fatal("unexpected start")
|
||||
return nil, nil
|
||||
return inv.Context, nil
|
||||
},
|
||||
})
|
||||
type localInput struct{ name string }
|
||||
@@ -126,8 +129,8 @@ func TestInvocationFeatureFlagsParseAndDiscover(t *testing.T) {
|
||||
if out := app.Execute(context.Background(), []string{"run", "--observe=bad", "--help"}, Streams{}); out.Kind != OutcomeUsage {
|
||||
t.Fatalf("help invalid flag: %#v", out)
|
||||
}
|
||||
if constructed != 0 {
|
||||
t.Fatal("input constructed before execution boundary")
|
||||
if constructed != 1 {
|
||||
t.Fatal("help or invalid input constructed feature input")
|
||||
}
|
||||
snapshot := app.routeSnapshot()
|
||||
for _, path := range [][]string{nil, {"run"}, {"short"}} {
|
||||
@@ -151,3 +154,103 @@ func TestInvocationFeatureFlagsParseAndDiscover(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvocationFeatureLifecycleAndFinalization(t *testing.T) {
|
||||
type key struct{}
|
||||
type input struct{ on bool }
|
||||
var events []string
|
||||
feature := NewInvocationFeature(InvocationFeatureSpec[input]{
|
||||
NewInput: func() input { events = append(events, "feature input"); return input{} },
|
||||
Flags: []Flag[input]{BoolFlag(FlagMeta{Name: "observe"}, false, func(i *input, v Supplied[bool]) { i.on = v.Value })},
|
||||
Start: func(inv Invocation, i input) (context.Context, func() Report) {
|
||||
events = append(events, "start")
|
||||
if !i.on {
|
||||
return nil, nil
|
||||
}
|
||||
return context.WithValue(inv.Context, key{}, "value"), func() Report { events = append(events, "report"); return Report{Value: "summary"} }
|
||||
},
|
||||
})
|
||||
var reject bool
|
||||
cmd := NewCommand(CommandSpec[struct{}]{Name: "run", NewInput: func() struct{} { events = append(events, "input"); return struct{}{} }, CheckInput: func(struct{}) error {
|
||||
events = append(events, "check")
|
||||
if reject {
|
||||
return errors.New("rejected")
|
||||
}
|
||||
return nil
|
||||
}, Run: func(inv Invocation, _ struct{}) Outcome {
|
||||
events = append(events, "run")
|
||||
if inv.Context.Value(key{}) != "value" {
|
||||
t.Error("missing context")
|
||||
}
|
||||
_ = inv.Reporter().AtEnd().Warn("deferred")
|
||||
return Failure("failed")
|
||||
}})
|
||||
app, err := New(Identity{Name: "test"}, WithInvocationFeature(cmd, feature), Completion("test"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"run", "--observe", "--verbose", "--format=json"}, Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Kind != OutcomeOperational || !reflect.DeepEqual(events, []string{"input", "check", "feature input", "start", "run", "report"}) {
|
||||
t.Fatalf("events=%v outcome=%#v", events, outcome)
|
||||
}
|
||||
if !strings.Contains(diagnostics.String(), "WARN: deferred\nsummary\nERROR: failed") {
|
||||
t.Fatalf("ordering: %q", diagnostics.String())
|
||||
}
|
||||
events = nil
|
||||
reject = true
|
||||
diagnostics.Reset()
|
||||
app.Execute(context.Background(), []string{"run", "--observe"}, Streams{Err: &diagnostics})
|
||||
if !reflect.DeepEqual(events, []string{"input", "check"}) || strings.Contains(diagnostics.String(), "summary") {
|
||||
t.Fatalf("check rejection: %v %q", events, diagnostics.String())
|
||||
}
|
||||
reject = false
|
||||
for _, args := range [][]string{{"run", "--help"}, {"version", "--observe"}, {"completion", "bash", "--observe"}, {"missing", "--observe"}, {"run", "--observe=invalid"}} {
|
||||
events = nil
|
||||
app.Execute(context.Background(), args, Streams{})
|
||||
if len(events) != 0 {
|
||||
t.Fatalf("builtin or error %v: %v", args, events)
|
||||
}
|
||||
}
|
||||
events = nil
|
||||
diagnostics.Reset()
|
||||
app.Execute(context.Background(), []string{"run", "--observe", "--quiet"}, Streams{Err: &diagnostics})
|
||||
if !strings.Contains(diagnostics.String(), "ERROR: failed") || strings.Contains(diagnostics.String(), "summary") || events[len(events)-1] != "report" {
|
||||
t.Fatalf("quiet: %v %q", events, diagnostics.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvocationFeatureDiagnosticFailures(t *testing.T) {
|
||||
cause := errors.New("domain cause")
|
||||
failTemplate := template.Must(template.New("bad").Parse("{{call .}}"))
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
report Report
|
||||
writer io.Writer
|
||||
quiet bool
|
||||
render, write bool
|
||||
}{
|
||||
{name: "render", report: Report{Value: func() (string, error) { return "", errors.New("bad template") }, TextTemplate: failTemplate}, render: true},
|
||||
{name: "quiet render", report: Report{Value: func() (string, error) { return "", errors.New("bad template") }, TextTemplate: failTemplate}, quiet: true, render: true},
|
||||
{name: "short write", report: Report{Value: "summary"}, writer: &reporterTestWriter{n: 1}, write: true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
feature := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Start: func(inv Invocation, _ struct{}) (context.Context, func() Report) {
|
||||
return nil, func() Report { return tc.report }
|
||||
}})
|
||||
cmd := WithInvocationFeature(NewCommand(CommandSpec[struct{}]{Name: "run", Run: func(Invocation, struct{}) Outcome { return Domain("domain", 7).WithCause(cause) }}), feature)
|
||||
app, err := New(Identity{Name: "test"}, cmd)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args := []string{"run"}
|
||||
if tc.quiet {
|
||||
args = append(args, "--quiet")
|
||||
}
|
||||
out := app.Execute(context.Background(), args, Streams{Err: tc.writer})
|
||||
if out.Kind != OutcomeDomain || out.Status != 7 || !errors.Is(out.Cause, cause) || (out.Presentation.Diagnostics.RenderError != nil) != tc.render || (out.Presentation.Diagnostics.WriteError != nil) != tc.write {
|
||||
t.Fatalf("outcome=%#v", out)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user