mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 10:49:24 +08:00
Merge pull request #1193 from ipfs/improve_parse_test
Improve parse_test.go
This commit is contained in:
@ -3,10 +3,67 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/ipfs/go-ipfs/commands"
|
"github.com/ipfs/go-ipfs/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type kvs map[string]interface{}
|
||||||
|
type words []string
|
||||||
|
|
||||||
|
func sameWords(a words, b words) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i, w := range a {
|
||||||
|
if w != b[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func sameKVs(a kvs, b kvs) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for k, v := range a {
|
||||||
|
if v != b[k] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSameWords(t *testing.T) {
|
||||||
|
a := []string{"v1", "v2"}
|
||||||
|
b := []string{"v1", "v2", "v3"}
|
||||||
|
c := []string{"v2", "v3"}
|
||||||
|
d := []string{"v2"}
|
||||||
|
e := []string{"v2", "v3"}
|
||||||
|
f := []string{"v2", "v1"}
|
||||||
|
|
||||||
|
test := func(a words, b words, v bool) {
|
||||||
|
if sameWords(a, b) != v {
|
||||||
|
t.Errorf("sameWords('%v', '%v') != %v", a, b, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test(a, b, false)
|
||||||
|
test(a, a, true)
|
||||||
|
test(a, c, false)
|
||||||
|
test(b, c, false)
|
||||||
|
test(c, d, false)
|
||||||
|
test(c, e, true)
|
||||||
|
test(b, e, false)
|
||||||
|
test(a, b, false)
|
||||||
|
test(a, f, false)
|
||||||
|
test(e, f, false)
|
||||||
|
test(f, f, true)
|
||||||
|
}
|
||||||
|
|
||||||
func TestOptionParsing(t *testing.T) {
|
func TestOptionParsing(t *testing.T) {
|
||||||
subCmd := &commands.Command{}
|
subCmd := &commands.Command{}
|
||||||
cmd := &commands.Command{
|
cmd := &commands.Command{
|
||||||
@ -19,30 +76,6 @@ func TestOptionParsing(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type kvs map[string]interface{}
|
|
||||||
type words []string
|
|
||||||
|
|
||||||
sameWords := func(a words, b words) bool {
|
|
||||||
for i, w := range a {
|
|
||||||
if w != b[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
sameKVs := func(a kvs, b kvs) bool {
|
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for k, v := range a {
|
|
||||||
if v != b[k] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
testHelper := func(args string, expectedOpts kvs, expectedWords words, expectErr bool) {
|
testHelper := func(args string, expectedOpts kvs, expectedWords words, expectErr bool) {
|
||||||
_, opts, input, _, err := parseOpts(strings.Split(args, " "), cmd)
|
_, opts, input, _, err := parseOpts(strings.Split(args, " "), cmd)
|
||||||
if expectErr {
|
if expectErr {
|
||||||
@ -117,76 +150,78 @@ func TestArgumentParsing(t *testing.T) {
|
|||||||
commands.StringArg("b", true, false, "another arg"),
|
commands.StringArg("b", true, false, "another arg"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
"stdinenabled": &commands.Command{
|
||||||
|
Arguments: []commands.Argument{
|
||||||
|
commands.StringArg("a", true, true, "some arg").EnableStdin(),
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err := Parse([]string{"noarg"}, nil, rootCmd)
|
test := func(cmd words, f *os.File, res words) {
|
||||||
if err != nil {
|
if f != nil {
|
||||||
t.Error("Should have passed")
|
if _, err := f.Seek(0, os.SEEK_SET); err != nil {
|
||||||
}
|
t.Fatal(err)
|
||||||
_, _, _, err = Parse([]string{"noarg", "value!"}, nil, rootCmd)
|
}
|
||||||
if err == nil {
|
}
|
||||||
t.Error("Should have failed (provided an arg, but command didn't define any)")
|
req, _, _, err := Parse(cmd, f, rootCmd)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Command '%v' should have passed parsing", cmd)
|
||||||
|
}
|
||||||
|
if !sameWords(req.Arguments(), res) {
|
||||||
|
t.Errorf("Arguments parsed from '%v' are not '%v'", cmd, res)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err = Parse([]string{"onearg", "value!"}, nil, rootCmd)
|
testFail := func(cmd words, msg string) {
|
||||||
if err != nil {
|
_, _, _, err := Parse(cmd, nil, rootCmd)
|
||||||
t.Error("Should have passed")
|
if err == nil {
|
||||||
}
|
t.Errorf("Should have failed: %v", msg)
|
||||||
_, _, _, err = Parse([]string{"onearg"}, nil, rootCmd)
|
}
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (didn't provide any args, arg is required)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err = Parse([]string{"twoargs", "value1", "value2"}, nil, rootCmd)
|
test([]string{"noarg"}, nil, []string{})
|
||||||
if err != nil {
|
testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any")
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
test([]string{"onearg", "value!"}, nil, []string{"value!"})
|
||||||
_, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd)
|
testFail([]string{"onearg"}, "didn't provide any args, arg is required")
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (only provided 1 arg, needs 2)")
|
test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||||
}
|
testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2")
|
||||||
_, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd)
|
testFail([]string{"twoargs"}, "didn't provide any args, 2 required")
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (didn't provide any args, 2 required)")
|
test([]string{"variadic", "value!"}, nil, []string{"value!"})
|
||||||
|
test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
|
||||||
|
testFail([]string{"variadic"}, "didn't provide any args, 1 required")
|
||||||
|
|
||||||
|
test([]string{"optional", "value!"}, nil, []string{"value!"})
|
||||||
|
test([]string{"optional"}, nil, []string{})
|
||||||
|
|
||||||
|
test([]string{"reversedoptional", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||||
|
test([]string{"reversedoptional", "value!"}, nil, []string{"value!"})
|
||||||
|
|
||||||
|
testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required")
|
||||||
|
testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1")
|
||||||
|
|
||||||
|
// 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())
|
||||||
|
|
||||||
|
if _, err := io.WriteString(fstdin, content); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return fstdin
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, _, err = Parse([]string{"variadic", "value!"}, nil, rootCmd)
|
test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"})
|
||||||
if err != nil {
|
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"variadic", "value1", "value2", "value3"}, nil, rootCmd)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"variadic"}, nil, rootCmd)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (didn't provide any args, 1 required)")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, _, err = Parse([]string{"optional", "value!"}, nil, rootCmd)
|
fstdin := fileToSimulateStdin(t, "stdin1")
|
||||||
if err != nil {
|
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"optional"}, nil, rootCmd)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2"}, nil, rootCmd)
|
test([]string{"stdinenabled"}, fstdin, []string{"stdin1"})
|
||||||
if err != nil {
|
test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"})
|
||||||
t.Error("Should have passed")
|
test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"})
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"reversedoptional", "value!"}, nil, rootCmd)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Should have passed")
|
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (didn't provide any args, 1 required)")
|
|
||||||
}
|
|
||||||
_, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Should have failed (provided too many args, only takes 1)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user