-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_help_test.go
138 lines (119 loc) · 2.96 KB
/
command_help_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
134
135
136
137
138
package cli_test
import (
"fmt"
"github.com/phogolabs/cli"
"github.com/phogolabs/cli/fake"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
)
var _ = Describe("ShowHelp", func() {
var (
ctx *cli.Context
parent *cli.Context
buffer *Buffer
)
BeforeEach(func() {
buffer = NewBuffer()
parent = &cli.Context{
Writer: buffer,
Command: &cli.Command{
Name: "root",
HelpName: "root_help",
Usage: "root_usage",
UsageText: "root_usage_text",
Commands: []*cli.Command{
cli.NewHelpCommand(),
&cli.Command{
Name: "action",
HelpName: "action_help",
Usage: "action_usage",
UsageText: "action_usage_text",
},
},
Metadata: cli.Map{
"HideVersion": false,
"Version": "1.0",
"Authors": []*cli.Author{},
"Copyright": "2020",
},
},
Parent: nil,
}
})
Context("when the command is main command", func() {
It("shows the help for the command", func() {
Expect(cli.NewHelpCommand().Action(parent)).To(Succeed())
Expect(buffer).To(Say("COPYRIGHT"))
})
})
Context("when the writer fails", func() {
BeforeEach(func() {
writer := &fake.Writer{}
writer.WriteReturns(0, fmt.Errorf("oh no!"))
parent.Writer = writer
})
It("returns an error", func() {
Expect(cli.NewHelpCommand().Action(parent)).To(MatchError("oh no!"))
})
})
Context("when help is executed for the command", func() {
BeforeEach(func() {
ctx = &cli.Context{
Parent: parent,
Writer: parent.Writer,
Command: cli.NewHelpCommand(),
Args: []string{"action"},
}
})
It("shows the help for the command", func() {
Expect(cli.NewHelpCommand().Action(ctx)).To(Succeed())
Expect(buffer).To(Say("action_help - action_usage"))
})
})
Context("when help is executed for the command that does not exist", func() {
BeforeEach(func() {
ctx = &cli.Context{
Parent: parent,
Writer: parent.Writer,
Command: cli.NewHelpCommand(),
Args: []string{"exec"},
}
})
It("shows the help for the command", func() {
Expect(cli.NewHelpCommand().Action(ctx)).To(Succeed())
Expect(buffer).To(Say("No help topic for 'exec'"))
})
})
Context("when the command is subcommand", func() {
BeforeEach(func() {
ctx = &cli.Context{
Parent: parent,
Writer: parent.Writer,
Command: cli.NewHelpCommand(),
}
})
It("shows the help for the command", func() {
Expect(cli.NewHelpCommand().Action(ctx)).To(Succeed())
Expect(buffer).To(Say("root_help - root_usage"))
})
})
})
var _ = Describe("ShowVersion", func() {
It("shows the version successfully", func() {
buffer := NewBuffer()
ctx := &cli.Context{
Parent: &cli.Context{
Writer: buffer,
Command: &cli.Command{
Name: "app",
Metadata: cli.Map{
"Version": "BETA",
},
},
},
}
Expect(cli.NewVersionCommand().Action(ctx)).To(Succeed())
Expect(buffer).To(Say("app version BETA"))
})
})