Document and validate automatic usage reporting

This commit is contained in:
2026-11-22 21:06:39 +00:00
parent a0608a53ee
commit a0cdd3d7c5
13 changed files with 188 additions and 30 deletions

View File

@@ -254,3 +254,52 @@ func TestInvocationFeatureDiagnosticFailures(t *testing.T) {
})
}
}
func TestRequiredInvocationFlagDoesNotBlockBuiltinsOrGroupHelp(t *testing.T) {
feature := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "observe", Required: true}, false, func(*struct{}, Supplied[bool]) {})}, Start: func(inv Invocation, _ struct{}) (context.Context, func() Report) {
t.Fatal("feature started")
return nil, nil
}})
app, err := New(Identity{Name: "test"}, WithInvocationFeature(Group("group", ""), feature))
if err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"version"}, {"help"}, {"group"}, {"group", "--help"}} {
if out := app.Execute(context.Background(), args, Streams{}); out.Status != 0 {
t.Fatalf("%v: %#v", args, out)
}
}
}
func TestInvocationFeaturesChainContextsAndReports(t *testing.T) {
type chainKey struct{}
var order []string
first := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Start: func(inv Invocation, _ struct{}) (context.Context, func() Report) {
order = append(order, "first start")
return context.WithValue(inv.Context, chainKey{}, "first"), func() Report { order = append(order, "first report"); return Report{Value: "first"} }
}})
second := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Start: func(inv Invocation, _ struct{}) (context.Context, func() Report) {
if inv.Context.Value(chainKey{}) != "first" {
t.Error("feature context not propagated")
}
order = append(order, "second start")
return nil, func() Report { order = append(order, "second report"); return Report{Value: "second"} }
}})
cmd := NewCommand(CommandSpec[struct{}]{Name: "run", Run: func(inv Invocation, _ struct{}) Outcome {
if inv.Context.Value(chainKey{}) != "first" {
t.Error("nil context did not preserve previous context")
}
order = append(order, "run")
return Success()
}})
cmd = WithInvocationFeature(WithInvocationFeature(cmd, first), second)
app, err := New(Identity{Name: "test"}, cmd)
if err != nil {
t.Fatal(err)
}
var diag bytes.Buffer
out := app.Execute(context.Background(), []string{"run"}, Streams{Err: &diag})
if out.Status != 0 || !reflect.DeepEqual(order, []string{"first start", "second start", "run", "first report", "second report"}) || diag.String() != "first\nsecond\n" {
t.Fatalf("%#v %v %q", out, order, diag.String())
}
}