-
Notifications
You must be signed in to change notification settings - Fork 5
/
gocmd_in_test.go
152 lines (138 loc) · 5.03 KB
/
gocmd_in_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
// gocmd
// For the full copyright and license information, please view the LICENSE.txt file.
package gocmd
import (
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var osArgs []string
func init() {
osArgs = make([]string, len(os.Args))
copy(osArgs, os.Args)
}
func resetArgs() {
os.Args = make([]string, len(osArgs))
copy(os.Args, osArgs)
}
func TestCmd_usageItems(t *testing.T) {
Convey("should return the correct usage items", t, func() {
cmd, err := New(Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"f" long:"foo" description:"Test foo"`
Bar struct {
Baz bool `short:"b" long:"baz" description:"Test baz"`
Qux struct {
Quux bool `short:"q" long:"quux" description:"Test quux"`
String string `short:"s" long:"string" default:"/go" env:"HOME" description:"Test"`
Default string `short:"d" long:"default" default:"default" description:"Test"`
Env string `short:"e" long:"env" env:"HOME" description:"Test"`
} `command:"qux" description:"Qux command"`
} `command:"bar" description:"Bar command"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
usageItems := cmd.usageItems("arg", -1, 0)
So(usageItems, ShouldNotBeNil)
So(usageItems, ShouldHaveLength, 1)
So(usageItems[0].kind, ShouldEqual, "arg")
So(usageItems[0].flagID, ShouldEqual, 0)
So(usageItems[0].parentID, ShouldEqual, -1)
So(usageItems[0].left, ShouldEqual, "-f, --foo")
So(usageItems[0].right, ShouldEqual, "Test foo")
So(usageItems[0].level, ShouldEqual, 1)
usageItems = cmd.usageItems("command", -1, 0)
So(usageItems, ShouldNotBeNil)
So(usageItems, ShouldHaveLength, 7)
So(usageItems[0].kind, ShouldEqual, "command")
So(usageItems[0].flagID, ShouldEqual, 1)
So(usageItems[0].parentID, ShouldEqual, -1)
So(usageItems[0].left, ShouldEqual, "bar")
So(usageItems[0].right, ShouldEqual, "Bar command")
So(usageItems[0].level, ShouldEqual, 1)
So(usageItems[1].kind, ShouldEqual, "arg")
So(usageItems[1].flagID, ShouldEqual, 2)
So(usageItems[1].parentID, ShouldEqual, 1)
So(usageItems[1].left, ShouldEqual, "-b, --baz")
So(usageItems[1].right, ShouldEqual, "Test baz")
So(usageItems[1].level, ShouldEqual, 2)
So(usageItems[2].kind, ShouldEqual, "command")
So(usageItems[2].flagID, ShouldEqual, 3)
So(usageItems[2].parentID, ShouldEqual, 1)
So(usageItems[2].left, ShouldEqual, "qux")
So(usageItems[2].right, ShouldEqual, "Qux command")
So(usageItems[2].level, ShouldEqual, 2)
So(usageItems[3].kind, ShouldEqual, "arg")
So(usageItems[3].flagID, ShouldEqual, 4)
So(usageItems[3].parentID, ShouldEqual, 3)
So(usageItems[3].left, ShouldEqual, "-q, --quux")
So(usageItems[3].right, ShouldEqual, "Test quux")
So(usageItems[3].level, ShouldEqual, 3)
So(usageItems[4].left, ShouldEqual, "-s, --string")
So(usageItems[4].right, ShouldEqual, "Test (default /go - override $HOME)")
So(usageItems[4].level, ShouldEqual, 3)
So(usageItems[5].left, ShouldEqual, "-d, --default")
So(usageItems[5].right, ShouldEqual, "Test (default default)")
So(usageItems[5].level, ShouldEqual, 3)
So(usageItems[6].left, ShouldEqual, "-e, --env")
So(usageItems[6].right, ShouldEqual, "Test (default $HOME)")
So(usageItems[6].level, ShouldEqual, 3)
})
}
func TestCmd_usageContent(t *testing.T) {
Convey("should return correct usage content", t, func() {
cmd, err := New(Options{
Name: "test",
Version: "1.0.0",
Description: "Test",
Flags: &struct {
Foo bool `short:"f" long:"foo" description:"Test foo"`
Bar bool `short:"b" description:"Test bar"`
Baz bool `long:"baz" description:"Test baz"`
Qux struct {
Foo bool `short:"f" long:"foo" description:"Test foo"`
Quux bool `long:"quux" default:"test" description:"Test quux"`
} `command:"qux" description:"Qux command"`
}{},
})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
usage := cmd.usageContent()
So(usage, ShouldNotBeEmpty)
So(usage, ShouldEqual, "Usage: test [options...] COMMAND [options...]\n\nTest\n\nOptions:\n -f, --foo \tTest foo\n -b \tTest bar\n --baz \tTest baz\n\nCommands:\n qux \tQux command\n -f, --foo \tTest foo\n --quux \tTest quux (default test)\n")
})
}
func TestCmd_isTest(t *testing.T) {
Convey("should return whether it's a test", t, func() {
cmd, err := New(Options{Name: "test"})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.isTest(), ShouldEqual, true)
os.Args = []string{"./app", "-test."}
cmd, err = New(Options{Name: "test"})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.isTest(), ShouldEqual, true)
resetArgs()
os.Args = []string{"./app"}
cmd, err = New(Options{Name: "test"})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
So(cmd.isTest(), ShouldEqual, false)
resetArgs()
})
}
func TestCmd_exit(t *testing.T) {
Convey("should not exit", t, func() {
os.Args = []string{"gocmd.test"}
cmd, err := New(Options{Name: "test"})
So(err, ShouldBeNil)
So(cmd, ShouldNotBeNil)
cmd.exit(0)
resetArgs()
})
}