From ba4e21df7a7ad8784ccda320fe015618636139a8 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Fri, 14 Jul 2023 00:39:10 +0900 Subject: [PATCH] add TestPersistentPreRunHooksForHelpCommand --- command_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/command_test.go b/command_test.go index ac435e09..d333f071 100644 --- a/command_test.go +++ b/command_test.go @@ -1611,6 +1611,25 @@ func TestPersistentHooks(t *testing.T) { } } +func TestPersistentPreRunHooksForHelpCommand(t *testing.T) { + executed := false + + rootCmd := &Command{ + Use: "root", + PersistentPreRun: func(*Command, []string) { executed = true }, + Run: emptyRun, + } + childCmd := &Command{Use: "child", Run: emptyRun} + rootCmd.AddCommand(childCmd) + + if _, err := executeCommand(rootCmd, "help"); err != nil { + t.Errorf("Unexpected error: %v", err) + } + if !executed { + t.Error("Root PersistentPreRun should have been executed") + } +} + // Related to https://github.com/spf13/cobra/issues/521. func TestGlobalNormFuncPropagation(t *testing.T) { normFunc := func(f *pflag.FlagSet, name string) pflag.NormalizedName {