Install shared reporting flags
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
commandmeta "gitea.maximumdirect.net/eric/CommandKit/internal/commandmeta"
|
||||
"gitea.maximumdirect.net/eric/CommandKit/internal/commandpath"
|
||||
)
|
||||
|
||||
@@ -74,12 +75,16 @@ func TestFlagAliasChainsUseScopedOverridesAndCanonicalEndpoints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFlagAliasDiscoveryContextsUseOnlyGlobalHelpAliases(t *testing.T) {
|
||||
func TestFlagAliasDiscoveryContextsUseGlobalReportingAliases(t *testing.T) {
|
||||
show := flagAliasCommand("show")
|
||||
app, err := New(Identity{Name: "Example"},
|
||||
Group("Tools", "tools", show),
|
||||
Alias("shortcut show", "Tools show"),
|
||||
FlagAlias("--hlep", "--help"),
|
||||
FlagAlias("--silent", "--quiet"),
|
||||
FlagAlias("--chatty", "--verbose"),
|
||||
FlagAlias("--trace", "--debug"),
|
||||
FlagAlias("--show-progress", "--progress"),
|
||||
FlagAliasFor("Tools show", "--local-help", "--help"),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -97,11 +102,93 @@ func TestFlagAliasDiscoveryContextsUseOnlyGlobalHelpAliases(t *testing.T) {
|
||||
if got := discovery[globalHelp].target.token(); got != "--help" {
|
||||
t.Fatalf("discovery --hlep target = %s, want --help", got)
|
||||
}
|
||||
for label, aliases := range map[string]resolvedFlagAliases{
|
||||
"root": app.flagAliases[commandpath.Path(nil).Key()],
|
||||
"group": app.flagAliases[commandpath.Path{"Tools"}.Key()],
|
||||
"command": app.flagAliases[commandpath.Path{"Tools", "show"}.Key()],
|
||||
"discovery": app.discoveryFlags[commandpath.Path{"shortcut"}.Key()],
|
||||
} {
|
||||
for source, target := range map[string]string{
|
||||
"silent": "quiet",
|
||||
"chatty": "verbose",
|
||||
"trace": "debug",
|
||||
"show-progress": "progress",
|
||||
} {
|
||||
key := flagSpellingKey(capturedFlagSpelling{name: source})
|
||||
if got := aliases.bySpelling[key].target.token(); got != "--"+target {
|
||||
t.Errorf("%s --%s target = %s, want --%s", label, source, got, target)
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, inherited := discovery[flagSpellingKey(capturedFlagSpelling{name: "local-help"})]; inherited {
|
||||
t.Fatal("discovery context inherited a command-scoped alias")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportingFlagsAreReservedAndAcceptApplicationShortAliases(t *testing.T) {
|
||||
for _, name := range []string{"quiet", "verbose", "debug", "progress"} {
|
||||
command := flagAliasCommand("reserved", stringFlag(name, ""))
|
||||
if len(command.record.issues) == 0 {
|
||||
t.Errorf("capturing --%s did not retain a declaration error", name)
|
||||
}
|
||||
app, err := New(Identity{Name: "Example"}, flagAliasCommand("safe"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot := app.routeSnapshot()
|
||||
if err := app.Register(command, Alias("reserved-shortcut", "reserved")); err == nil || !strings.Contains(err.Error(), "reserved for reporting") {
|
||||
t.Errorf("registering --%s returned %v, want a retained reporting reservation error", name, err)
|
||||
}
|
||||
if app.routeSnapshot() != snapshot || app.commands["reserved"] != nil || app.commands["safe"] == nil || len(app.routingRules) != 0 {
|
||||
t.Fatalf("failed registration of --%s changed the installed commands", name)
|
||||
}
|
||||
}
|
||||
|
||||
declarations := []Declaration{flagAliasCommand("run")}
|
||||
for short, target := range map[string]string{"q": "quiet", "v": "verbose", "d": "debug"} {
|
||||
declarations = append(declarations, FlagAlias("-"+short, "--"+target))
|
||||
}
|
||||
if _, err := New(Identity{Name: "Example"}, declarations...); err != nil {
|
||||
t.Fatalf("application short aliases for reporting flags were rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportingFlagSpellingsConflictWithAliasSources(t *testing.T) {
|
||||
for _, name := range []string{"quiet", "verbose", "debug", "progress"} {
|
||||
if _, err := New(Identity{Name: "Example"}, FlagAlias("--"+name, "--help")); err == nil || !strings.Contains(err.Error(), "conflicts with a canonical spelling") {
|
||||
t.Errorf("alias source --%s returned %v, want a canonical spelling collision", name, err)
|
||||
}
|
||||
}
|
||||
if _, err := New(Identity{Name: "Example"}, Group("tools", "", flagAliasCommand("show")), FlagAliasFor("tools", "--quiet", "--help")); err == nil || !strings.Contains(err.Error(), "conflicts with a canonical spelling") {
|
||||
t.Errorf("group-scoped canonical reporting spelling returned %v, want a collision", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoutingSnapshotCarriesReportingMetadataOutsideConsumerBindings(t *testing.T) {
|
||||
app, err := New(Identity{Name: "Example"},
|
||||
flagAliasCommand("run", stringFlag("format", "f")),
|
||||
Group("tools", "", flagAliasCommand("show")),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var check func(*commandmeta.Command)
|
||||
check = func(node *commandmeta.Command) {
|
||||
if len(node.CoreFlags) != 4 {
|
||||
t.Errorf("%s core reporting flags = %#v", node.Name, node.CoreFlags)
|
||||
}
|
||||
for _, flag := range node.Flags {
|
||||
if commandmeta.IsReportingFlag(flag.Name) {
|
||||
t.Errorf("%s reporting flag %q was added to consumer bindings", node.Name, flag.Name)
|
||||
}
|
||||
}
|
||||
for _, child := range node.Children {
|
||||
check(child)
|
||||
}
|
||||
}
|
||||
check(app.routeSnapshot().metadata)
|
||||
}
|
||||
|
||||
func TestFlagAliasDuplicatesAndScopesAreDeterministic(t *testing.T) {
|
||||
command := flagAliasCommand("inspect", stringFlag("color", "c"), stringFlag("format", "f"))
|
||||
duplicate := FlagAlias("--colour", "--color")
|
||||
|
||||
Reference in New Issue
Block a user