Keep nested help commands with application handlers
This commit is contained in:
@@ -340,6 +340,61 @@ func TestExecuteHelpSkipsRequiredInputsAndRejectsNilContext(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteRunsApplicationOwnedNestedHelp(t *testing.T) {
|
||||
type input struct{ topic commandkit.Supplied[string] }
|
||||
var received []input
|
||||
domainCause := errors.New("application help result")
|
||||
help, err := commandkit.NewCommand(commandkit.CommandSpec[input]{
|
||||
Name: "help",
|
||||
Description: "Explain a session topic.",
|
||||
Arguments: []commandkit.Argument[input]{commandkit.ValueArgument(
|
||||
commandkit.ArgumentMeta{Name: "TOPIC"}, true,
|
||||
func(value string) (string, error) { return value, nil },
|
||||
func(target *input, value commandkit.Supplied[string]) { target.topic = value },
|
||||
)},
|
||||
Run: func(_ commandkit.Invocation, value input) commandkit.Outcome {
|
||||
received = append(received, value)
|
||||
return commandkit.Domain("session-help", 7, domainCause)
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session, err := commandkit.Group("session", "Session commands.", help)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
app, err := commandkit.New(commandkit.Identity{Name: "example"}, session)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"session", "help", "restore"}, commandkit.Streams{Err: &diagnostic})
|
||||
if outcome.Kind != commandkit.OutcomeDomain || outcome.Domain != "session-help" || outcome.Status != 7 || !errors.Is(outcome.Cause, domainCause) {
|
||||
t.Fatalf("nested help handler outcome = %#v, diagnostics %q", outcome, diagnostic.String())
|
||||
}
|
||||
if len(received) != 1 || received[0].topic != (commandkit.Supplied[string]{Value: "restore", Set: true}) {
|
||||
t.Fatalf("nested help handler input = %#v", received)
|
||||
}
|
||||
|
||||
for _, args := range [][]string{{"session", "help", "--help"}, {"help", "session", "help"}} {
|
||||
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
||||
var output, diagnostics bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||
if outcome.Kind != commandkit.OutcomeSuccess || outcome.Status != 0 || diagnostics.Len() != 0 {
|
||||
t.Fatalf("metadata help outcome = %#v, diagnostics %q", outcome, diagnostics.String())
|
||||
}
|
||||
if !strings.Contains(output.String(), "Explain a session topic.") || !strings.Contains(output.String(), "TOPIC") {
|
||||
t.Fatalf("metadata help omitted nested command details: %q", output.String())
|
||||
}
|
||||
if len(received) != 1 {
|
||||
t.Fatalf("metadata help invoked the application handler: %#v", received)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpRendersRegisteredMetadataAndResolvesExactPaths(t *testing.T) {
|
||||
type input struct {
|
||||
count commandkit.Supplied[int]
|
||||
|
||||
Reference in New Issue
Block a user