Partition flag occurrences once per invocation
This commit is contained in:
@@ -303,3 +303,43 @@ func TestInvocationFeaturesChainContextsAndReports(t *testing.T) {
|
||||
t.Fatalf("%#v %v %q", out, order, diag.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFeatureOccurrencePartitionsPreserveCapturedOrder(t *testing.T) {
|
||||
type inner struct{ order []string }
|
||||
type outer struct{ values FlagValues[inner] }
|
||||
got := make(map[string][]string)
|
||||
bindings := make(map[string][]Flag[inner])
|
||||
for _, prefix := range []string{"first", "second", "local"} {
|
||||
for _, suffix := range []string{"a", "b"} {
|
||||
bindings[prefix] = append(bindings[prefix], OccurrenceFlag(FlagMeta{Name: prefix + "-" + suffix}, func(s string) (string, error) { return s, nil }, func(i *inner, v Supplied[string]) {
|
||||
if v.Set {
|
||||
i.order = append(i.order, v.Value)
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
capture := func(prefix string) []Flag[outer] {
|
||||
return CaptureFlags(bindings[prefix], func(o *outer) *FlagValues[inner] { return &o.values })
|
||||
}
|
||||
apply := func(prefix string, o outer) {
|
||||
var i inner
|
||||
if err := o.values.ApplyTo(&i, bindings[prefix]...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got[prefix] = i.order
|
||||
}
|
||||
feature := func(prefix string) InvocationFeature {
|
||||
return NewInvocationFeature(InvocationFeatureSpec[outer]{Flags: capture(prefix), Start: func(inv Invocation, o outer) (context.Context, func() Report) { apply(prefix, o); return nil, nil }})
|
||||
}
|
||||
cmd := NewCommand(CommandSpec[outer]{Name: "run", Flags: capture("local"), Run: func(inv Invocation, o outer) Outcome { apply("local", o); return Success() }})
|
||||
cmd = WithInvocationFeature(WithInvocationFeature(cmd, feature("first")), feature("second"))
|
||||
cmd = WithInvocationFeature(cmd, NewInvocationFeature(InvocationFeatureSpec[struct{}]{Start: func(Invocation, struct{}) (context.Context, func() Report) { return nil, nil }}))
|
||||
app, err := New(Identity{Name: "test"}, cmd, FlagAlias("--pick", "--first-b"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result := app.Execute(context.Background(), []string{"run", "--pick=B", "--local-b=L2", "--second-a=S", "--first-a=A", "--local-a=L1"}, Streams{})
|
||||
if result.Status != 0 || !reflect.DeepEqual(got, map[string][]string{"first": {"B", "A"}, "second": {"S"}, "local": {"L2", "L1"}}) {
|
||||
t.Fatalf("%#v %#v", result, got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user