Align shared debug help and completion
This commit is contained in:
@@ -120,3 +120,51 @@ func TestCompletionQueryDiscovery(t *testing.T) {
|
||||
t.Fatalf("discovery outcome=%#v reply=%q", outcome, output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedDebugHelpAndCompletion(t *testing.T) {
|
||||
var factories, setters, checks, handlers int
|
||||
command := NewCommand(CommandSpec[struct{}]{
|
||||
Name: "run",
|
||||
NewInput: func() struct{} { factories++; return struct{}{} },
|
||||
Flags: []Flag[struct{}]{BoolFlag(FlagMeta{Name: "debug", Short: "d", Usage: "application tracing", Required: true, Completion: CompleteChoices(CompletionChoice{Value: "true"}, CompletionChoice{Value: "trace"})}, true, func(*struct{}, Supplied[bool]) { setters++ })},
|
||||
CheckInput: func(struct{}) error { checks++; return nil },
|
||||
Run: func(Invocation, struct{}) Outcome { handlers++; return Success() },
|
||||
})
|
||||
app, err := New(Identity{Name: "example"}, command, Completion("example"), Alias("shortcut", "run"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{{"run", "--help"}, {"help", "run"}, {"shortcut", "--help"}, {"help", "shortcut"}, {"run", "--unknown"}, {"shortcut", "--unknown"}} {
|
||||
var output, diagnostic bytes.Buffer
|
||||
result := app.Execute(context.Background(), args, Streams{Out: &output, Err: &diagnostic})
|
||||
text := output.String() + diagnostic.String()
|
||||
if strings.Count(text, "--debug") != 1 || !strings.Contains(text, "-d") || !strings.Contains(text, "application tracing") || !strings.Contains(text, "application input defaults to true") || !strings.Contains(text, "required") || !strings.Contains(text, "default: false") {
|
||||
t.Fatalf("%v: outcome=%#v text=%q", args, result, text)
|
||||
}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
args []string
|
||||
want string
|
||||
count int
|
||||
}{
|
||||
{[]string{"__complete", "run", "--deb"}, "--debug", 1},
|
||||
{[]string{"__complete", "run", "-"}, "\n-d\t", 1},
|
||||
{[]string{"__complete", "run", "--debug=tr"}, "trace", 1},
|
||||
{[]string{"__complete", "run", "-d=tr"}, "trace", 1},
|
||||
} {
|
||||
var output bytes.Buffer
|
||||
result := app.Execute(context.Background(), tc.args, Streams{Out: &output, Err: io.Discard})
|
||||
if result.Kind != OutcomeSuccess || strings.Count(output.String(), tc.want) != tc.count {
|
||||
t.Fatalf("%v: %#v %q", tc.args, result, output.String())
|
||||
}
|
||||
}
|
||||
if factories != 0 || setters != 0 || checks != 0 || handlers != 0 {
|
||||
t.Fatalf("callbacks: %d %d %d %d", factories, setters, checks, handlers)
|
||||
}
|
||||
if result := app.Execute(context.Background(), []string{"run", "--debug=invalid", "--help"}, Streams{Out: io.Discard, Err: io.Discard}); result.Kind != OutcomeUsage {
|
||||
t.Fatalf("help skipped boolean validation: %#v", result)
|
||||
}
|
||||
if result := app.Execute(context.Background(), []string{"run", "--debug=trace"}, Streams{Out: io.Discard, Err: io.Discard}); result.Kind != OutcomeUsage {
|
||||
t.Fatalf("suggestion bypassed boolean parsing: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user