-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_test.go
More file actions
98 lines (91 loc) · 2.16 KB
/
usage_test.go
File metadata and controls
98 lines (91 loc) · 2.16 KB
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
package cli
import (
"bytes"
"embed"
"io"
"io/fs"
"testing"
)
//go:embed testdata
var testdata embed.FS
func newTestUsage(t *testing.T) fs.FS {
t.Helper()
usage, err := fs.Sub(testdata, "testdata")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return NewUsageFS(usage)
}
func TestUsage(t *testing.T) {
var tests = []struct {
scope string
args []string
want string
}{
{
"",
[]string{"appname", "help"},
"README.md\n",
},
{
"",
[]string{"appname", "help", "test"},
"test.md\n",
},
{
"cli",
[]string{"appname", "help"},
"cli/README.md\n",
},
{
"cli",
[]string{"appname", "help", "test"},
"cli/test.md\n",
},
}
for _, tt := range tests {
var buf bytes.Buffer
opts := []Option{Scope(tt.scope), Stdout(&buf), Stderr(io.Discard)}
app := New("appname", newTestUsage(t), nil, opts...)
app.Add("test", testCommand, nil)
err := app.Run(tt.args)
if err != nil {
t.Fatalf("help args=%v scope='%s'\ncommand should not error", tt.args, tt.scope)
}
have := buf.String()
if have != tt.want {
t.Fatalf("help %v\nhave '%s'\nwant '%s'", tt.args, have, tt.want)
}
}
}
func TestUsageNil(t *testing.T) {
app := New("appname", nil, nil, Stdout(io.Discard), Stderr(io.Discard))
err := app.Usage(nil, "test")
if err != ErrExitFailure {
t.Fatalf("usage should error")
}
}
func TestUsageRoot(t *testing.T) {
for _, scope := range []string{"", "cli"} {
var buf bytes.Buffer
opts := []Option{Scope(scope), Stdout(io.Discard), Stderr(&buf)}
app := New("appname", newTestUsage(t), nil, opts...)
err := app.Run([]string{"appname"})
if err != ErrExitFailure {
t.Fatalf("help root command scope='%s'\ncommand should error", scope)
}
have := buf.String()
want := "README.md\n"
if have != want {
t.Fatalf("help root command scope='%s'\nhave '%s'\nwant '%s'", scope, have, want)
}
}
}
func TestUsageNotFound(t *testing.T) {
app := New("appname", newTestUsage(t), nil, Stdout(io.Discard), Stderr(io.Discard))
app.Add("test", func([]string) error { return nil }, nil)
err := app.Run([]string{"appname", "help", "not-found"})
if err != ErrExitFailure {
t.Fatalf("help command should error")
}
}