diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 06:16:15 -0500 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 06:16:15 -0500 |
| commit | c9b15a7c574929d29d4caa3bca27fd0d0755bbeb (patch) | |
| tree | 3be0574d75f85dabfa06a1896087b40a4ed9046e /subcmd_test.go | |
| parent | Release v0.3.0 (diff) | |
| download | subcmd-c9b15a7c574929d29d4caa3bca27fd0d0755bbeb.zip | |
Fix -h flag and use functional options
Diffstat (limited to 'subcmd_test.go')
| -rw-r--r-- | subcmd_test.go | 95 |
1 files changed, 42 insertions, 53 deletions
diff --git a/subcmd_test.go b/subcmd_test.go index 57b86c2..bd3c083 100644 --- a/subcmd_test.go +++ b/subcmd_test.go @@ -33,32 +33,6 @@ func (m *mockCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...any) return m.exitStatus } -// testRegistryOption configures a test registry. -type testRegistryOption func(*Registry) - -// withCommand adds a command to the registry. -func withCommand(cmd Command, group string) testRegistryOption { - return func(r *Registry) { - r.Register(cmd, group) - } -} - -// withBuffer sets the output writer for the registry. -func withBuffer(buf *bytes.Buffer) testRegistryOption { - return func(r *Registry) { - r.SetOutput(buf) - } -} - -// newTestRegistry creates a registry configured for testing with -// the given options. -func newTestRegistry(opts ...testRegistryOption) *Registry { - r := NewRegistry() - for _, opt := range opts { - opt(r) - } - return r -} // setupOsArgs sets up os.Args and flag.CommandLine for testing, // returning a cleanup function that restores the original state. @@ -85,7 +59,7 @@ func TestRegistry_Register(t *testing.T) { func TestRegistry_Execute_NoSubcommand(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry(withBuffer(&buf)) + r := NewRegistry(WithOutput(&buf)) cleanup := setupOsArgs("prog") defer cleanup() @@ -99,7 +73,7 @@ func TestRegistry_Execute_NoSubcommand(t *testing.T) { func TestRegistry_Execute_UnknownSubcommand(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry(withBuffer(&buf)) + r := NewRegistry(WithOutput(&buf)) cleanup := setupOsArgs("prog", "unknown") defer cleanup() @@ -115,10 +89,8 @@ func TestRegistry_Execute_Success(t *testing.T) { name: "test", exitStatus: ExitSuccess, } - r := newTestRegistry( - withBuffer(&buf), - withCommand(cmd, ""), - ) + r := NewRegistry(WithOutput(&buf)) + r.Register(cmd, "") cleanup := setupOsArgs("prog", "test") defer cleanup() @@ -134,7 +106,8 @@ func TestRegistry_Execute_WithFlags(t *testing.T) { name: "test", exitStatus: ExitSuccess, } - r := newTestRegistry(withBuffer(&buf), withCommand(cmd, "")) + r := NewRegistry(WithOutput(&buf)) + r.Register(cmd, "") cleanup := setupOsArgs("prog", "test", "-test=value") defer cleanup() @@ -144,15 +117,34 @@ func TestRegistry_Execute_WithFlags(t *testing.T) { check.Equal(t, cmd.flagValue, "value") } +func TestRegistry_Execute_HelpFlag(t *testing.T) { + var buf bytes.Buffer + cmd := &mockCommand{ + name: "test", + usage: "test:\n This is the test command usage.\n", + } + r := NewRegistry( + WithOutput(&buf), + WithErrorHandling(flag.ContinueOnError), + ) + r.Register(cmd, "") + cleanup := setupOsArgs("prog", "test", "-h") + defer cleanup() + + status := r.Execute(context.Background()) + + check.Equal(t, status, ExitFailure) + check.Contains(t, buf.String(), "This is the test command usage.") +} + func TestRegistry_Commands(t *testing.T) { cmd1 := &mockCommand{name: "aaa", synopsis: "First"} cmd2 := &mockCommand{name: "bbb", synopsis: "Second"} cmd3 := &mockCommand{name: "ccc", synopsis: "Third"} - r := newTestRegistry( - withCommand(cmd2, "group1"), - withCommand(cmd1, "group1"), - withCommand(cmd3, "group2"), - ) + r := NewRegistry() + r.Register(cmd2, "group1") + r.Register(cmd1, "group1") + r.Register(cmd3, "group2") commands := r.Commands() @@ -199,11 +191,9 @@ func TestHelpCommand_NoArgs(t *testing.T) { var buf bytes.Buffer cmd1 := &mockCommand{name: "test1", synopsis: "Test command 1"} cmd2 := &mockCommand{name: "test2", synopsis: "Test command 2"} - r := newTestRegistry( - withBuffer(&buf), - withCommand(cmd1, "group1"), - withCommand(cmd2, "group1"), - ) + r := NewRegistry(WithOutput(&buf)) + r.Register(cmd1, "group1") + r.Register(cmd2, "group1") help := &helpCommand{registry: r} r.Register(help, "") fs := flag.NewFlagSet("help", flag.ExitOnError) @@ -226,7 +216,8 @@ func TestHelpCommand_WithArg(t *testing.T) { name: "test", usage: "test:\n This is a test command\n", } - r := newTestRegistry(withBuffer(&buf), withCommand(cmd, "")) + r := NewRegistry(WithOutput(&buf)) + r.Register(cmd, "") help := &helpCommand{registry: r} r.Register(help, "") fs := flag.NewFlagSet("help", flag.ExitOnError) @@ -240,7 +231,7 @@ func TestHelpCommand_WithArg(t *testing.T) { func TestHelpCommand_UnknownCommand(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry(withBuffer(&buf)) + r := NewRegistry(WithOutput(&buf)) help := &helpCommand{registry: r} fs := flag.NewFlagSet("help", flag.ExitOnError) fs.Parse([]string{"unknown"}) @@ -293,12 +284,10 @@ func TestManHelpCommand_WithSubcommand(t *testing.T) { func TestCommandsCommand(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry( - withBuffer(&buf), - withCommand(&mockCommand{name: "zzz"}, ""), - withCommand(&mockCommand{name: "aaa"}, ""), - withCommand(&mockCommand{name: "mmm"}, ""), - ) + r := NewRegistry(WithOutput(&buf)) + r.Register(&mockCommand{name: "zzz"}, "") + r.Register(&mockCommand{name: "aaa"}, "") + r.Register(&mockCommand{name: "mmm"}, "") cmds := &commandsCommand{registry: r} r.Register(cmds, "") fs := flag.NewFlagSet("commands", flag.ExitOnError) @@ -318,7 +307,7 @@ func TestCommandsCommand(t *testing.T) { func TestFlagsCommand_NoFlags(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry(withBuffer(&buf)) + r := NewRegistry(WithOutput(&buf)) flags := &flagsCommand{registry: r} fs := flag.NewFlagSet("flags", flag.ExitOnError) @@ -330,7 +319,7 @@ func TestFlagsCommand_NoFlags(t *testing.T) { func TestFlagsCommand_WithFlags(t *testing.T) { var buf bytes.Buffer - r := newTestRegistry(withBuffer(&buf)) + r := NewRegistry(WithOutput(&buf)) fs := flag.NewFlagSet("flags", flag.ExitOnError) flag.String("testflag", "default", "test flag description") flags := &flagsCommand{registry: r} |