Check PromptKit selector collisions against installed flags

This commit is contained in:
2026-10-07 16:51:15 +00:00
parent c26d279b4f
commit a21dd3d89e
4 changed files with 53 additions and 3 deletions

View File

@@ -404,3 +404,47 @@ func sortedKeys(values map[string]any) []string {
}
return keys
}
func TestPromptKitSelectorNamesCollideOnlyWithInstalledFlags(t *testing.T) {
for _, name := range []string{"version", "scope"} {
var captured commandkit.Supplied[string]
suite := commandkit.PromptKitSuite[commandkit.Supplied[string]]{
Selectors: []commandkit.Selector[commandkit.Supplied[string]]{{Binding: commandkit.ValueFlag[commandkit.Supplied[string]](commandkit.FlagMeta{Name: name}, nil, func(s string) (string, error) { return s, nil }, func(c *commandkit.Supplied[string], v commandkit.Supplied[string]) { *c = v })}},
InspectProfile: func(_ context.Context, _ commandkit.Selection, c commandkit.Supplied[string], _ string) (commandkit.Acquired[*promptkit.ProfileInspection], error) {
captured = c
return commandkit.Acquired[*promptkit.ProfileInspection]{Value: &promptkit.ProfileInspection{}}, nil
},
}
app, _ := commandkit.New(commandkit.Identity{Name: "example"})
if err := commandkit.RegisterPromptKit(app, suite); err != nil {
t.Fatalf("%s: %v", name, err)
}
result := app.Execute(context.Background(), []string{"promptkit", "show", "profile", "example", "--" + name, "selected"}, commandkit.Streams{})
if result.Kind != commandkit.OutcomeSuccess || captured != (commandkit.Supplied[string]{Value: "selected", Set: true}) {
t.Fatalf("%s: result=%#v captured=%#v", name, result, captured)
}
suite.InspectPrompt = func(context.Context, commandkit.Selection, commandkit.Supplied[string], string, string) (commandkit.Acquired[*promptkit.PromptInspection], error) {
return commandkit.Acquired[*promptkit.PromptInspection]{Value: &promptkit.PromptInspection{}}, nil
}
other, _ := commandkit.New(commandkit.Identity{Name: "example"})
err := commandkit.RegisterPromptKit(other, suite)
if name == "version" && err == nil {
t.Fatal("accepted collision with installed prompt version flag")
}
if name == "scope" && err != nil {
t.Fatalf("scope selector conflicts with no installed flag: %v", err)
}
}
for _, name := range []string{"config", "overlay", "format", "help"} {
app, _ := commandkit.New(commandkit.Identity{Name: "example"})
suite := commandkit.PromptKitSuite[struct{}]{
InspectProfile: func(context.Context, commandkit.Selection, struct{}, string) (commandkit.Acquired[*promptkit.ProfileInspection], error) {
return commandkit.Acquired[*promptkit.ProfileInspection]{}, nil
},
Selectors: []commandkit.Selector[struct{}]{{Binding: commandkit.ValueFlag[struct{}](commandkit.FlagMeta{Name: name}, nil, func(s string) (string, error) { return s, nil }, func(*struct{}, commandkit.Supplied[string]) {})}},
}
if err := commandkit.RegisterPromptKit(app, suite); err == nil {
t.Fatalf("accepted standard flag collision --%s", name)
}
}
}