forked from guidowb/cf-targets-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cf_targets_test.go
204 lines (178 loc) · 7.11 KB
/
cf_targets_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
package main
import (
realos "os"
. "code.cloudfoundry.org/cli/cf/util/testhelpers/io"
. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
fakes "code.cloudfoundry.org/cli/plugin/pluginfakes"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
type FakeOS struct {
exitCalled int
exitCalledWithCode int
mkdirCalled int
mkdirCalledWithPath string
mkdirCalledWithMode realos.FileMode
removeCalled int
removeCalledWithPath string
symlinkCalled int
symlinkCalledWithTarget string
symlinkCalledWithSource string
readdirCalled int
readdirCalledWithPath string
readfileCalled int
readfileCalledWithPath string
writefileCalled int
writefileCalledWithPath string
writefileCalledWithContent []byte
writefileCalledWithMode realos.FileMode
readdirShouldReturn []realos.FileInfo
readfileShouldReturn []byte
}
func (os *FakeOS) Exit(code int) {
os.exitCalled++
os.exitCalledWithCode = code
}
func (os *FakeOS) Mkdir(path string, mode realos.FileMode) {
os.mkdirCalled++
os.mkdirCalledWithPath = path
os.mkdirCalledWithMode = mode
}
func (os *FakeOS) Remove(path string) {
os.removeCalled++
os.removeCalledWithPath = path
}
func (os *FakeOS) Symlink(target string, source string) error {
os.symlinkCalled++
os.symlinkCalledWithTarget = target
os.symlinkCalledWithSource = source
return nil
}
func (os *FakeOS) ReadDir(path string) ([]realos.FileInfo, error) {
os.readdirCalled++
os.readdirCalledWithPath = path
return os.readdirShouldReturn, nil
}
func (os *FakeOS) ReadFile(path string) ([]byte, error) {
os.readfileCalled++
os.readfileCalledWithPath = path
return os.readfileShouldReturn, nil
}
func (os *FakeOS) WriteFile(path string, content []byte, mode realos.FileMode) error {
os.writefileCalled++
os.writefileCalledWithPath = path
os.writefileCalledWithContent = content
os.writefileCalledWithMode = mode
return nil
}
var _ = Describe("TargetsPlugin", func() {
var fakeCliConnection *fakes.FakeCliConnection
var targetsPlugin *TargetsPlugin
var fakeOS FakeOS
BeforeEach(func() {
fakeOS = FakeOS{}
os = &fakeOS
fakeCliConnection = &fakes.FakeCliConnection{}
targetsPlugin = newTargetsPlugin()
})
Describe("Command Syntax", func() {
It("displays usage when targets called with too many arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"targets", "blah"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "targets"}))
})
It("displays usage when set-target called with too many arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"set-target", "blah", "blah"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "set-target", "[-f]", "NAME"}))
})
It("displays usage when set-target called with too few arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"set-target"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "set-target", "[-f]", "NAME"}))
})
It("displays usage when set-target called with unsupported option", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"set-target", "blah", "-k"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "set-target", "[-f]", "NAME"}))
})
It("displays usage when save-target called with too many arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"save-target", "blah", "blah"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "save-target", "[-f]", "[NAME]"}))
})
It("displays usage when save-target called with unsupported option", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"save-target", "blah", "-k"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "save-target", "[-f]", "[NAME]"}))
})
It("displays usage when delete-target called with too few arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"delete-target"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "delete-target", "NAME"}))
})
It("displays usage when delete-target called with too many arguments", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"delete-target", "blah", "blah"})
})
Expect(fakeOS.exitCalled).To(Equal(1))
Expect(fakeOS.exitCalledWithCode).To(Equal(1))
Expect(output).To(ContainSubstrings([]string{"Usage:", "cf", "delete-target", "NAME"}))
})
It("displays proper first time message", func() {
output := CaptureOutput(func() {
targetsPlugin.Run(fakeCliConnection, []string{"targets"})
})
Expect(fakeOS.exitCalled).To(Equal(0))
Expect(output).To(ContainSubstrings([]string{"No targets have been saved"}))
Expect(output).To(ContainSubstrings([]string{"cf", "save-target", "NAME"}))
})
})
Describe("Configuration File Manipulation", func() {
It("creates the proper target directory", func() {
Expect(fakeOS.mkdirCalled).To(Equal(1))
Expect(fakeOS.mkdirCalledWithPath).To(HaveSuffix("/.cf/targets"))
})
It("properly saves first target", func() {
targetsPlugin.Run(fakeCliConnection, []string{"save-target", "first"})
Expect(fakeOS.exitCalled).To(Equal(0))
Expect(fakeOS.writefileCalled).To(Equal(1))
Expect(fakeOS.writefileCalledWithPath).To(HaveSuffix("/.cf/targets/first.config.json"))
Expect(fakeOS.symlinkCalled).To(Equal(1))
Expect(fakeOS.symlinkCalledWithSource).To(HaveSuffix("/.cf/targets/current"))
Expect(fakeOS.symlinkCalledWithTarget).To(HaveSuffix("/.cf/targets/first.config.json"))
})
It("properly saves second target", func() {
targetsPlugin.Run(fakeCliConnection, []string{"save-target", "first"})
targetsPlugin.Run(fakeCliConnection, []string{"save-target", "second"})
Expect(fakeOS.exitCalled).To(Equal(0))
Expect(fakeOS.writefileCalled).To(Equal(2))
Expect(fakeOS.writefileCalledWithPath).To(HaveSuffix("/.cf/targets/second.config.json"))
Expect(fakeOS.removeCalledWithPath).To(HaveSuffix("/.cf/targets/current"))
Expect(fakeOS.symlinkCalled).To(Equal(2))
Expect(fakeOS.symlinkCalledWithSource).To(HaveSuffix("/.cf/targets/current"))
Expect(fakeOS.symlinkCalledWithTarget).To(HaveSuffix("/.cf/targets/second.config.json"))
})
})
})