diff --git a/snok.go b/snok.go index d3c90c5643ce1f20415cdd50e72dda082c991378..8d885b72d842edc43c95ecef734f484f415d1a79 100644 --- a/snok.go +++ b/snok.go @@ -73,7 +73,6 @@ func (ct *CommandTest) Run(t *testing.T) { } func (ct *CommandTest) testCmd(t *testing.T, cmd *cobra.Command, args []string) { - ct.RootCmd.ResetFlags() args = append(args, cmd.Name()) containers, exists := cmd.Annotations["containers"] if exists { diff --git a/snok_test.go b/snok_test.go index e9f61643bb0e9c6425d3082b7d47ab188c107af5..6ce73913f7da31c47b5ec75321f6fb727d421227 100644 --- a/snok_test.go +++ b/snok_test.go @@ -24,6 +24,7 @@ func mockUnexpectedError(t require.TestingT, err error, msgAndArgs ...interface{ func TestMain(m *testing.M) { rootCmd.AddCommand(envCmd, echoCmd, errorCmd) + rootCmd.PersistentFlags().BoolP("debug", "d", false, "debug flag") echoCmd.Flags().BoolP("reverse", "r", false, "reverse the output") ct = NewCommandTest(rootCmd) ct.handleUnexpectedError = mockUnexpectedError @@ -59,6 +60,11 @@ var rootCmd = &cobra.Command{ "name": "Version", "args": ["--version"], "output": "/^test version \\d+\\.\\d+\\.\\d+$/" + }, + { + "name": "Help", + "args": ["--help"], + "output": "/^Usage:\n test \\[command\\]/" } ]`, }, @@ -104,9 +110,9 @@ var echoCmd = &cobra.Command{ }, { "name": "Echo Input (data)", - "args": ["-r","The","rain"], + "args": ["-d", "-r","The","rain"], "input": "data:,in Spain falls mainly on the plain", - "output": "nialp eht no ylniam sllaf niapS ni niar ehT" + "output": "DEBUG: nialp eht no ylniam sllaf niapS ni niar ehT" }, { "name": "Echo Input (base64)", @@ -138,13 +144,16 @@ var echoCmd = &cobra.Command{ b, err := io.ReadAll(cmd.InOrStdin()) str := fmt.Sprintf("%s %s", strings.Join(args, " "), b) str = strings.TrimRightFunc(str, unicode.IsSpace) - if cmd.Flags().Changed("reverse") { + if ok, _ := cmd.Flags().GetBool("reverse"); ok { rstr := "" for _, v := range str { rstr = string(v) + rstr } str = rstr } + if ok, _ := cmd.Flags().GetBool("debug"); ok { + fmt.Print("DEBUG: ") + } fmt.Print(str) return },