-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgob_test.go
334 lines (283 loc) · 9.15 KB
/
gob_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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package gobmock
import (
"io"
"log"
"os"
"github.com/mitchellh/go-homedir"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/progrium/go-basher"
)
var (
bash *basher.Context
stdout *gbytes.Buffer
stderr *gbytes.Buffer
bashPath string
)
var _ = Describe("Integration", func() {
BeforeSuite(func() {
extractBash()
})
AfterSuite(func() {
os.Remove(bashPath)
})
BeforeEach(func() {
bash, _ = basher.NewContext(bashPath, true)
stdout = gbytes.NewBuffer()
stderr = gbytes.NewBuffer()
bash.Stdout = io.MultiWriter(GinkgoWriter, stdout)
bash.Stderr = io.MultiWriter(GinkgoWriter, stderr)
bash.SelfPath = "/bin/echo"
bash.CopyEnv()
})
exportedSubShellTest := `main_test() {
local sub_shell
sub_shell="$(mktemp)"
trap "rm '${sub_shell}'" EXIT
echo "#!${BASH}" > "${sub_shell}"
echo 'echo "My child should bring home $(curl some://nonsense > /dev/null 2>&1; echo $?) bad grades"' >> "${sub_shell}"
chmod +x "${sub_shell}"
"${sub_shell}"
}`
shallowSubShellTest := `main_test() {
local sub_shell
sub_shell="$(mktemp)"
trap "rm '${sub_shell}'" EXIT
echo "#!${BASH}" > "${sub_shell}"
echo 'echo "Look mom, we are bashing with $(file ${BASH})"' >> "${sub_shell}"
chmod +x "${sub_shell}"
"${sub_shell}"
file $(which echo)
}`
Context("Stub", func() {
It("stubs executables", func() {
gobs := []Gob{Stub("curl")}
ApplyMocks(bash, gobs)
status, err := bash.Run("curl", []string{"xyz://nothing.to.see.here"})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
})
It("works pith pipefail", func() {
sourceString(`set -o pipefail
main_test() {
echo "Yay!" | curl abs://urdly.namedurl
}`)
gobs := []Gob{Stub("curl")}
ApplyMocks(bash, gobs)
status, err := bash.Run("main_test", []string{})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
})
It("can be used in a child process", func() {
sourceString(exportedSubShellTest)
gobs := []Gob{Stub("curl")}
ApplyMocks(bash, gobs)
status, err := bash.Run("main_test", []string{})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
Expect(stdout).To(gbytes.Say("My child should bring home 0 bad grades"))
})
})
Context("Spy", func() {
It("stubs the executable", func() {
gobs := []Gob{Spy("curl")}
ApplyMocks(bash, gobs)
status, err := bash.Run("curl", []string{"xyz://nothing.to.see.here"})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
})
It("reports the arguments", func() {
gobs := []Gob{Spy("curl")}
ApplyMocks(bash, gobs)
bash.Run("curl", []string{"xyz://nothing.to.see.here"})
Expect(stderr).To(gbytes.Say("curl xyz://nothing.to.see.here"))
})
It("reports the number of the call", func() {
gobs := []Gob{Spy("curl")}
ApplyMocks(bash, gobs)
sourceString("test_main() { curl abc; curl cfg; curl dbc; }")
bash.Run("test_main", []string{""})
Expect(stderr).To(gbytes.Say("<1> curl abc"))
Expect(stderr).To(gbytes.Say("<2> curl cfg"))
Expect(stderr).To(gbytes.Say("<3> curl dbc"))
})
It("reports the standard input", func() {
sourceString(`
test_main() {
printf "Waves travel with malaria.\nThe self has samadhi\n" | curl
}`)
gobs := []Gob{Spy("curl")}
ApplyMocks(bash, gobs)
bash.Run("test_main", []string{})
Expect(stderr).To(gbytes.Say("<1 received> input:\n"))
Expect(stderr).To(gbytes.Say("Waves travel with malaria.\n"))
Expect(stderr).To(gbytes.Say("The self has samadhi"))
Expect(stderr).To(gbytes.Say("[1 end received]"))
})
It("is able to call through", func() {
sourceString(`
test_main() {
printf "One for all"
}
`)
gobs := []Gob{SpyAndCallThrough("printf")}
ApplyMocks(bash, gobs)
bash.Run("test_main", []string{})
Expect(stdout).To(gbytes.Say("One for all"))
Expect(stderr).To(gbytes.Say("<1> printf One for all"))
})
It("propagates error when calling through", func() {
gobs := []Gob{SpyAndCallThrough("false")}
ApplyMocks(bash, gobs)
exitCode, err := bash.Run("false", []string{})
Expect(err).ToNot(HaveOccurred())
Expect(exitCode).To(Equal(1))
})
It("is able to call through on a condition", func() {
sourceString(`
test_main() {
printf Cats
printf Dogz
}
`)
gobs := []Gob{SpyAndConditionallyCallThrough("printf", "[[ $1 =~ at ]]")}
ApplyMocks(bash, gobs)
bash.Run("test_main", []string{})
Expect(stdout).To(gbytes.Say("Cats"))
Expect(stdout).NotTo(gbytes.Say("Dogz"))
Expect(stderr).To(gbytes.Say("<1> printf Cats"))
Expect(stderr).To(gbytes.Say("<2> printf Dogz"))
})
It("pipes the input when calling through", func() {
sourceString(`
test_main() {
echo "Oranges and
lemons
say the bells
of St. Clement's" | grep "$1"
}
`)
gobs := []Gob{SpyAndCallThrough("grep")}
ApplyMocks(bash, gobs)
bash.Run("test_main", []string{"lemo"})
Expect(stderr).To(gbytes.Say("<1> grep lemo"))
Expect(stderr).To(gbytes.Say("<1 received"))
Expect(stdout).To(gbytes.Say("lemons"))
})
It("propagates error when piping input to called through command", func() {
sourceString(`
test_main() {
echo "Oranges and
lemons
say the bells
of St. Clement's" | grep "$1"
}
`)
gobs := []Gob{SpyAndCallThrough("grep")}
ApplyMocks(bash, gobs)
exitCode, err := bash.Run("test_main", []string{"apple"})
Expect(err).NotTo(HaveOccurred())
Expect(exitCode).To(Equal(1))
})
Context("shallow mocking", func() {
BeforeEach(func() {
sourceString(shallowSubShellTest)
})
It("is available for spies", func() {
ApplyMocks(bash, []Gob{ShallowSpy("file")})
status, _ := bash.Run("main_test", []string{})
Expect(status).To(Equal(0))
Expect(stderr).NotTo(gbytes.Say("file .*/bash"))
Expect(stderr).To(gbytes.Say("<1> file .*/echo"))
Expect(stdout).To(gbytes.Say("we are bashing with /\\w+"))
})
It("is available for call through spies", func() {
ApplyMocks(bash, []Gob{ShallowSpyAndCallThrough("file")})
status, _ := bash.Run("main_test", []string{})
Expect(status).To(Equal(0))
Expect(stderr).NotTo(gbytes.Say("file .*/bash"))
Expect(stderr).To(gbytes.Say("<1> file .*/echo"))
Expect(stdout).To(gbytes.Say("we are bashing with /\\w+"))
})
It("is available for conditionally call through spies", func() {
ApplyMocks(bash, []Gob{ShallowSpyAndConditionallyCallThrough("file", "[[ 1 -eq 1 ]]")})
status, _ := bash.Run("main_test", []string{})
Expect(status).To(Equal(0))
Expect(stderr).NotTo(gbytes.Say("file .*/bash"))
Expect(stderr).To(gbytes.Say("<1> file .*/echo"))
Expect(stdout).To(gbytes.Say("we are bashing with /\\w+"))
})
})
})
Context("Mock", func() {
It("stubs the executable", func() {
gobs := []Gob{Mock("curl", "")}
ApplyMocks(bash, gobs)
status, err := bash.Run("curl", []string{"zabi://daba.dooooo"})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
})
It("can simulate the return code", func() {
gobs := []Gob{Mock("curl", "return 1")}
ApplyMocks(bash, gobs)
status, err := bash.Run("curl", []string{"https://google.ie"})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(1))
})
It("produces predefined output", func() {
gobs := []Gob{Mock("curl", "echo 'Such much wow'")}
ApplyMocks(bash, gobs)
status, err := bash.Run("curl", []string{"boop://dodge.for.prezident"})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
Expect(stdout).To(gbytes.Say("Such much wow"))
})
It("can be exported to child processes", func() {
sourceString(exportedSubShellTest)
gobs := []Gob{Mock("curl", "")}
ApplyMocks(bash, gobs)
status, err := bash.Run("main_test", []string{})
Expect(err).NotTo(HaveOccurred())
Expect(status).To(Equal(0))
Expect(stdout).To(gbytes.Say("My child should bring home 0 bad grades"))
})
It("is able to call through", func() {
sourceString(`test_main() { printf "monkey"; printf "honey"; }`)
gobs := []Gob{MockOrCallThrough("printf", "echo berries", "[ $1 == 'monkey' ]")}
ApplyMocks(bash, gobs)
bash.Run("test_main", []string{})
Expect(stderr).To(gbytes.Say("<1> printf monkey"))
Expect(stderr).To(gbytes.Say("<2> printf honey"))
Expect(stdout).To(gbytes.Say("monkey"))
Expect(stdout).NotTo(gbytes.Say("honey"))
Expect(string(stdout.Contents())).To(ContainSubstring("berries"))
})
It("propagates a subshell error when calling through", func() {
sourceString(`test_main() { me=$(ls /bogus); }`)
gobs := []Gob{MockOrCallThrough("ls", "echo wild", "[ 1 -eq 1 ]")}
ApplyMocks(bash, gobs)
status, err := bash.Run("test_main", []string{})
Expect(err).ToNot(HaveOccurred())
Expect(status).To(Equal(1))
})
})
})
func sourceString(script string) {
bash.Source("", func(string) ([]byte, error) {
return []byte(script), nil
})
}
func extractBash() {
bashDir, err := homedir.Expand("~/.basher")
if err != nil {
log.Fatal(err, "1")
}
bashPath = bashDir + "/bash"
if _, err := os.Stat(bashPath); os.IsNotExist(err) {
err = basher.RestoreAsset(bashDir, "bash")
if err != nil {
log.Fatal(err, "1")
}
}
}