From 6495e274444691f8848eb499e6bcaa1e6b7601c7 Mon Sep 17 00:00:00 2001 From: Francis Nickels Date: Mon, 23 May 2022 18:53:15 -0700 Subject: [PATCH] add test cases --- FJNnotes.md | 3 -- command_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) delete mode 100644 FJNnotes.md diff --git a/FJNnotes.md b/FJNnotes.md deleted file mode 100644 index 22cccc9c..00000000 --- a/FJNnotes.md +++ /dev/null @@ -1,3 +0,0 @@ -Things to fix: -* default --version flag is not shown when running `tool help` but is shown when running `tool` or `tool --help` by itself -* `tool --help command` shows general help instead of `tool command --help` results diff --git a/command_test.go b/command_test.go index d48fef1a..d8ff19ff 100644 --- a/command_test.go +++ b/command_test.go @@ -2161,3 +2161,90 @@ func TestSetContextPersistentPreRun(t *testing.T) { t.Error(err) } } + +const VERSION_FLAG = "--version" +const HELP_FLAG = "--help" + +func TestNoRootRunCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description"} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestNoRootRunCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description"} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +} + +func TestHelpCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, "help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestHelpCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, "help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +} + +func TestHelpflagCommandExecutedWithVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Version: "1.0.0", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, HELP_FLAG) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringContains(t, output, VERSION_FLAG) +} + +func TestHelpflagCommandExecutedWithoutVersionSet(t *testing.T) { + rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, err := executeCommand(rootCmd, HELP_FLAG) + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, rootCmd.Long) + checkStringContains(t, output, HELP_FLAG) + checkStringOmits(t, output, VERSION_FLAG) +}