Parse invocation flags across command discovery
This commit is contained in:
@@ -5,6 +5,9 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
internalcobra "go.maximumdirect.net/commandkit/internal/cobra"
|
||||
commandmeta "go.maximumdirect.net/commandkit/internal/commandmeta"
|
||||
)
|
||||
|
||||
func TestInvocationFeatureCaptureAndInstallation(t *testing.T) {
|
||||
@@ -95,3 +98,56 @@ func TestInvocationFeatureRegistrationOrderAndAliasCollision(t *testing.T) {
|
||||
t.Fatalf("feature alias rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvocationFeatureFlagsParseAndDiscover(t *testing.T) {
|
||||
type featureInput struct{ selected Supplied[bool] }
|
||||
constructed := 0
|
||||
feature := NewInvocationFeature(InvocationFeatureSpec[featureInput]{
|
||||
NewInput: func() featureInput { constructed++; return featureInput{} },
|
||||
Flags: []Flag[featureInput]{BoolFlag(FlagMeta{Name: "observe", Short: "o"}, false, func(i *featureInput, v Supplied[bool]) { i.selected = v })},
|
||||
Start: func(inv Invocation, _ featureInput) (context.Context, func() Report) {
|
||||
t.Fatal("unexpected start")
|
||||
return nil, nil
|
||||
},
|
||||
})
|
||||
type localInput struct{ name string }
|
||||
var got string
|
||||
cmd := NewCommand(CommandSpec[localInput]{Name: "run", Flags: []Flag[localInput]{ValueFlag(FlagMeta{Name: "name"}, (*string)(nil), func(s string) (string, error) { return s, nil }, func(i *localInput, v Supplied[string]) { i.name = v.Value })}, Run: func(_ Invocation, i localInput) Outcome { got = i.name; return Success() }})
|
||||
app, err := New(Identity{Name: "test"}, WithInvocationFeature(cmd, feature), Alias("short path", "run"), FlagAlias("--watch", "--observe"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out := app.Execute(context.Background(), []string{"run", "--watch", "--observe=false", "--name=ok"}, Streams{}); out.Status != 0 || got != "ok" {
|
||||
t.Fatalf("result=%#v name=%q", out, got)
|
||||
}
|
||||
if out := app.Execute(context.Background(), []string{"run", "--observe=bad", "--observe=true"}, Streams{}); out.Kind != OutcomeUsage {
|
||||
t.Fatalf("invalid earlier occurrence: %#v", out)
|
||||
}
|
||||
if out := app.Execute(context.Background(), []string{"run", "--observe=bad", "--help"}, Streams{}); out.Kind != OutcomeUsage {
|
||||
t.Fatalf("help invalid flag: %#v", out)
|
||||
}
|
||||
if constructed != 0 {
|
||||
t.Fatal("input constructed before execution boundary")
|
||||
}
|
||||
snapshot := app.routeSnapshot()
|
||||
for _, path := range [][]string{nil, {"run"}, {"short"}} {
|
||||
route := resolveEnteredRoute(path, snapshot)
|
||||
view := app.helpView(route, snapshot)
|
||||
session := internalcobra.Prepare(snapshot.metadata, commandmeta.ParseOptions{})
|
||||
content, failure := session.RenderHelpView(view)
|
||||
if failure != nil || !strings.Contains(content, "--observe") {
|
||||
t.Fatalf("help %v: %q %#v", path, content, failure)
|
||||
}
|
||||
ctx := resolveCompletionContext(snapshot, path, "--obs")
|
||||
target := completionTargetFor(snapshot, ctx)
|
||||
found := false
|
||||
for _, choice := range target.choices {
|
||||
if choice.Value == "--observe" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("completion %v: %#v", path, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user