-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmenu_test.go
More file actions
123 lines (108 loc) · 3.77 KB
/
Copy pathmenu_test.go
File metadata and controls
123 lines (108 loc) · 3.77 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package exoskeleton
import (
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBuildMenuUsage(t *testing.T) {
entrypoint := &Entrypoint{name: "entrypoint"}
module := &directoryCommand{executableCommand: executableCommand{parent: entrypoint, name: "module", cache: nullCache{}}}
entrypoint.cmds = Commands{module}
menu, _ := buildMenu(entrypoint, &MenuOptions{})
assert.Equal(t, "entrypoint <command> [<args>]", menu.Usage)
menu, _ = buildMenu(module, &MenuOptions{})
assert.Equal(t, "entrypoint module <command> [<args>]", menu.Usage)
}
func TestMenuForTrailer(t *testing.T) {
entrypoint := &Entrypoint{name: "entrypoint"}
module := &directoryCommand{executableCommand: executableCommand{parent: entrypoint, name: "module", cache: nullCache{}}}
entrypoint.cmds = Commands{module}
menu, _ := MenuFor(entrypoint, &MenuOptions{})
assert.Contains(t, menu, "Run \033[96mentrypoint help <command>\033[0m to print information on a specific command.")
menu, _ = MenuFor(module, &MenuOptions{})
assert.Contains(t, menu, "Run \033[96mentrypoint help module <command>\033[0m to print information on a specific command.")
}
func TestBuildMenuRendersSigilWithoutDiscoveringSubcommands(t *testing.T) {
entrypoint := &Entrypoint{name: "entrypoint"}
stub := &stubParent{name: "stub", parent: entrypoint}
entrypoint.cmds = Commands{stub}
menu, errs := buildMenu(entrypoint, &MenuOptions{})
assert.Empty(t, errs)
assert.Equal(t, "stub:", menu.Sections[0].MenuItems[0].Name)
assert.False(t, stub.subcommandsCalled, "buildMenu should not discover subcommands to render the sigil")
}
func TestMenuForSections(t *testing.T) {
entrypoint, err := New([]string{fixtures})
if err != nil {
t.Error(err)
}
scenarios := []struct {
depth int
expected string
}{
{
0,
`COMMANDS
echoargs Echoes the args it received
env Prints environment variables
exit Exits with the given code
go: Provides several commands
hello Prints "hello"
opencli-tool An OpenCLI tool
suggest Suggests arguments`,
},
{
1,
`COMMANDS
echoargs Echoes the args it received
env Prints environment variables
exit Exits with the given code
go build compile packages and dependencies
go mod: module maintenance
hello Prints "hello"
opencli-tool An OpenCLI tool
suggest Suggests arguments`,
},
{
2,
`COMMANDS
echoargs Echoes the args it received
env Prints environment variables
exit Exits with the given code
go build compile packages and dependencies
go mod init initialize new module in current directory
go mod tidy add missing and remove unused modules
hello Prints "hello"
opencli-tool An OpenCLI tool
suggest Suggests arguments`,
},
{
-1,
`COMMANDS
echoargs Echoes the args it received
env Prints environment variables
exit Exits with the given code
go build compile packages and dependencies
go mod init initialize new module in current directory
go mod tidy add missing and remove unused modules
hello Prints "hello"
opencli-tool An OpenCLI tool
suggest Suggests arguments`,
},
}
for _, s := range scenarios {
menu, errs := MenuFor(entrypoint, &MenuOptions{Depth: s.depth})
assert.Empty(t, errs)
assert.Equal(t, s.expected, sections(nocolor(menu)), "Given depth=%d", s.depth)
}
}
// TODO: support NOCOLOR and remove this
func nocolor(s string) string {
re := regexp.MustCompile("\033\\[[;\\d]+m")
return re.ReplaceAllLiteralString(s, "")
}
func sections(s string) string {
lines := strings.SplitAfter(s, "\n")
return strings.TrimRight(strings.Join(lines[3:(len(lines)-1)], ""), "\n")
}