github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/required_test.go (about) 1 package cli 2 3 import ( 4 "errors" 5 "io" 6 "testing" 7 8 "github.com/spf13/cobra" 9 "gotest.tools/v3/assert" 10 ) 11 12 func TestRequiresNoArgs(t *testing.T) { 13 testCases := []testCase{ 14 { 15 validateFunc: NoArgs, 16 expectedError: "no error", 17 }, 18 { 19 args: []string{"foo"}, 20 validateFunc: NoArgs, 21 expectedError: "accepts no arguments.", 22 }, 23 } 24 for _, tc := range testCases { 25 cmd := newDummyCommand(tc.validateFunc) 26 cmd.SetArgs(tc.args) 27 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 28 } 29 } 30 31 func TestRequiresMinArgs(t *testing.T) { 32 testCases := []testCase{ 33 { 34 validateFunc: RequiresMinArgs(0), 35 expectedError: "no error", 36 }, 37 { 38 validateFunc: RequiresMinArgs(1), 39 expectedError: "at least 1 argument.", 40 }, 41 { 42 args: []string{"foo"}, 43 validateFunc: RequiresMinArgs(2), 44 expectedError: "at least 2 arguments.", 45 }, 46 } 47 for _, tc := range testCases { 48 cmd := newDummyCommand(tc.validateFunc) 49 cmd.SetArgs(tc.args) 50 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 51 } 52 } 53 54 func TestRequiresMaxArgs(t *testing.T) { 55 testCases := []testCase{ 56 { 57 validateFunc: RequiresMaxArgs(0), 58 expectedError: "no error", 59 }, 60 { 61 args: []string{"foo", "bar"}, 62 validateFunc: RequiresMaxArgs(1), 63 expectedError: "at most 1 argument.", 64 }, 65 { 66 args: []string{"foo", "bar", "baz"}, 67 validateFunc: RequiresMaxArgs(2), 68 expectedError: "at most 2 arguments.", 69 }, 70 } 71 for _, tc := range testCases { 72 cmd := newDummyCommand(tc.validateFunc) 73 cmd.SetArgs(tc.args) 74 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 75 } 76 } 77 78 func TestRequiresRangeArgs(t *testing.T) { 79 testCases := []testCase{ 80 { 81 validateFunc: RequiresRangeArgs(0, 0), 82 expectedError: "no error", 83 }, 84 { 85 validateFunc: RequiresRangeArgs(0, 1), 86 expectedError: "no error", 87 }, 88 { 89 args: []string{"foo", "bar"}, 90 validateFunc: RequiresRangeArgs(0, 1), 91 expectedError: "at most 1 argument.", 92 }, 93 { 94 args: []string{"foo", "bar", "baz"}, 95 validateFunc: RequiresRangeArgs(0, 2), 96 expectedError: "at most 2 arguments.", 97 }, 98 { 99 validateFunc: RequiresRangeArgs(1, 2), 100 expectedError: "at least 1 ", 101 }, 102 } 103 for _, tc := range testCases { 104 cmd := newDummyCommand(tc.validateFunc) 105 cmd.SetArgs(tc.args) 106 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 107 } 108 } 109 110 func TestExactArgs(t *testing.T) { 111 testCases := []testCase{ 112 { 113 validateFunc: ExactArgs(0), 114 expectedError: "no error", 115 }, 116 { 117 validateFunc: ExactArgs(1), 118 expectedError: "exactly 1 argument.", 119 }, 120 { 121 validateFunc: ExactArgs(2), 122 expectedError: "exactly 2 arguments.", 123 }, 124 } 125 for _, tc := range testCases { 126 cmd := newDummyCommand(tc.validateFunc) 127 cmd.SetArgs(tc.args) 128 assert.ErrorContains(t, cmd.Execute(), tc.expectedError) 129 } 130 } 131 132 type testCase struct { 133 args []string 134 validateFunc cobra.PositionalArgs 135 expectedError string 136 } 137 138 func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command { 139 cmd := &cobra.Command{ 140 Use: "dummy", 141 Args: validationFunc, 142 RunE: func(cmd *cobra.Command, args []string) error { 143 return errors.New("no error") 144 }, 145 } 146 cmd.SetOut(io.Discard) 147 return cmd 148 }