diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 05:38:06 -0500 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-12-27 05:38:06 -0500 |
| commit | 6b5ccdf15ac799bc190084d69ded8e38f1e79cb9 (patch) | |
| tree | bfc05e627950ee3d7dbfd847d6c4349a78535bcf /subcmd_test.go | |
| parent | Refactor tests to reduce duplication (diff) | |
| download | subcmd-6b5ccdf15ac799bc190084d69ded8e38f1e79cb9.zip | |
Add ManHelpCommand to delegate 'help' to opening man pages
Diffstat (limited to 'subcmd_test.go')
| -rw-r--r-- | subcmd_test.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/subcmd_test.go b/subcmd_test.go index 6841f9e..57b86c2 100644 --- a/subcmd_test.go +++ b/subcmd_test.go @@ -5,6 +5,7 @@ import ( "context" "flag" "os" + "os/exec" "strings" "testing" @@ -250,6 +251,46 @@ func TestHelpCommand_UnknownCommand(t *testing.T) { check.Contains(t, buf.String(), "Unknown command: unknown") } +func TestManHelpCommand_NoArgs(t *testing.T) { + var calledName string + var calledArgs []string + oldExecCommand := execCommand + execCommand = func(name string, args ...string) *exec.Cmd { + calledName = name + calledArgs = args + return exec.Command("true") + } + defer func() { execCommand = oldExecCommand }() + help := ManHelpCommand("myapp") + fs := flag.NewFlagSet("help", flag.ExitOnError) + fs.Parse([]string{}) + + status := help.Execute(context.Background(), fs) + + check.Equal(t, status, ExitSuccess) + check.Equal(t, calledName, "man") + check.Equal(t, len(calledArgs), 1) + check.Equal(t, calledArgs[0], "myapp") +} + +func TestManHelpCommand_WithSubcommand(t *testing.T) { + var calledArgs []string + oldExecCommand := execCommand + execCommand = func(name string, args ...string) *exec.Cmd { + calledArgs = args + return exec.Command("true") + } + defer func() { execCommand = oldExecCommand }() + help := ManHelpCommand("myapp") + fs := flag.NewFlagSet("help", flag.ExitOnError) + fs.Parse([]string{"build"}) + + status := help.Execute(context.Background(), fs) + + check.Equal(t, status, ExitSuccess) + check.Equal(t, calledArgs[0], "myapp-build") +} + func TestCommandsCommand(t *testing.T) { var buf bytes.Buffer r := newTestRegistry( |