github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/required_test.go (about) 1 package cli 2 3 import ( 4 "errors" 5 "io/ioutil" 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 runTestCases(t, testCases) 25 } 26 27 func TestRequiresMinArgs(t *testing.T) { 28 testCases := []testCase{ 29 { 30 validateFunc: RequiresMinArgs(0), 31 expectedError: "no error", 32 }, 33 { 34 validateFunc: RequiresMinArgs(1), 35 expectedError: "at least 1 argument.", 36 }, 37 { 38 args: []string{"foo"}, 39 validateFunc: RequiresMinArgs(2), 40 expectedError: "at least 2 arguments.", 41 }, 42 } 43 runTestCases(t, testCases) 44 } 45 46 func TestRequiresMaxArgs(t *testing.T) { 47 testCases := []testCase{ 48 { 49 validateFunc: RequiresMaxArgs(0), 50 expectedError: "no error", 51 }, 52 { 53 args: []string{"foo", "bar"}, 54 validateFunc: RequiresMaxArgs(1), 55 expectedError: "at most 1 argument.", 56 }, 57 { 58 args: []string{"foo", "bar", "baz"}, 59 validateFunc: RequiresMaxArgs(2), 60 expectedError: "at most 2 arguments.", 61 }, 62 } 63 runTestCases(t, testCases) 64 } 65 66 func TestRequiresRangeArgs(t *testing.T) { 67 testCases := []testCase{ 68 { 69 validateFunc: RequiresRangeArgs(0, 0), 70 expectedError: "no error", 71 }, 72 { 73 validateFunc: RequiresRangeArgs(0, 1), 74 expectedError: "no error", 75 }, 76 { 77 args: []string{"foo", "bar"}, 78 validateFunc: RequiresRangeArgs(0, 1), 79 expectedError: "at most 1 argument.", 80 }, 81 { 82 args: []string{"foo", "bar", "baz"}, 83 validateFunc: RequiresRangeArgs(0, 2), 84 expectedError: "at most 2 arguments.", 85 }, 86 { 87 validateFunc: RequiresRangeArgs(1, 2), 88 expectedError: "at least 1 ", 89 }, 90 } 91 runTestCases(t, testCases) 92 } 93 94 func TestExactArgs(t *testing.T) { 95 testCases := []testCase{ 96 { 97 validateFunc: ExactArgs(0), 98 expectedError: "no error", 99 }, 100 { 101 validateFunc: ExactArgs(1), 102 expectedError: "exactly 1 argument.", 103 }, 104 { 105 validateFunc: ExactArgs(2), 106 expectedError: "exactly 2 arguments.", 107 }, 108 } 109 runTestCases(t, testCases) 110 } 111 112 type testCase struct { 113 args []string 114 validateFunc cobra.PositionalArgs 115 expectedError string 116 } 117 118 func runTestCases(t *testing.T, testCases []testCase) { 119 for _, tc := range testCases { 120 cmd := newDummyCommand(tc.validateFunc) 121 cmd.SetArgs(tc.args) 122 cmd.SetOut(ioutil.Discard) 123 124 err := cmd.Execute() 125 assert.ErrorContains(t, err, tc.expectedError) 126 } 127 } 128 129 func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command { 130 cmd := &cobra.Command{ 131 Use: "dummy", 132 Args: validationFunc, 133 RunE: func(cmd *cobra.Command, args []string) error { 134 return errors.New("no error") 135 }, 136 } 137 return cmd 138 }