From fd272b71846c7c9ae5673b66e84f82b3e00f4ccf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Erik=20Hedenstr=C3=B6m?= <erik@hedenstroem.com>
Date: Sat, 18 May 2024 22:11:27 +0000
Subject: [PATCH] Checking that all flags get reset

---
 snok.go      |  1 -
 snok_test.go | 15 ++++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/snok.go b/snok.go
index d3c90c5..8d885b7 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 e9f6164..6ce7391 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
 	},
-- 
GitLab