forked from choria-io/fisk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage_test.go
133 lines (111 loc) · 3.58 KB
/
usage_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 fisk
import (
"bytes"
"strings"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
)
func TestFormatTwoColumns(t *testing.T) {
buf := bytes.NewBuffer(nil)
formatTwoColumns(buf, 2, 2, 20, [][2]string{
{"--hello", "Hello world help with something that is cool."},
})
expected := ` --hello Hello
world
help with
something
that is
cool.
`
assert.Equal(t, expected, buf.String())
}
func TestFormatTwoColumnsWide(t *testing.T) {
samples := [][2]string{
{strings.Repeat("x", 29), "29 chars"},
{strings.Repeat("x", 30), "30 chars"}}
buf := bytes.NewBuffer(nil)
formatTwoColumns(buf, 0, 0, 80, samples)
expected := `xxxxxxxxxxxxxxxxxxxxxxxxxxxxx29 chars
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
30 chars
`
assert.Equal(t, expected, buf.String())
}
func TestHiddenCommand(t *testing.T) {
templates := []struct{ name, template string }{
{"default", KingpinDefaultUsageTemplate},
{"Compact", CompactUsageTemplate},
{"Long", LongHelpTemplate},
{"Man", ManPageTemplate},
}
var buf bytes.Buffer
t.Log("1")
a := New("test", "Test").Writer(&buf).Terminate(nil)
a.Command("visible", "visible")
a.Command("hidden", "hidden").Hidden()
for _, tp := range templates {
buf.Reset()
a.UsageTemplate(tp.template)
a.Parse(nil)
// a.Parse([]string{"--help"})
usage := buf.String()
t.Logf("Usage for %s is:\n%s\n", tp.name, usage)
assert.NotContains(t, usage, "hidden")
assert.Contains(t, usage, "visible")
}
}
func TestUsageFuncs(t *testing.T) {
var buf bytes.Buffer
a := New("test", "Test").Writer(&buf).Terminate(nil)
tpl := `{{ add 2 1 }}`
a.UsageTemplate(tpl)
a.UsageFuncs(template.FuncMap{
"add": func(x, y int) int { return x + y },
})
a.Parse([]string{"--help"})
usage := buf.String()
assert.Equal(t, "3", usage)
}
func TestCmdClause_HelpLong(t *testing.T) {
var buf bytes.Buffer
tpl := `{{define "FormatUsage"}}{{.HelpLong}}{{end -}}
{{template "FormatUsage" .Context.SelectedCommand}}`
a := New("test", "Test").Writer(&buf).Terminate(nil).UsageTemplate(KingpinDefaultUsageTemplate)
a.UsageTemplate(tpl)
a.Command("command", "short help text").HelpLong("long help text")
a.Parse([]string{"command", "--help"})
usage := buf.String()
assert.Equal(t, "long help text", usage)
}
func TestArgEnvVar(t *testing.T) {
var buf bytes.Buffer
a := New("test", "Test").Writer(&buf).Terminate(nil).UsageTemplate(KingpinDefaultUsageTemplate)
a.Arg("arg", "Enable arg").Envar("ARG").String()
a.Flag("flag", "Enable flag").Envar("FLAG").String()
a.Parse([]string{"command", "--help"})
usage := buf.String()
assert.Contains(t, usage, "($ARG)")
assert.Contains(t, usage, "($FLAG)")
}
func TestShortMainUSage(t *testing.T) {
var buf bytes.Buffer
a := New("test", "Test Command").UsageWriter(&buf).Terminate(nil)
sub := a.Command("sub", "Sub command").HelpLong("sub long help")
sub.Command("subsub1", "Subsub2 command").HelpLong("subsub1 long help")
sub.Command("subsub2", "Subsub2 command")
a.UsageTemplate(ShorterMainUsageTemplate)
a.Parse([]string{"--help"})
assert.NotContains(t, buf.String(), "long help")
assert.NotContains(t, buf.String(), "Flags:")
buf.Reset()
a.Parse([]string{"sub", "--help"})
assert.Contains(t, buf.String(), "sub long help")
assert.NotContains(t, buf.String(), "subsub1 long help")
assert.NotContains(t, buf.String(), "subsub2 long help")
buf.Reset()
a.Parse([]string{"sub", "subsub1", "--help"})
assert.NotContains(t, buf.String(), "sub long help")
assert.Contains(t, buf.String(), "subsub1 long help")
assert.NotContains(t, buf.String(), "subsub2 long help")
}