github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/command/telemetry_utils_test.go (about) 1 package command 2 3 import ( 4 "testing" 5 6 "github.com/spf13/cobra" 7 "gotest.tools/v3/assert" 8 ) 9 10 func setupCobraCommands() (*cobra.Command, *cobra.Command, *cobra.Command) { 11 rootCmd := &cobra.Command{ 12 Use: "root [OPTIONS] COMMAND [ARG...]", 13 } 14 childCmd := &cobra.Command{ 15 Use: "child [OPTIONS] COMMAND [ARG...]", 16 } 17 grandchildCmd := &cobra.Command{ 18 Use: "grandchild [OPTIONS] COMMAND [ARG...]", 19 } 20 childCmd.AddCommand(grandchildCmd) 21 rootCmd.AddCommand(childCmd) 22 23 return rootCmd, childCmd, grandchildCmd 24 } 25 26 func TestGetFullCommandName(t *testing.T) { 27 rootCmd, childCmd, grandchildCmd := setupCobraCommands() 28 29 t.Parallel() 30 31 for _, tc := range []struct { 32 testName string 33 cmd *cobra.Command 34 expected string 35 }{ 36 { 37 testName: "rootCmd", 38 cmd: rootCmd, 39 expected: "root", 40 }, 41 { 42 testName: "childCmd", 43 cmd: childCmd, 44 expected: "root child", 45 }, 46 { 47 testName: "grandChild", 48 cmd: grandchildCmd, 49 expected: "root child grandchild", 50 }, 51 } { 52 tc := tc 53 t.Run(tc.testName, func(t *testing.T) { 54 t.Parallel() 55 actual := getFullCommandName(tc.cmd) 56 assert.Equal(t, actual, tc.expected) 57 }) 58 } 59 } 60 61 func TestGetCommandName(t *testing.T) { 62 rootCmd, childCmd, grandchildCmd := setupCobraCommands() 63 64 t.Parallel() 65 66 for _, tc := range []struct { 67 testName string 68 cmd *cobra.Command 69 expected string 70 }{ 71 { 72 testName: "rootCmd", 73 cmd: rootCmd, 74 expected: "", 75 }, 76 { 77 testName: "childCmd", 78 cmd: childCmd, 79 expected: "child", 80 }, 81 { 82 testName: "grandchildCmd", 83 cmd: grandchildCmd, 84 expected: "child grandchild", 85 }, 86 } { 87 tc := tc 88 t.Run(tc.testName, func(t *testing.T) { 89 t.Parallel() 90 actual := getCommandName(tc.cmd) 91 assert.Equal(t, actual, tc.expected) 92 }) 93 } 94 }