commands: improve test coverage for FileArg parsing
License: MIT Signed-off-by: Thomas Gardner <tmg@fastmail.com>
Thomas Gardner committed
Jun 25, 2016 at 22:37 UTC
2ad63fd0dfcf44a7c0c6fe02f451ef824117102d
2 files changed
+81
-20
commands/cli/parse.go
+5
-3
@@ -11,8 +11,8 @@ import (
11
12
cmds "github.com/ipfs/go-ipfs/commands"
13
files "github.com/ipfs/go-ipfs/commands/files"
14
- u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
14
logging "gx/ipfs/QmYtB7Qge8cJpXc4irsEp8zRqfnZMBeB7aTrMEkPk67DRv/go-log"
15
+ u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
16
)
17
18
var log = logging.Logger("commands/cli")
@@ -305,7 +305,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
305
var err error
306
if fpath == "-" {
307
if err = printReadInfo(stdin, msgStdinInfo); err == nil {
308
- file = files.NewReaderFile("", "", stdin, nil)
308
+ fpath = stdin.Name()
309
+ file = files.NewReaderFile("", fpath, stdin, nil)
310
}
311
} else {
312
file, err = appendFile(fpath, argDef, recursive, hidden)
@@ -321,7 +322,8 @@ func parseArgs(inputs []string, stdin *os.File, argDefs []cmds.Argument, recursi
322
if err := printReadInfo(stdin, msgStdinInfo); err != nil {
323
return nil, nil, err
324
}
324
- fileArgs[""] = files.NewReaderFile("", "", stdin, nil)
325
+ fpath := stdin.Name()
326
+ fileArgs[fpath] = files.NewReaderFile("", fpath, stdin, nil)
327
} else {
328
break
329
}
commands/cli/parse_test.go
+76
-17
@@ -179,7 +179,12 @@ func TestArgumentParsing(t *testing.T) {
179
},
180
"FileArg": {
181
Arguments: []commands.Argument{
182
- commands.FileArg("a", false, false, "some arg"),
182
+ commands.FileArg("a", true, false, "some arg"),
183
+ },
184
+ },
185
+ "FileArg+Variadic": {
186
+ Arguments: []commands.Argument{
187
+ commands.FileArg("a", true, true, "some arg"),
188
},
189
},
190
"FileArg+Stdin": {
@@ -187,10 +192,34 @@ func TestArgumentParsing(t *testing.T) {
192
commands.FileArg("a", true, true, "some arg").EnableStdin(),
193
},
194
},
195
+ "StringArg+FileArg": {
196
+ Arguments: []commands.Argument{
197
+ commands.StringArg("a", true, false, "some arg"),
198
+ commands.FileArg("a", true, false, "some arg"),
199
+ },
200
+ },
201
+ "StringArg+FileArg+Stdin": {
202
+ Arguments: []commands.Argument{
203
+ commands.StringArg("a", true, false, "some arg"),
204
+ commands.FileArg("a", true, true, "some arg").EnableStdin(),
205
+ },
206
+ },
207
+ "StringArg+FileArg+Variadic": {
208
+ Arguments: []commands.Argument{
209
+ commands.StringArg("a", true, false, "some arg"),
210
+ commands.FileArg("a", true, true, "some arg"),
211
+ },
212
+ },
213
+ "StringArg+FileArg+Variadic+Stdin": {
214
+ Arguments: []commands.Argument{
215
+ commands.StringArg("a", true, false, "some arg"),
216
+ commands.FileArg("a", true, true, "some arg"),
217
+ },
218
+ },
219
},
220
}
221
193
- test := func(cmd words, f *os.File, res words) {
222
+ test := func(cmd words, f *os.File, exp words) {
223
if f != nil {
224
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
225
t.Fatal(err)
@@ -200,8 +229,18 @@ func TestArgumentParsing(t *testing.T) {
229
if err != nil {
230
t.Errorf("Command '%v' should have passed parsing: %v", cmd, err)
231
}
203
- if !sameWords(req.Arguments(), res) {
204
- t.Errorf("Arguments parsed from '%v' are '%v' instead of '%v'", cmd, req.Arguments(), res)
232
+
233
+ parsedWords := make([]string, len(req.Arguments()))
234
+ copy(parsedWords, req.Arguments())
235
+
236
+ if files := req.Files(); files != nil {
237
+ for file, err := files.NextFile(); err != io.EOF; file, err = files.NextFile() {
238
+ parsedWords = append(parsedWords, file.FullPath())
239
+ }
240
+ }
241
+
242
+ if !sameWords(parsedWords, exp) {
243
+ t.Errorf("Arguments parsed from '%v' are '%v' instead of '%v'", cmd, parsedWords, exp)
244
}
245
}
246
@@ -241,23 +280,43 @@ func TestArgumentParsing(t *testing.T) {
280
testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required")
281
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1")
282
244
- // Use a temp file to simulate stdin
245
- fileToSimulateStdin := func(t *testing.T, content string) *os.File {
246
- fstdin, err := ioutil.TempFile("", "")
283
+ // Since FileArgs are presently stored ordered by Path, the enum string
284
+ // is used to construct a predictably ordered sequence of filenames.
285
+ tmpFile := func(t *testing.T, enum string) *os.File {
286
+ f, err := ioutil.TempFile("", enum)
287
if err != nil {
288
t.Fatal(err)
289
}
290
251
- if _, err := io.WriteString(fstdin, content); err != nil {
252
- t.Fatal(err)
253
- }
254
- return fstdin
291
+ return f
292
}
256
- fstdin := fileToSimulateStdin(t, "stdin1")
257
- defer os.Remove(fstdin.Name())
293
+ file1 := tmpFile(t, "1")
294
+ file2 := tmpFile(t, "2")
295
+ file3 := tmpFile(t, "3")
296
+ defer os.Remove(file3.Name())
297
+ defer os.Remove(file2.Name())
298
+ defer os.Remove(file1.Name())
299
259
- test([]string{"noarg"}, fstdin, []string{})
260
- test([]string{"FileArg", fstdin.Name()}, nil, []string{})
261
- test([]string{"FileArg+Stdin"}, fstdin, []string{})
262
- test([]string{"FileArg+Stdin", "-"}, fstdin, []string{})
300
+ test([]string{"noarg"}, file1, []string{})
301
+ test([]string{"FileArg", file1.Name()}, nil, []string{file1.Name()})
302
+ test([]string{"FileArg+Variadic", file1.Name(), file2.Name()}, nil,
303
+ []string{file1.Name(), file2.Name()})
304
+ test([]string{"FileArg+Stdin"}, file1, []string{file1.Name()})
305
+ test([]string{"FileArg+Stdin", "-"}, file1, []string{file1.Name()})
306
+ test([]string{"FileArg+Stdin", file1.Name(), "-"}, file2,
307
+ []string{file1.Name(), file2.Name()})
308
+ test([]string{"StringArg+FileArg",
309
+ "foo", file1.Name()}, nil, []string{"foo", file1.Name()})
310
+ test([]string{"StringArg+FileArg+Variadic",
311
+ "foo", file1.Name(), file2.Name()}, nil,
312
+ []string{"foo", file1.Name(), file2.Name()})
313
+ test([]string{"StringArg+FileArg+Stdin",
314
+ "foo", file1.Name(), "-"}, file2,
315
+ []string{"foo", file1.Name(), file2.Name()})
316
+ test([]string{"StringArg+FileArg+Variadic+Stdin",
317
+ "foo", file1.Name(), file2.Name()}, file3,
318
+ []string{"foo", file1.Name(), file2.Name()})
319
+ test([]string{"StringArg+FileArg+Variadic+Stdin",
320
+ "foo", file1.Name(), file2.Name(), "-"}, file3,
321
+ []string{"foo", file1.Name(), file2.Name(), file3.Name()})
322
}