Require exact resolved paths for explicit help
This commit is contained in:
@@ -94,6 +94,44 @@ func TestRunnableCommandCanHaveAliasOnlyDiscoveryPrefixes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExplicitHelpRequiresAnExactResolvedTarget(t *testing.T) {
|
||||
initialized := false
|
||||
run := NewCommand(CommandSpec[struct{}]{
|
||||
Name: "run", NewInput: func() struct{} { initialized = true; return struct{}{} },
|
||||
Arguments: []Argument[struct{}]{RepeatArgument(ArgumentMeta{Name: "ARG"}, 2, 2,
|
||||
func(value string) (string, error) { return value, nil }, func(*struct{}, Supplied[[]string]) {})},
|
||||
Run: func(Invocation, struct{}) Outcome { t.Error("help ran the command"); return Success() },
|
||||
})
|
||||
app, err := New(Identity{Name: "Demo"}, run, Group("box", "Box", simpleRunnable("show")),
|
||||
Alias("r", "run"), Alias("b", "box"), Alias("list show", "box show"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
{"help", "run", "first", "second"}, {"help", "r", "first"},
|
||||
{"help", "r", "first", "second"}, {"help", "r", "first", "second", "third"},
|
||||
} {
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Kind != OutcomeUsage || output.Len() != 0 {
|
||||
t.Errorf("Execute(%q) = %v, output %q, diagnostics %q", args, outcome.Kind, output.String(), diagnostics.String())
|
||||
}
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
{"help", "run"}, {"help", "r"}, {"help", "box"}, {"help", "b"},
|
||||
{"help", "list"}, {"r", "first", "second", "--help"},
|
||||
} {
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Kind != OutcomeSuccess || !strings.Contains(output.String(), "Usage:") {
|
||||
t.Errorf("Execute(%q) = %v, output %q, diagnostics %q", args, outcome.Kind, output.String(), diagnostics.String())
|
||||
}
|
||||
}
|
||||
if initialized {
|
||||
t.Fatal("help initialized command input")
|
||||
}
|
||||
}
|
||||
|
||||
func routeTestApp(t *testing.T, extra ...Declaration) *App {
|
||||
t.Helper()
|
||||
collections := simpleRunnable("collections")
|
||||
|
||||
Reference in New Issue
Block a user