Share invocation flag lookup across installation contexts

This commit is contained in:
2026-11-22 22:24:06 +00:00
parent 3415d9337d
commit 3e4309f084
4 changed files with 78 additions and 20 deletions

View File

@@ -343,3 +343,41 @@ func TestFeatureOccurrencePartitionsPreserveCapturedOrder(t *testing.T) {
t.Fatalf("%#v %#v", result, got)
}
}
func TestSharedInvocationSpellingsActivateAliasesAndPreserveRollback(t *testing.T) {
starts := 0
feature := NewInvocationFeature(InvocationFeatureSpec[struct{}]{Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "observe", Short: "o"}, false, func(*struct{}, Supplied[bool]) {})}, Start: func(Invocation, struct{}) (context.Context, func() Report) { starts++; return nil, nil }})
run := NewCommand(CommandSpec[struct{}]{Name: "run", Run: func(Invocation, struct{}) Outcome { return Success() }})
app, err := New(Identity{Name: "test"}, run, Alias("deep run", "run"), FlagAlias("--watch", "--observe"))
if err != nil {
t.Fatal(err)
}
if r := app.Execute(context.Background(), []string{"run", "--watch"}, Streams{}); r.Kind != OutcomeUsage {
t.Fatalf("inactive alias: %#v", r)
}
if err := app.Register(WithInvocationFeature(Group("owner", ""), feature), FlagAliasFor("run", "--inspect", "--observe")); err != nil {
t.Fatal(err)
}
for _, args := range [][]string{{"run", "--watch"}, {"deep", "run", "--inspect"}, {"run", "-o"}} {
if r := app.Execute(context.Background(), args, Streams{}); r.Status != 0 {
t.Fatalf("%v: %#v", args, r)
}
}
if starts != 3 {
t.Fatalf("starts=%d", starts)
}
var out bytes.Buffer
if r := app.Execute(context.Background(), []string{"deep", "--watch", "--help"}, Streams{Out: &out}); r.Status != 0 || !strings.Contains(out.String(), "--observe") || starts != 3 {
t.Fatalf("discovery: %#v %q", r, out.String())
}
conflict := NewCommand(CommandSpec[struct{}]{Name: "conflict", Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "other", Short: "o"}, false, func(*struct{}, Supplied[bool]) {})}})
if err := app.Register(conflict); err == nil {
t.Fatal("short spelling collision accepted")
}
if err := app.Register(FlagAlias("--observe", "--verbose")); err == nil {
t.Fatal("canonical alias source collision accepted")
}
if r := app.Execute(context.Background(), []string{"deep", "run", "--watch"}, Streams{}); r.Status != 0 || starts != 4 {
t.Fatalf("rollback: %#v starts=%d", r, starts)
}
}