-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcomandante_test.go
133 lines (104 loc) · 3.12 KB
/
comandante_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
package comandante
import (
"bytes"
"flag"
"os"
"strings"
"testing"
)
func TestComandanteNew(t *testing.T) {
c := New("binaryName", "binaryDescription")
// the commandante should have binaryName and binaryDescription set
if c.binaryName != "binaryName" {
t.Error("comandante binaryName is incorrect")
}
if c.description != "binaryDescription" {
t.Error("comandante description is incorrect")
}
// the length of registered commands should be 0
if len(c.registeredCommands) != 0 {
t.Error("registeredCommands should initially be zero")
}
}
func TestRunCommand(t *testing.T) {
c := New("binaryName", "")
a := false
cmd := NewCommand("test", "short description", func() error { a = true; return nil })
c.RegisterCommand(cmd)
oldArgs := os.Args
os.Args = []string{"bin", "test"}
c.Run()
if a != true {
t.Error("a should be true. It looks like the command didn't run")
}
os.Args = oldArgs
}
func TestRegisterCommand(t *testing.T) {
c := New("binaryName", "")
cmd := NewCommand("test", "short description", func() error { return nil })
c.RegisterCommand(cmd)
// one command should be registered
if len(c.registeredCommands) != 1 {
t.Error("registeredCommands should be 1")
}
// adding a second command with the same name should fail
err := c.RegisterCommand(cmd)
if err == nil {
t.Error("RegisterCommand should fail when adding two commands of the same name")
}
}
func TestCommandArgs(t *testing.T) {
c := New("binaryName", "")
testFlag := ""
cmd := NewCommand("test", "short description", func() error { return nil })
cmd.FlagInit = func(fs *flag.FlagSet) {
fs.StringVar(&testFlag, "testing", "", "This is the usage")
}
cmd.FlagInit(&cmd.flagSet)
c.RegisterCommand(cmd)
cmd.parseFlags([]string{"--testing=value"})
if testFlag != "value" {
t.Error("parsing a sub command flag did not work", testFlag)
}
}
func TestDefaultHelp(t *testing.T) {
c := New("binaryName", "Handles things")
b := bytes.NewBufferString("")
cmd := createHelpCommand(c, b)
c.RegisterCommand(cmd)
othercmd := NewCommand("other", "does stuff", func() error { return nil })
c.RegisterCommand(othercmd)
c.printDefaultHelp(b)
if !strings.Contains(b.String(), "other") {
t.Error("Missing the 'other' command")
}
if !strings.Contains(b.String(), "help") {
t.Error("Missing the 'help' command")
}
if !strings.Contains(b.String(), "more information about a command") {
t.Error("Missing extra help text")
}
}
func TestHelpCommand(t *testing.T) {
c := New("binaryName", "")
b := bytes.NewBufferString("")
cmd := createHelpCommand(c, b)
c.RegisterCommand(cmd)
othercmd := NewCommand("other", "short", func() error { return nil })
doc := "The documentation"
othercmd.Documentation = doc
c.RegisterCommand(othercmd)
// the command should be found
oldArgs := os.Args
os.Args = []string{"bin", "help", "other"}
found := c.getCommand("help")
if found == nil {
t.Error("Help command was not registered")
}
// running the command should write help text to our buffer
found.Action()
if !strings.Contains(b.String(), doc) {
t.Error("printing documentation from the help command did not work")
}
os.Args = oldArgs
}