mirror of
https://github.com/ipfs/kubo.git
synced 2025-08-06 19:44:01 +08:00
ignore those last bits, this time its for real
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
@ -178,49 +177,32 @@ func TestArgumentParsing(t *testing.T) {
|
||||
commands.StringArg("b", true, false, "another arg"),
|
||||
},
|
||||
},
|
||||
"FileArg": {
|
||||
"stdinenabled": {
|
||||
Arguments: []commands.Argument{
|
||||
commands.FileArg("a", true, false, "some arg"),
|
||||
commands.StringArg("a", true, true, "some arg").EnableStdin(),
|
||||
},
|
||||
},
|
||||
"FileArg+Variadic": {
|
||||
Arguments: []commands.Argument{
|
||||
commands.FileArg("a", true, true, "some arg"),
|
||||
},
|
||||
},
|
||||
"FileArg+Stdin": {
|
||||
Arguments: []commands.Argument{
|
||||
commands.FileArg("a", true, true, "some arg").EnableStdin(),
|
||||
},
|
||||
},
|
||||
"StringArg+FileArg": {
|
||||
"stdinenabled2args": &commands.Command{
|
||||
Arguments: []commands.Argument{
|
||||
commands.StringArg("a", true, false, "some arg"),
|
||||
commands.FileArg("a", true, false, "some arg"),
|
||||
commands.StringArg("b", true, true, "another arg").EnableStdin(),
|
||||
},
|
||||
},
|
||||
"StringArg+FileArg+Stdin": {
|
||||
"stdinenablednotvariadic": &commands.Command{
|
||||
Arguments: []commands.Argument{
|
||||
commands.StringArg("a", true, false, "some arg"),
|
||||
commands.FileArg("a", true, true, "some arg").EnableStdin(),
|
||||
commands.StringArg("a", true, false, "some arg").EnableStdin(),
|
||||
},
|
||||
},
|
||||
"StringArg+FileArg+Variadic": {
|
||||
"stdinenablednotvariadic2args": &commands.Command{
|
||||
Arguments: []commands.Argument{
|
||||
commands.StringArg("a", true, false, "some arg"),
|
||||
commands.FileArg("a", true, true, "some arg"),
|
||||
},
|
||||
},
|
||||
"StringArg+FileArg+Variadic+Stdin": {
|
||||
Arguments: []commands.Argument{
|
||||
commands.StringArg("a", true, false, "some arg"),
|
||||
commands.FileArg("a", true, true, "some arg"),
|
||||
commands.StringArg("b", true, false, "another arg").EnableStdin(),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
test := func(cmd words, f *os.File, exp words) {
|
||||
test := func(cmd words, f *os.File, res words) {
|
||||
if f != nil {
|
||||
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -230,18 +212,8 @@ func TestArgumentParsing(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Errorf("Command '%v' should have passed parsing: %v", cmd, err)
|
||||
}
|
||||
|
||||
parsedWords := make([]string, len(req.Arguments()))
|
||||
copy(parsedWords, req.Arguments())
|
||||
|
||||
if files := req.Files(); files != nil {
|
||||
for file, err := files.NextFile(); err != io.EOF; file, err = files.NextFile() {
|
||||
parsedWords = append(parsedWords, file.FullPath())
|
||||
}
|
||||
}
|
||||
|
||||
if !sameWords(parsedWords, exp) {
|
||||
t.Errorf("Arguments parsed from '%v' are '%v' instead of '%v'", cmd, parsedWords, exp)
|
||||
if !sameWords(req.Arguments(), res) {
|
||||
t.Errorf("Arguments parsed from '%v' are '%v' instead of '%v'", cmd, req.Arguments(), res)
|
||||
}
|
||||
}
|
||||
|
||||
@ -281,52 +253,59 @@ func TestArgumentParsing(t *testing.T) {
|
||||
testFail([]string{"reversedoptional"}, nil, "didn't provide any args, 1 required")
|
||||
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, nil, "provided too many args, only takes 1")
|
||||
|
||||
// Since FileArgs are presently stored ordered by Path, the enum string
|
||||
// is used to construct a predictably ordered sequence of filenames.
|
||||
tmpFile := func(t *testing.T, enum string) *os.File {
|
||||
f, err := ioutil.TempFile("", enum)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fn, err := filepath.EvalSymlinks(f.Name())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
f, err = os.Create(fn)
|
||||
// Use a temp file to simulate stdin
|
||||
fileToSimulateStdin := func(t *testing.T, content string) *os.File {
|
||||
fstdin, err := ioutil.TempFile("", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fstdin.Name())
|
||||
|
||||
return f
|
||||
if _, err := io.WriteString(fstdin, content); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return fstdin
|
||||
}
|
||||
file1 := tmpFile(t, "1")
|
||||
file2 := tmpFile(t, "2")
|
||||
file3 := tmpFile(t, "3")
|
||||
defer os.Remove(file3.Name())
|
||||
defer os.Remove(file2.Name())
|
||||
defer os.Remove(file1.Name())
|
||||
|
||||
test([]string{"noarg"}, file1, []string{})
|
||||
test([]string{"FileArg", file1.Name()}, nil, []string{file1.Name()})
|
||||
test([]string{"FileArg+Variadic", file1.Name(), file2.Name()}, nil,
|
||||
[]string{file1.Name(), file2.Name()})
|
||||
test([]string{"FileArg+Stdin"}, file1, []string{file1.Name()})
|
||||
test([]string{"FileArg+Stdin", "-"}, file1, []string{file1.Name()})
|
||||
test([]string{"FileArg+Stdin", file1.Name(), "-"}, file2,
|
||||
[]string{file1.Name(), file2.Name()})
|
||||
test([]string{"StringArg+FileArg",
|
||||
"foo", file1.Name()}, nil, []string{"foo", file1.Name()})
|
||||
test([]string{"StringArg+FileArg+Variadic",
|
||||
"foo", file1.Name(), file2.Name()}, nil,
|
||||
[]string{"foo", file1.Name(), file2.Name()})
|
||||
test([]string{"StringArg+FileArg+Stdin",
|
||||
"foo", file1.Name(), "-"}, file2,
|
||||
[]string{"foo", file1.Name(), file2.Name()})
|
||||
test([]string{"StringArg+FileArg+Variadic+Stdin",
|
||||
"foo", file1.Name(), file2.Name()}, file3,
|
||||
[]string{"foo", file1.Name(), file2.Name()})
|
||||
test([]string{"StringArg+FileArg+Variadic+Stdin",
|
||||
"foo", file1.Name(), file2.Name(), "-"}, file3,
|
||||
[]string{"foo", file1.Name(), file2.Name(), file3.Name()})
|
||||
test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||
|
||||
fstdin := fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"stdinenabled"}, fstdin, []string{"stdin1"})
|
||||
test([]string{"stdinenabled", "value1"}, fstdin, []string{"value1"})
|
||||
test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"value1", "value2"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1\nstdin2")
|
||||
test([]string{"stdinenabled"}, fstdin, []string{"stdin1", "stdin2"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1\nstdin2\nstdin3")
|
||||
test([]string{"stdinenabled"}, fstdin, []string{"stdin1", "stdin2", "stdin3"})
|
||||
|
||||
test([]string{"stdinenabled2args", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"stdinenabled2args", "value1"}, fstdin, []string{"value1", "stdin1"})
|
||||
test([]string{"stdinenabled2args", "value1", "value2"}, fstdin, []string{"value1", "value2"})
|
||||
test([]string{"stdinenabled2args", "value1", "value2", "value3"}, fstdin, []string{"value1", "value2", "value3"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1\nstdin2")
|
||||
test([]string{"stdinenabled2args", "value1"}, fstdin, []string{"value1", "stdin1", "stdin2"})
|
||||
|
||||
test([]string{"stdinenablednotvariadic", "value1"}, nil, []string{"value1"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"stdinenablednotvariadic"}, fstdin, []string{"stdin1"})
|
||||
test([]string{"stdinenablednotvariadic", "value1"}, fstdin, []string{"value1"})
|
||||
|
||||
test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"stdinenablednotvariadic2args", "value1"}, fstdin, []string{"value1", "stdin1"})
|
||||
test([]string{"stdinenablednotvariadic2args", "value1", "value2"}, fstdin, []string{"value1", "value2"})
|
||||
testFail([]string{"stdinenablednotvariadic2args"}, fstdin, "cant use stdin for non stdin arg")
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"noarg"}, fstdin, []string{})
|
||||
|
||||
fstdin = fileToSimulateStdin(t, "stdin1")
|
||||
test([]string{"optionalsecond", "value1", "value2"}, fstdin, []string{"value1", "value2"})
|
||||
}
|
||||
|
Reference in New Issue
Block a user