forked from docker/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequired_test.go
More file actions
141 lines (129 loc) · 2.87 KB
/
Copy pathrequired_test.go
File metadata and controls
141 lines (129 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package cli
import (
"errors"
"io/ioutil"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRequiresNoArgs(t *testing.T) {
testCases := []testCase{
{
validateFunc: NoArgs,
expectedError: "no error",
},
{
args: []string{"foo"},
validateFunc: NoArgs,
expectedError: "accepts no arguments.",
},
}
runTestCases(t, testCases)
}
func TestRequiresMinArgs(t *testing.T) {
testCases := []testCase{
{
validateFunc: RequiresMinArgs(0),
expectedError: "no error",
},
{
validateFunc: RequiresMinArgs(1),
expectedError: "at least 1 argument.",
},
{
args: []string{"foo"},
validateFunc: RequiresMinArgs(2),
expectedError: "at least 2 arguments.",
},
}
runTestCases(t, testCases)
}
func TestRequiresMaxArgs(t *testing.T) {
testCases := []testCase{
{
validateFunc: RequiresMaxArgs(0),
expectedError: "no error",
},
{
args: []string{"foo", "bar"},
validateFunc: RequiresMaxArgs(1),
expectedError: "at most 1 argument.",
},
{
args: []string{"foo", "bar", "baz"},
validateFunc: RequiresMaxArgs(2),
expectedError: "at most 2 arguments.",
},
}
runTestCases(t, testCases)
}
func TestRequiresRangeArgs(t *testing.T) {
testCases := []testCase{
{
validateFunc: RequiresRangeArgs(0, 0),
expectedError: "no error",
},
{
validateFunc: RequiresRangeArgs(0, 1),
expectedError: "no error",
},
{
args: []string{"foo", "bar"},
validateFunc: RequiresRangeArgs(0, 1),
expectedError: "at most 1 argument.",
},
{
args: []string{"foo", "bar", "baz"},
validateFunc: RequiresRangeArgs(0, 2),
expectedError: "at most 2 arguments.",
},
{
validateFunc: RequiresRangeArgs(1, 2),
expectedError: "at least 1 ",
},
}
runTestCases(t, testCases)
}
func TestExactArgs(t *testing.T) {
testCases := []testCase{
{
validateFunc: ExactArgs(0),
expectedError: "no error",
},
{
validateFunc: ExactArgs(1),
expectedError: "exactly 1 argument.",
},
{
validateFunc: ExactArgs(2),
expectedError: "exactly 2 arguments.",
},
}
runTestCases(t, testCases)
}
type testCase struct {
args []string
validateFunc cobra.PositionalArgs
expectedError string
}
func runTestCases(t *testing.T, testCases []testCase) {
for _, tc := range testCases {
cmd := newDummyCommand(tc.validateFunc)
cmd.SetArgs(tc.args)
cmd.SetOutput(ioutil.Discard)
err := cmd.Execute()
require.Error(t, err, "Expected an error: %s", tc.expectedError)
assert.Contains(t, err.Error(), tc.expectedError)
}
}
func newDummyCommand(validationFunc cobra.PositionalArgs) *cobra.Command {
cmd := &cobra.Command{
Use: "dummy",
Args: validationFunc,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("no error")
},
}
return cmd
}