@@ -3,10 +3,67 @@ package cli
3
3
import (
4
4
"strings"
5
5
"testing"
6
+ "io"
7
+ "io/ioutil"
8
+ "os"
6
9
7
10
"github.com/ipfs/go-ipfs/commands"
8
11
)
9
12
13
+ type kvs map [string ]interface {}
14
+ type words []string
15
+
16
+ func sameWords (a words , b words ) bool {
17
+ if len (a ) != len (b ) {
18
+ return false
19
+ }
20
+ for i , w := range a {
21
+ if w != b [i ] {
22
+ return false
23
+ }
24
+ }
25
+ return true
26
+ }
27
+
28
+ func sameKVs (a kvs , b kvs ) bool {
29
+ if len (a ) != len (b ) {
30
+ return false
31
+ }
32
+ for k , v := range a {
33
+ if v != b [k ] {
34
+ return false
35
+ }
36
+ }
37
+ return true
38
+ }
39
+
40
+ func TestSameWords (t * testing.T ) {
41
+ a := []string {"v1" , "v2" }
42
+ b := []string {"v1" , "v2" , "v3" }
43
+ c := []string {"v2" , "v3" }
44
+ d := []string {"v2" }
45
+ e := []string {"v2" , "v3" }
46
+ f := []string {"v2" , "v1" }
47
+
48
+ test := func (a words , b words , v bool ) {
49
+ if sameWords (a , b ) != v {
50
+ t .Errorf ("sameWords('%v', '%v') != %v" , a , b , v )
51
+ }
52
+ }
53
+
54
+ test (a , b , false )
55
+ test (a , a , true )
56
+ test (a , c , false )
57
+ test (b , c , false )
58
+ test (c , d , false )
59
+ test (c , e , true )
60
+ test (b , e , false )
61
+ test (a , b , false )
62
+ test (a , f , false )
63
+ test (e , f , false )
64
+ test (f , f , true )
65
+ }
66
+
10
67
func TestOptionParsing (t * testing.T ) {
11
68
subCmd := & commands.Command {}
12
69
cmd := & commands.Command {
@@ -19,30 +76,6 @@ func TestOptionParsing(t *testing.T) {
19
76
},
20
77
}
21
78
22
- type kvs map [string ]interface {}
23
- type words []string
24
-
25
- sameWords := func (a words , b words ) bool {
26
- for i , w := range a {
27
- if w != b [i ] {
28
- return false
29
- }
30
- }
31
- return true
32
- }
33
-
34
- sameKVs := func (a kvs , b kvs ) bool {
35
- if len (a ) != len (b ) {
36
- return false
37
- }
38
- for k , v := range a {
39
- if v != b [k ] {
40
- return false
41
- }
42
- }
43
- return true
44
- }
45
-
46
79
testHelper := func (args string , expectedOpts kvs , expectedWords words , expectErr bool ) {
47
80
_ , opts , input , _ , err := parseOpts (strings .Split (args , " " ), cmd )
48
81
if expectErr {
@@ -117,76 +150,78 @@ func TestArgumentParsing(t *testing.T) {
117
150
commands .StringArg ("b" , true , false , "another arg" ),
118
151
},
119
152
},
153
+ "stdinenabled" : & commands.Command {
154
+ Arguments : []commands.Argument {
155
+ commands .StringArg ("a" , true , true , "some arg" ).EnableStdin (),
156
+ },
157
+ },
120
158
},
121
159
}
122
160
123
- _ , _ , _ , err := Parse ([]string {"noarg" }, nil , rootCmd )
124
- if err != nil {
125
- t .Error ("Should have passed" )
126
- }
127
- _ , _ , _ , err = Parse ([]string {"noarg" , "value!" }, nil , rootCmd )
128
- if err == nil {
129
- t .Error ("Should have failed (provided an arg, but command didn't define any)" )
161
+ test := func (cmd words , f * os.File , res words ) {
162
+ if f != nil {
163
+ if _ , err := f .Seek (0 , os .SEEK_SET ); err != nil {
164
+ t .Fatal (err )
165
+ }
166
+ }
167
+ req , _ , _ , err := Parse (cmd , f , rootCmd )
168
+ if err != nil {
169
+ t .Errorf ("Command '%v' should have passed parsing" , cmd )
170
+ }
171
+ if ! sameWords (req .Arguments (), res ) {
172
+ t .Errorf ("Arguments parsed from '%v' are not '%v'" , cmd , res )
173
+ }
130
174
}
131
175
132
- _ , _ , _ , err = Parse ([]string {"onearg" , "value!" }, nil , rootCmd )
133
- if err != nil {
134
- t .Error ("Should have passed" )
135
- }
136
- _ , _ , _ , err = Parse ([]string {"onearg" }, nil , rootCmd )
137
- if err == nil {
138
- t .Error ("Should have failed (didn't provide any args, arg is required)" )
176
+ testFail := func (cmd words , msg string ) {
177
+ _ , _ , _ , err := Parse (cmd , nil , rootCmd )
178
+ if err == nil {
179
+ t .Errorf ("Should have failed: %v" , msg )
180
+ }
139
181
}
140
182
141
- _ , _ , _ , err = Parse ([]string {"twoargs" , "value1" , "value2" }, nil , rootCmd )
142
- if err != nil {
143
- t .Error ("Should have passed" )
144
- }
145
- _ , _ , _ , err = Parse ([]string {"twoargs" , "value!" }, nil , rootCmd )
146
- if err == nil {
147
- t .Error ("Should have failed (only provided 1 arg, needs 2)" )
148
- }
149
- _ , _ , _ , err = Parse ([]string {"twoargs" }, nil , rootCmd )
150
- if err == nil {
151
- t .Error ("Should have failed (didn't provide any args, 2 required)" )
152
- }
183
+ test ([]string {"noarg" }, nil , []string {})
184
+ testFail ([]string {"noarg" , "value!" }, "provided an arg, but command didn't define any" )
153
185
154
- _ , _ , _ , err = Parse ([]string {"variadic" , "value!" }, nil , rootCmd )
155
- if err != nil {
156
- t .Error ("Should have passed" )
157
- }
158
- _ , _ , _ , err = Parse ([]string {"variadic" , "value1" , "value2" , "value3" }, nil , rootCmd )
159
- if err != nil {
160
- t .Error ("Should have passed" )
161
- }
162
- _ , _ , _ , err = Parse ([]string {"variadic" }, nil , rootCmd )
163
- if err == nil {
164
- t .Error ("Should have failed (didn't provide any args, 1 required)" )
165
- }
186
+ test ([]string {"onearg" , "value!" }, nil , []string {"value!" })
187
+ testFail ([]string {"onearg" }, "didn't provide any args, arg is required" )
166
188
167
- _ , _ , _ , err = Parse ([]string {"optional" , "value!" }, nil , rootCmd )
168
- if err != nil {
169
- t .Error ("Should have passed" )
170
- }
171
- _ , _ , _ , err = Parse ([]string {"optional" }, nil , rootCmd )
172
- if err != nil {
173
- t .Error ("Should have passed" )
174
- }
189
+ test ([]string {"twoargs" , "value1" , "value2" }, nil , []string {"value1" , "value2" })
190
+ testFail ([]string {"twoargs" , "value!" }, "only provided 1 arg, needs 2" )
191
+ testFail ([]string {"twoargs" }, "didn't provide any args, 2 required" )
175
192
176
- _ , _ , _ , err = Parse ([]string {"reversedoptional" , "value1" , "value2" }, nil , rootCmd )
177
- if err != nil {
178
- t .Error ("Should have passed" )
179
- }
180
- _ , _ , _ , err = Parse ([]string {"reversedoptional" , "value!" }, nil , rootCmd )
181
- if err != nil {
182
- t .Error ("Should have passed" )
183
- }
184
- _ , _ , _ , err = Parse ([]string {"reversedoptional" }, nil , rootCmd )
185
- if err == nil {
186
- t .Error ("Should have failed (didn't provide any args, 1 required)" )
187
- }
188
- _ , _ , _ , err = Parse ([]string {"reversedoptional" , "value1" , "value2" , "value3" }, nil , rootCmd )
189
- if err == nil {
190
- t .Error ("Should have failed (provided too many args, only takes 1)" )
193
+ test ([]string {"variadic" , "value!" }, nil , []string {"value!" })
194
+ test ([]string {"variadic" , "value1" , "value2" , "value3" }, nil , []string {"value1" , "value2" , "value3" })
195
+ testFail ([]string {"variadic" }, "didn't provide any args, 1 required" )
196
+
197
+ test ([]string {"optional" , "value!" }, nil , []string {"value!" })
198
+ test ([]string {"optional" }, nil , []string {})
199
+
200
+ test ([]string {"reversedoptional" , "value1" , "value2" }, nil , []string {"value1" , "value2" })
201
+ test ([]string {"reversedoptional" , "value!" }, nil , []string {"value!" })
202
+
203
+ testFail ([]string {"reversedoptional" }, "didn't provide any args, 1 required" )
204
+ testFail ([]string {"reversedoptional" , "value1" , "value2" , "value3" }, "provided too many args, only takes 1" )
205
+
206
+ // Use a temp file to simulate stdin
207
+ fileToSimulateStdin := func (t * testing.T , content string ) (* os.File ) {
208
+ fstdin , err := ioutil .TempFile ("" , "" )
209
+ if err != nil {
210
+ t .Fatal (err )
211
+ }
212
+ defer os .Remove (fstdin .Name ())
213
+
214
+ if _ , err := io .WriteString (fstdin , content ); err != nil {
215
+ t .Fatal (err )
216
+ }
217
+ return fstdin
191
218
}
219
+
220
+ test ([]string {"stdinenabled" , "value1" , "value2" }, nil , []string {"value1" , "value2" })
221
+
222
+ fstdin := fileToSimulateStdin (t , "stdin1" )
223
+
224
+ test ([]string {"stdinenabled" }, fstdin , []string {"stdin1" })
225
+ test ([]string {"stdinenabled" , "value1" }, fstdin , []string {"stdin1" , "value1" })
226
+ test ([]string {"stdinenabled" , "value1" , "value2" }, fstdin , []string {"stdin1" , "value1" , "value2" })
192
227
}
0 commit comments