From 082a3658e72aa1cfd97e0e9ff41b3884b37c27aa Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Sat, 25 Jan 2025 16:06:01 -0500 Subject: [PATCH] Add test Signed-off-by: Marc Khouzam --- command_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/command_test.go b/command_test.go index 837b6b3..0b0d6c6 100644 --- a/command_test.go +++ b/command_test.go @@ -1018,6 +1018,37 @@ func TestSetHelpCommand(t *testing.T) { } } +func TestSetHelpTemplate(t *testing.T) { + rootCmd := &Command{Use: "root", Run: emptyRun} + childCmd := &Command{Use: "child", Run: emptyRun} + rootCmd.AddCommand(childCmd) + + rootCmd.SetHelpTemplate("WORKS {{.UseLine}}") + + // Call the help on the root command and check the new template is used + got, err := executeCommand(rootCmd, "--help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected := "WORKS " + rootCmd.UseLine() + if got != expected { + t.Errorf("Expected %q, got %q", expected, got) + } + + // Call the help on the child command and check + // the new template is inherited from the parent + got, err = executeCommand(rootCmd, "child", "--help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + expected = "WORKS " + childCmd.UseLine() + if got != expected { + t.Errorf("Expected %q, got %q", expected, got) + } +} + func TestHelpFlagExecuted(t *testing.T) { rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun} @@ -1083,6 +1114,33 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) { checkStringContains(t, output, childCmd.Long) } +func TestSetUsageTemplate(t *testing.T) { + rootCmd := &Command{Use: "root", Run: emptyRun} + childCmd := &Command{Use: "child", Run: emptyRun} + rootCmd.AddCommand(childCmd) + + rootCmd.SetUsageTemplate("WORKS {{.UseLine}}") + + // Trigger the usage on the root command and check the new template is used + got, err := executeCommand(rootCmd, "--invalid") + if err == nil { + t.Errorf("Expected error but did not get one") + } + + expected := "WORKS " + rootCmd.UseLine() + checkStringContains(t, got, expected) + + // Trigger the usage on the child command and check + // the new template is inherited from the parent + got, err = executeCommand(rootCmd, "child", "--invalid") + if err == nil { + t.Errorf("Expected error but did not get one") + } + + expected = "WORKS " + childCmd.UseLine() + checkStringContains(t, got, expected) +} + func TestVersionFlagExecuted(t *testing.T) { rootCmd := &Command{Use: "root", Version: "1.0.0", Run: emptyRun}