forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
244 lines (194 loc) · 8.21 KB
/
main_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main_test
import (
"bufio"
"os"
"os/exec"
"path/filepath"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
)
var _ = Describe("main", func() {
var (
old_PLUGINS_HOME string
)
BeforeEach(func() {
old_PLUGINS_HOME = os.Getenv("CF_PLUGIN_HOME")
dir, err := os.Getwd()
Expect(err).NotTo(HaveOccurred())
fullDir := filepath.Join(dir, "..", "fixtures", "config", "main-plugin-test-config")
err = os.Setenv("CF_PLUGIN_HOME", fullDir)
Expect(err).NotTo(HaveOccurred())
})
AfterEach(func() {
err := os.Setenv("CF_PLUGIN_HOME", old_PLUGINS_HOME)
Expect(err).NotTo(HaveOccurred())
})
Describe("Help menu with -h/--help", func() {
It("prints the help output with our custom template when run with 'cf -h'", func() {
output := Cf("-h").Wait(1 * time.Second)
Eventually(output.Out.Contents).Should(ContainSubstring("A command line tool to interact with Cloud Foundry"))
Eventually(output.Out.Contents).Should(ContainSubstring("CF_TRACE=true"))
})
It("prints the help output with our custom template when run with 'cf --help'", func() {
output := Cf("--help").Wait(1 * time.Second)
Eventually(output.Out.Contents).Should(ContainSubstring("A command line tool to interact with Cloud Foundry"))
Eventually(output.Out.Contents).Should(ContainSubstring("CF_TRACE=true"))
})
It("accepts -h and --h flags for all commands", func() {
result := Cf("push", "-h")
Consistently(result.Out).ShouldNot(Say("Incorrect Usage"))
Eventually(result.Out.Contents).Should(ContainSubstring("USAGE"))
result = Cf("target", "--h")
Consistently(result.Out).ShouldNot(Say("Incorrect Usage"))
Eventually(result.Out.Contents).Should(ContainSubstring("USAGE"))
})
})
Describe("Shows version with -v", func() {
It("prints the cf version if '-v' flag is provided", func() {
output := Cf("-v").Wait(1 * time.Second)
Eventually(output.Out.Contents).Should(ContainSubstring("version"))
Ω(output.ExitCode()).To(Equal(0))
})
})
Describe("Commands /w new non-codegangsta structure", func() {
It("prints usage help for all non-codegangsta commands by providing `help` flag", func() {
output := Cf("api", "-h").Wait(1 * time.Second)
Eventually(output.Out.Contents).Should(ContainSubstring("USAGE"))
Eventually(output.Out.Contents).Should(ContainSubstring("OPTIONS"))
})
It("accepts -h and --h flags for non-codegangsta commands", func() {
result := Cf("api", "-h")
Consistently(result.Out).ShouldNot(Say("Invalid flag: -h"))
Eventually(result.Out.Contents).Should(ContainSubstring("api - Set or view target api url"))
result = Cf("api", "--h")
Consistently(result.Out).ShouldNot(Say("Invalid flag: --h"))
Eventually(result.Out.Contents).Should(ContainSubstring("api - Set or view target api url"))
})
It("runs requirement of the non-codegangsta command", func() {
dir, err := os.Getwd()
Expect(err).ToNot(HaveOccurred())
fullDir := filepath.Join(dir, "..", "fixtures") //set home to a config w/o targeted api
result := CfWith_CF_HOME(fullDir, "app", "app-should-never-exist-blah-blah")
Eventually(result.Out).Should(Say("No API endpoint set."))
})
})
Describe("exit codes", func() {
It("exits non-zero when an unknown command is invoked", func() {
result := Cf("some-command-that-should-never-actually-be-a-real-thing-i-can-use")
Eventually(result, 3*time.Second).Should(Say("not a registered command"))
Eventually(result).Should(Exit(1))
})
It("exits non-zero when known command is invoked with invalid option", func() {
result := Cf("push", "--crazy")
Eventually(result).Should(Exit(1))
})
})
It("can print help menu by executing only the command `cf`", func() {
output := Cf().Wait(3 * time.Second)
Eventually(output.Out.Contents).Should(ContainSubstring("A command line tool to interact with Cloud Foundry"))
})
Describe("Plugins", func() {
It("Can call a plugin command from the Plugins configuration if it does not exist as a cf command", func() {
output := Cf("test_1_cmd1").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("You called cmd1 in test_1"))
})
It("Can call a plugin command via alias if it does not exist as a cf command", func() {
output := Cf("test_1_cmd1_alias").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("You called cmd1 in test_1"))
})
It("Can call another plugin command when more than one plugin is installed", func() {
output := Cf("test_2_cmd1").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("You called cmd1 in test_2"))
})
It("informs user for any invalid commands", func() {
output := Cf("foo-bar")
Eventually(output.Out, 3*time.Second).Should(Say("'foo-bar' is not a registered command"))
})
It("Calls help if the plugin shares the same name", func() {
output := Cf("help")
Consistently(output.Out, 1).ShouldNot(Say("You called help in test_with_help"))
})
It("shows help with a '-h' or '--help' flag in plugin command", func() {
output := Cf("test_1_cmd1", "-h").Wait(3 * time.Second)
Eventually(output.Out).ShouldNot(Say("You called cmd1 in test_1"))
Eventually(output.Out.Contents).Should(ContainSubstring("USAGE:"))
Eventually(output.Out.Contents).Should(ContainSubstring("OPTIONS:"))
})
It("Calls the core push command if the plugin shares the same name", func() {
output := Cf("push")
Consistently(output.Out, 1).ShouldNot(Say("You called push in test_with_push"))
})
It("Calls the core short name if a plugin shares the same name", func() {
output := Cf("p")
Consistently(output.Out, 1).ShouldNot(Say("You called p within the plugin"))
})
It("Passes all arguments to a plugin", func() {
output := Cf("my-say", "foo").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("foo"))
})
It("Passes all arguments and flags to a plugin", func() {
output := Cf("my-say", "foo", "--loud").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("FOO"))
})
It("Calls a plugin that calls core commands", func() {
output := Cf("awesomeness").Wait(3 * time.Second)
Eventually(output.Out).Should(Say("my-say")) //look for another plugin
})
It("Sends stdoutput to the plugin to echo", func() {
output := Cf("core-command", "plugins").Wait(3 * time.Second)
Eventually(output.Out.Contents).Should(MatchRegexp("Command output from the plugin(.*\\W)*awesomeness(.*\\W)*FIN"))
})
It("Can call a core commmand from a plugin without terminal output", func() {
output := Cf("core-command-quiet", "plugins").Wait(3 * time.Second)
Eventually(output.Out.Contents).Should(MatchRegexp("^\n---------- Command output from the plugin"))
})
It("Can call a plugin that requires stdin (interactive)", func() {
session := CfWithIo("input", "silly\n").Wait(5 * time.Second)
Eventually(session.Out).Should(Say("silly"))
})
It("exits 1 when a plugin panics", func() {
session := Cf("panic").Wait(5 * time.Second)
Eventually(session).Should(Exit(1))
})
It("exits 1 when a plugin exits 1", func() {
session := Cf("exit1").Wait(5 * time.Second)
Eventually(session).Should(Exit(1))
})
})
})
func Cf(args ...string) *Session {
path, err := Build("github.com/cloudfoundry/cli/main")
Expect(err).NotTo(HaveOccurred())
session, err := Start(exec.Command(path, args...), GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
return session
}
func CfWithIo(command string, args string) *Session {
path, err := Build("github.com/cloudfoundry/cli/main")
Expect(err).NotTo(HaveOccurred())
cmd := exec.Command(path, command)
stdin, err := cmd.StdinPipe()
Expect(err).ToNot(HaveOccurred())
buffer := bufio.NewWriter(stdin)
buffer.WriteString(args)
buffer.Flush()
session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
return session
}
func CfWith_CF_HOME(cfHome string, args ...string) *Session {
path, err := Build("github.com/cloudfoundry/cli/main")
Expect(err).NotTo(HaveOccurred())
cmd := exec.Command(path, args...)
cmd.Env = append(cmd.Env, "CF_HOME="+cfHome)
session, err := Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
return session
}
// gexec.Build leaves a compiled binary behind in /tmp.
var _ = AfterSuite(func() {
CleanupBuildArtifacts()
})