Discover help routes through intermediate aliases
This commit is contained in:
@@ -239,6 +239,90 @@ func TestDiscoveryHelpComposesGroupShortcutsAndOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpDiscoversRoutesThroughIntermediateAliases(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
nested bool
|
||||
override bool
|
||||
expanded bool
|
||||
}{
|
||||
{name: "group"},
|
||||
{name: "discovery", nested: true},
|
||||
{name: "discovery override", nested: true, override: true},
|
||||
{name: "expanded chain", nested: true, expanded: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
built, runs := 0, 0
|
||||
var canonical, entered string
|
||||
makeCommand := func(name string) Command {
|
||||
return NewCommand(CommandSpec[struct{}]{
|
||||
Name: name, NewInput: func() struct{} { built++; return struct{}{} },
|
||||
Flags: []Flag[struct{}]{ValueFlag(FlagMeta{Name: "format"}, (*string)(nil),
|
||||
func(value string) (string, error) { return value, nil }, func(*struct{}, Supplied[string]) {})},
|
||||
Run: func(inv Invocation, _ struct{}) Outcome {
|
||||
runs++
|
||||
entered, canonical = inv.EnteredPath(), inv.CanonicalPath()
|
||||
return Success()
|
||||
},
|
||||
})
|
||||
}
|
||||
group, target := Group("box", "Box"), "box"
|
||||
if test.expanded {
|
||||
group, target = Group("outer", "Outer", group), "outer box"
|
||||
}
|
||||
prefix, middle := "p", "q"
|
||||
if test.nested {
|
||||
prefix, middle = "p admin", "q admin"
|
||||
}
|
||||
declarations := []Declaration{group, makeCommand("inspect"), makeCommand("alternate"),
|
||||
Alias("p", "q"), Alias("q", target), Alias(middle+" pick", "inspect"), Alias(middle+" other", "inspect"),
|
||||
FlagAlias("--hlep", "--help"), FlagAliasFor("inspect", "--fmt", "--format")}
|
||||
wantTarget := "inspect"
|
||||
if test.override {
|
||||
declarations = append(declarations, Alias(prefix+" pick", "alternate"))
|
||||
wantTarget = "alternate"
|
||||
}
|
||||
app, err := New(Identity{Name: "Demo"}, declarations...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
append(strings.Fields(prefix), "--help"),
|
||||
append([]string{"help"}, strings.Fields(prefix)...),
|
||||
append(strings.Fields(prefix), "--hlep"),
|
||||
} {
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Kind != OutcomeSuccess {
|
||||
t.Errorf("Execute(%q) = %v, diagnostics %q", args, outcome.Kind, diagnostics.String())
|
||||
}
|
||||
for _, fragment := range []string{"Demo " + prefix, "pick → " + wantTarget, "other → inspect", "--hlep → --help"} {
|
||||
if !strings.Contains(output.String(), fragment) {
|
||||
t.Errorf("help %q omits %q", output.String(), fragment)
|
||||
}
|
||||
}
|
||||
if strings.Contains(output.String(), "--fmt") || strings.Contains(output.String(), "--format") ||
|
||||
(test.override && strings.Contains(output.String(), "pick → inspect")) {
|
||||
t.Errorf("help exposes a descendant flag or overridden destination: %q", output.String())
|
||||
}
|
||||
}
|
||||
if built != 0 || runs != 0 {
|
||||
t.Fatalf("help built %d inputs and ran %d handlers", built, runs)
|
||||
}
|
||||
for _, suffix := range []string{"pick", "other"} {
|
||||
outcome := app.Execute(context.Background(), strings.Fields(prefix+" "+suffix), Streams{Out: io.Discard, Err: io.Discard})
|
||||
want := "inspect"
|
||||
if suffix == "pick" {
|
||||
want = wantTarget
|
||||
}
|
||||
if outcome.Kind != OutcomeSuccess || canonical != want || entered != prefix+" "+suffix {
|
||||
t.Errorf("route %s %s: outcome %v, entered %q, canonical %q", prefix, suffix, outcome.Kind, entered, canonical)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryHelpThroughExpandedGroupPath(t *testing.T) {
|
||||
app, err := New(Identity{Name: "Demo"}, Group("outer", "Outer", Group("box", "Box")),
|
||||
simpleRunnable("inspect"), Alias("pk", "outer box"), Alias("outer box admin pick", "inspect"))
|
||||
|
||||
Reference in New Issue
Block a user