Repository navigation
Expand file tree
/
Copy pathusage_test.go
More file actions
102 lines (95 loc) · 2.9 KB
/
Copy pathusage_test.go
File metadata and controls
102 lines (95 loc) · 2.9 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
99
100
101
102
package cli
import (
"fmt"
"io"
"strings"
"testing"
)
type usageOpts struct {
C bool `cli:"name=c desc='include comments'"`
W bool `cli:"name=w desc='write the normalized form back to each file'"`
N string `cli:"name=name desc='a name'"`
}
func usageTree(t *testing.T, withOpts bool) *Command {
t.Helper()
child := NewCommand("view").WithAliases("v").
WithSynopsis("view [files]").WithDescription("view object files")
if withOpts {
o, err := StructOpts(&usageOpts{})
if err != nil {
t.Fatal(err)
}
child = child.WithOpts(o...)
}
NewCommand("o").WithSynopsis("o <cmd>").WithDescription("the tool").WithSubs(child)
return child
}
func renderUsage(t *testing.T, cmd *Command, err error) string {
t.Helper()
var out, errOut strings.Builder
cc := &Context{
Out: nopCloser{&out},
Err: nopCloser{&errOut},
In: io.NopCloser(strings.NewReader("")),
}
cmd.Usage(cc, err)
if err != nil {
return errOut.String()
}
return out.String()
}
// Usage ends with a newline.
//
// Each option is written with its newline in FRONT, so the last one had none behind
// it: the usage ended mid-line, a shell prompt landed on top of the last option, and
// anything reading the output as lines lost it.
//
// -w write the normalized form back to each file bool%
func TestUsageEndsWithANewline(t *testing.T) {
for _, tc := range []struct {
name string
cmd *Command
err error
}{
{"a command with options", usageTree(t, true), nil},
{"a command with none", usageTree(t, false), nil},
{"a command with options, after a usage error", usageTree(t, true), fmt.Errorf("%w: bad", ErrUsage)},
{"the root", usageTree(t, true).Parent, nil},
} {
t.Run(tc.name, func(t *testing.T) {
got := renderUsage(t, tc.cmd, tc.err)
if got == "" {
t.Fatal("no usage written")
}
if !strings.HasSuffix(got, "\n") {
t.Errorf("usage ends mid-line: %q", got[max(0, len(got)-60):])
}
})
}
}
// The terminator goes on w AFTER the flush, not into the tabwriter: a terminated
// row is one tabwriter pads to the column width, which would put trailing spaces on
// every option line.
func TestUsageDoesNotPadEveryLine(t *testing.T) {
got := renderUsage(t, usageTree(t, true), nil)
for _, line := range strings.Split(strings.TrimSuffix(got, "\n"), "\n") {
if !strings.Contains(line, " ") || !strings.HasPrefix(line, " -") {
continue // not an option row
}
if strings.HasSuffix(line, " ") {
t.Errorf("an option row is padded with trailing space: %q", line)
}
}
}
// and the options themselves are all there, each on its own line
func TestUsageListsEveryOption(t *testing.T) {
got := renderUsage(t, usageTree(t, true), nil)
for _, want := range []string{"-c", "-w", "-name", "include comments", "a name"} {
if !strings.Contains(got, want) {
t.Errorf("usage does not mention %q:\n%s", want, got)
}
}
if n := strings.Count(got, "\n -"); n != 3 {
t.Errorf("%d option rows, want 3:\n%s", n, got)
}
}