aboutsummaryrefslogtreecommitdiffstats
path: root/subcmd_test.go
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-13 09:38:18 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-10-13 09:38:18 -0400
commitec47c447fe112f3aa3e8b42c914f0bdc2a649e7e (patch)
treea85e4fba976bea69f098b7c672c883a7627a3186 /subcmd_test.go
downloadsubcmd-ec47c447fe112f3aa3e8b42c914f0bdc2a649e7e.zip
Initial Commit
Diffstat (limited to 'subcmd_test.go')
-rw-r--r--subcmd_test.go342
1 files changed, 342 insertions, 0 deletions
diff --git a/subcmd_test.go b/subcmd_test.go
new file mode 100644
index 0000000..726cef9
--- /dev/null
+++ b/subcmd_test.go
@@ -0,0 +1,342 @@
+package subcmd
+
+import (
+ "bytes"
+ "context"
+ "flag"
+ "os"
+ "strings"
+ "testing"
+
+ "git.sr.ht/~eugenetriguba/checkmate/check"
+)
+
+// mockCommand is a simple test command
+type mockCommand struct {
+ name string
+ synopsis string
+ usage string
+ exitStatus ExitStatus
+ executed bool
+ flagValue string
+}
+
+func (m *mockCommand) Name() string { return m.name }
+func (m *mockCommand) Synopsis() string { return m.synopsis }
+func (m *mockCommand) Usage() string { return m.usage }
+
+func (m *mockCommand) SetFlags(f *flag.FlagSet) {
+ f.StringVar(&m.flagValue, "test", "", "test flag")
+}
+
+func (m *mockCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...any) ExitStatus {
+ m.executed = true
+ return m.exitStatus
+}
+
+func TestRegistry_Register(t *testing.T) {
+ r := NewRegistry()
+
+ cmd1 := &mockCommand{name: "test1", synopsis: "Test command 1"}
+ cmd2 := &mockCommand{name: "test2", synopsis: "Test command 2"}
+
+ r.Register(cmd1, "")
+ r.Register(cmd2, "group1")
+
+ check.Equal(t, r.Command("test1"), cmd1)
+ check.Equal(t, r.Command("test2"), cmd2)
+ check.Nil(t, r.Command("nonexistent"))
+}
+
+func TestRegistry_Execute_NoSubcommand(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ // Simulate no subcommand
+ oldArgs := os.Args
+ os.Args = []string{"prog"}
+ flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flag.Parse()
+ defer func() { os.Args = oldArgs }()
+
+ status := r.Execute(context.Background())
+
+ check.Equal(t, status, ExitFailure)
+ check.Contains(t, buf.String(), "No subcommand specified")
+}
+
+func TestRegistry_Execute_UnknownSubcommand(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ oldArgs := os.Args
+ os.Args = []string{"prog", "unknown"}
+ flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flag.Parse()
+ defer func() { os.Args = oldArgs }()
+
+ status := r.Execute(context.Background())
+
+ check.Equal(t, status, ExitFailure)
+ check.Contains(t, buf.String(), "Unknown subcommand: unknown")
+}
+
+func TestRegistry_Execute_Success(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ cmd := &mockCommand{
+ name: "test",
+ exitStatus: ExitSuccess,
+ }
+ r.Register(cmd, "")
+
+ oldArgs := os.Args
+ os.Args = []string{"prog", "test"}
+ flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flag.Parse()
+ defer func() { os.Args = oldArgs }()
+
+ status := r.Execute(context.Background())
+
+ check.Equal(t, status, ExitSuccess)
+ check.Equal(t, cmd.executed, true)
+}
+
+func TestRegistry_Execute_WithFlags(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ cmd := &mockCommand{
+ name: "test",
+ exitStatus: ExitSuccess,
+ }
+ r.Register(cmd, "")
+
+ oldArgs := os.Args
+ os.Args = []string{"prog", "test", "-test=value"}
+ flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flag.Parse()
+ defer func() { os.Args = oldArgs }()
+
+ status := r.Execute(context.Background())
+
+ check.Equal(t, status, ExitSuccess)
+ check.Equal(t, cmd.flagValue, "value")
+}
+
+func TestRegistry_Commands(t *testing.T) {
+ r := NewRegistry()
+
+ cmd1 := &mockCommand{name: "aaa", synopsis: "First"}
+ cmd2 := &mockCommand{name: "bbb", synopsis: "Second"}
+ cmd3 := &mockCommand{name: "ccc", synopsis: "Third"}
+
+ r.Register(cmd1, "group1")
+ r.Register(cmd2, "group1")
+ r.Register(cmd3, "group2")
+
+ commands := r.Commands()
+
+ check.Equal(t, len(commands), 2)
+
+ group1 := commands["group1"]
+ check.Equal(t, len(group1), 2)
+
+ // Check sorting
+ check.Equal(t, group1[0].Name(), "aaa")
+ check.Equal(t, group1[1].Name(), "bbb")
+
+ group2 := commands["group2"]
+ check.Equal(t, len(group2), 1)
+}
+
+func TestHelpCommand_NoArgs(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ help := &helpCommand{registry: r}
+ r.Register(help, "")
+
+ cmd1 := &mockCommand{name: "test1", synopsis: "Test command 1"}
+ cmd2 := &mockCommand{name: "test2", synopsis: "Test command 2"}
+ r.Register(cmd1, "group1")
+ r.Register(cmd2, "group1")
+
+ fs := flag.NewFlagSet("help", flag.ExitOnError)
+ status := help.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitSuccess)
+
+ output := buf.String()
+ check.Contains(t, output, "Available commands")
+ check.Contains(t, output, "test1")
+ check.Contains(t, output, "test2")
+ check.Contains(t, output, "Test command 1")
+}
+
+func TestHelpCommand_WithArg(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ help := &helpCommand{registry: r}
+ r.Register(help, "")
+
+ cmd := &mockCommand{
+ name: "test",
+ usage: "test:\n This is a test command\n",
+ }
+ r.Register(cmd, "")
+
+ fs := flag.NewFlagSet("help", flag.ExitOnError)
+ fs.Parse([]string{"test"})
+
+ status := help.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitSuccess)
+ check.Contains(t, buf.String(), "This is a test command")
+}
+
+func TestHelpCommand_UnknownCommand(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ help := &helpCommand{registry: r}
+
+ fs := flag.NewFlagSet("help", flag.ExitOnError)
+ fs.Parse([]string{"unknown"})
+
+ status := help.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitFailure)
+ check.Contains(t, buf.String(), "Unknown command: unknown")
+}
+
+func TestCommandsCommand(t *testing.T) {
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ cmds := &commandsCommand{registry: r}
+ r.Register(cmds, "")
+
+ r.Register(&mockCommand{name: "zzz"}, "")
+ r.Register(&mockCommand{name: "aaa"}, "")
+ r.Register(&mockCommand{name: "mmm"}, "")
+
+ fs := flag.NewFlagSet("commands", flag.ExitOnError)
+ status := cmds.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitSuccess)
+
+ output := buf.String()
+ lines := strings.Split(strings.TrimSpace(output), "\n")
+
+ // Should be sorted alphabetically
+ check.Equal(t, len(lines), 4)
+
+ expected := []string{"aaa", "commands", "mmm", "zzz"}
+ for i, line := range lines {
+ check.Equal(t, line, expected[i])
+ }
+}
+
+func TestFlagsCommand_NoFlags(t *testing.T) {
+ // Create a clean flag set
+ oldCommandLine := flag.CommandLine
+ flag.CommandLine = flag.NewFlagSet("test", flag.ExitOnError)
+ defer func() { flag.CommandLine = oldCommandLine }()
+
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ flags := &flagsCommand{registry: r}
+
+ fs := flag.NewFlagSet("flags", flag.ExitOnError)
+ status := flags.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitSuccess)
+ check.Contains(t, buf.String(), "No top-level flags defined")
+}
+
+func TestFlagsCommand_WithFlags(t *testing.T) {
+ // Create a clean flag set with a test flag
+ oldCommandLine := flag.CommandLine
+ flag.CommandLine = flag.NewFlagSet("test", flag.ExitOnError)
+ flag.String("testflag", "default", "test flag description")
+ defer func() { flag.CommandLine = oldCommandLine }()
+
+ r := NewRegistry()
+ var buf bytes.Buffer
+ r.SetOutput(&buf)
+
+ flags := &flagsCommand{registry: r}
+
+ fs := flag.NewFlagSet("flags", flag.ExitOnError)
+ status := flags.Execute(context.Background(), fs)
+
+ check.Equal(t, status, ExitSuccess)
+
+ output := buf.String()
+ check.Contains(t, output, "testflag")
+ check.Contains(t, output, "test flag description")
+}
+
+func TestPackageLevelFunctions(t *testing.T) {
+ // Save and restore default registry
+ oldRegistry := defaultRegistry
+ defer func() { defaultRegistry = oldRegistry }()
+
+ defaultRegistry = NewRegistry()
+ var buf bytes.Buffer
+ SetOutput(&buf)
+
+ cmd := &mockCommand{
+ name: "test",
+ synopsis: "Test command",
+ exitStatus: ExitSuccess,
+ }
+
+ Register(cmd, "group1")
+
+ // Test Commands()
+ commands := Commands()
+ check.Equal(t, len(commands["group1"]), 1)
+
+ // Verify command is registered (use registry method)
+ check.Equal(t, defaultRegistry.Command("test"), cmd)
+
+ // Test Execute()
+ oldArgs := os.Args
+ os.Args = []string{"prog", "test"}
+ flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
+ flag.Parse()
+ defer func() { os.Args = oldArgs }()
+
+ status := Execute(context.Background())
+ check.Equal(t, status, ExitSuccess)
+}
+
+func TestExitStatus(t *testing.T) {
+ check.Equal(t, int(ExitSuccess), 0)
+ check.Equal(t, int(ExitFailure), 1)
+}
+
+func TestBuiltInCommands(t *testing.T) {
+ help := HelpCommand()
+ check.Equal(t, help.Name(), "help")
+
+ commands := CommandsCommand()
+ check.Equal(t, commands.Name(), "commands")
+
+ flags := FlagsCommand()
+ check.Equal(t, flags.Name(), "flags")
+}