internal/control/parse_test.go
52 lines · 2406 bytes
1package control
2
3import (
4 "bytes"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/protocol"
9 "gitbay.org/gitbay/internal/store"
10)
11
12// TestCommandParseFailures runs argv that every command must refuse
13// before touching the store: an unknown flag, a flag with no value, too
14// many arguments, a value outside its range. Each is exit 2 with the
15// command's usage in the message (#129, #96).
16func TestCommandParseFailures(t *testing.T) {
17 admin := store.User{Username: "root", IsAdmin: true}
18 cases := []struct {
19 argv []string
20 user store.User
21 want string // substring of the message
22 }{
23 {[]string{"issue", "create", "a/b", "--bogus"}, store.User{}, "unknown flag"},
24 {[]string{"issue", "create", "a/b", "--title"}, store.User{}, "requires a value"},
25 {[]string{"issue", "list", "a/b", "c/d"}, store.User{}, "unexpected argument"},
26 {[]string{"mr", "create", "a/b", "--source"}, store.User{}, "requires a value"},
27 {[]string{"mr", "merge", "a/b", "1", "--strategy"}, store.User{}, "requires a value"},
28 {[]string{"mr", "review", "a/b", "1", "--bogus"}, store.User{}, "usage"},
29 {[]string{"repo", "grep", "a/b", "x", "y"}, store.User{}, "unexpected argument"},
30 {[]string{"repo", "create", "a/b", "--visibility", "x"}, store.User{}, "unknown flag"},
31 {[]string{"repo", "log", "a/b", "--limit", "0"}, store.User{}, "--limit must be"},
32 {[]string{"keys", "add", "--scope"}, store.User{}, "requires a value"},
33 {[]string{"token", "create", "extra"}, store.User{}, "unexpected argument"},
34 {[]string{"webhook", "deliveries", "a/b", "--limit", "500"}, store.User{}, "--limit must be"},
35 {[]string{"admin", "repo", "list", "--visibility", "secret"}, admin, "--visibility requires"},
36 {[]string{"admin", "user", "list", "extra"}, admin, "unexpected argument"},
37 {[]string{"audit", "--limit", "0"}, admin, "--limit must be"},
38 {[]string{"account", "import-bundle", "x"}, store.User{}, "unexpected argument"},
39 }
40 for _, tc := range cases {
41 var out, errOut bytes.Buffer
42 c := &Ctx{User: tc.user, Scope: "full", Stdout: &out, Stderr: &errOut}
43 code := Dispatch(c, tc.argv)
44 if code != protocol.ExitUsage {
45 t.Errorf("%v: exit %d, want %d (%s)", tc.argv, code, protocol.ExitUsage, strings.TrimSpace(errOut.String()))
46 continue
47 }
48 if !strings.Contains(errOut.String(), tc.want) {
49 t.Errorf("%v: message %q lacks %q", tc.argv, strings.TrimSpace(errOut.String()), tc.want)
50 }
51 }
52}