parse_test.go 6.5 KB
Newer Older
1 2 3
package cli

import (
Etienne Laurin's avatar
Etienne Laurin committed
4
	"strings"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"testing"
6 7 8
	"io"
	"io/ioutil"
	"os"
Matt Bell's avatar
Matt Bell committed
9

10
	"github.com/ipfs/go-ipfs/commands"
11 12
)

13 14 15 16
type kvs map[string]interface{}
type words []string

func sameWords(a words, b words) bool {
17 18 19
	if len(a) != len(b) {
		return false
	}
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	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
}

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
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)
}

67
func TestOptionParsing(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
68
	subCmd := &commands.Command{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70
	cmd := &commands.Command{
		Options: []commands.Option{
Etienne Laurin's avatar
Etienne Laurin committed
71 72
			commands.StringOption("string", "s", "a string"),
			commands.BoolOption("bool", "b", "a bool"),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
		},
Matt Bell's avatar
Matt Bell committed
74
		Subcommands: map[string]*commands.Command{
Matt Bell's avatar
Matt Bell committed
75
			"test": subCmd,
Matt Bell's avatar
Matt Bell committed
76
		},
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
77
	}
78

Etienne Laurin's avatar
Etienne Laurin committed
79 80 81 82 83 84 85 86 87 88 89 90
	testHelper := func(args string, expectedOpts kvs, expectedWords words, expectErr bool) {
		_, opts, input, _, err := parseOpts(strings.Split(args, " "), cmd)
		if expectErr {
			if err == nil {
				t.Errorf("Command line '%v' parsing should have failed", args)
			}
		} else if err != nil {
			t.Errorf("Command line '%v' failed to parse: %v", args, err)
		} else if !sameWords(input, expectedWords) || !sameKVs(opts, expectedOpts) {
			t.Errorf("Command line '%v':\n  parsed as  %v %v\n  instead of %v %v",
				args, opts, input, expectedOpts, expectedWords)
		}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	}
Etienne Laurin's avatar
Etienne Laurin committed
92 93 94

	testFail := func(args string) {
		testHelper(args, kvs{}, words{}, true)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
95
	}
Etienne Laurin's avatar
Etienne Laurin committed
96 97 98

	test := func(args string, expectedOpts kvs, expectedWords words) {
		testHelper(args, expectedOpts, expectedWords, false)
Matt Bell's avatar
Matt Bell committed
99
	}
Etienne Laurin's avatar
Etienne Laurin committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	test("-", kvs{}, words{"-"})
	testFail("-b -b")
	test("beep boop", kvs{}, words{"beep", "boop"})
	test("test beep boop", kvs{}, words{"beep", "boop"})
	testFail("-s")
	test("-s foo", kvs{"s": "foo"}, words{})
	test("-sfoo", kvs{"s": "foo"}, words{})
	test("-s=foo", kvs{"s": "foo"}, words{})
	test("-b", kvs{"b": ""}, words{})
	test("-bs foo", kvs{"b": "", "s": "foo"}, words{})
	test("-sb", kvs{"s": "b"}, words{})
	test("-b foo", kvs{"b": ""}, words{"foo"})
	test("--bool foo", kvs{"bool": ""}, words{"foo"})
	testFail("--bool=foo")
	testFail("--string")
	test("--string foo", kvs{"string": "foo"}, words{})
	test("--string=foo", kvs{"string": "foo"}, words{})
	test("-- -b", kvs{}, words{"-b"})
	test("foo -b", kvs{"b": ""}, words{"foo"})
120
}
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

func TestArgumentParsing(t *testing.T) {
	rootCmd := &commands.Command{
		Subcommands: map[string]*commands.Command{
			"noarg": &commands.Command{},
			"onearg": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, false, "some arg"),
				},
			},
			"twoargs": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, false, "some arg"),
					commands.StringArg("b", true, false, "another arg"),
				},
			},
			"variadic": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, true, "some arg"),
				},
			},
			"optional": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("b", false, true, "another arg"),
				},
			},
			"reversedoptional": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", false, false, "some arg"),
					commands.StringArg("b", true, false, "another arg"),
				},
			},
153 154 155 156 157
			"stdinenabled": &commands.Command{
				Arguments: []commands.Argument{
					commands.StringArg("a", true, true, "some arg").EnableStdin(),
				},
			},
158 159 160
		},
	}

161 162 163 164 165 166 167 168 169 170 171 172 173
	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)
			}
		}
		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)
		}
174
	}
175

176 177 178 179 180
	testFail := func(cmd words, msg string) {
		_, _, _, err := Parse(cmd, nil, rootCmd)
		if err == nil {
			t.Errorf("Should have failed: %v", msg)
		}
181 182
	}

183 184
	test([]string{"noarg"}, nil, []string{})
	testFail([]string{"noarg", "value!"}, "provided an arg, but command didn't define any")
185

186 187
	test([]string{"onearg", "value!"}, nil, []string{"value!"})
	testFail([]string{"onearg"}, "didn't provide any args, arg is required")
188

189
	test([]string{"twoargs", "value1", "value2"}, nil, []string{"value1", "value2"})
190 191
	testFail([]string{"twoargs", "value!"}, "only provided 1 arg, needs 2")
	testFail([]string{"twoargs"}, "didn't provide any args, 2 required")
192

193 194
	test([]string{"variadic", "value!"}, nil, []string{"value!"})
	test([]string{"variadic", "value1", "value2", "value3"}, nil, []string{"value1", "value2", "value3"})
195
	testFail([]string{"variadic"}, "didn't provide any args, 1 required")
196

197 198 199 200 201
	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!"})
202

203 204
	testFail([]string{"reversedoptional"}, "didn't provide any args, 1 required")
	testFail([]string{"reversedoptional", "value1", "value2", "value3"}, "provided too many args, only takes 1")
205 206

	// Use a temp file to simulate stdin
207 208 209 210 211 212
	fileToSimulateStdin := func(t *testing.T, content string) (*os.File) {
		fstdin, err := ioutil.TempFile("", "")
		if err != nil {
			t.Fatal(err)
		}
		defer os.Remove(fstdin.Name())
213

214 215 216 217
		if _, err := io.WriteString(fstdin, content); err != nil {
			t.Fatal(err)
		}
		return fstdin
218 219
	}

220
	test([]string{"stdinenabled", "value1", "value2"}, nil, []string{"value1", "value2"})
221 222 223

	fstdin := fileToSimulateStdin(t, "stdin1")

224 225 226
	test([]string{"stdinenabled"}, fstdin, []string{"stdin1"})
	test([]string{"stdinenabled", "value1"}, fstdin, []string{"stdin1", "value1"})
	test([]string{"stdinenabled", "value1", "value2"}, fstdin, []string{"stdin1", "value1", "value2"})
227 228 229 230 231 232

	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"})
233
}