forked from maxbrunsfeld/counterfeiter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
273 lines (222 loc) · 6.91 KB
/
parser_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
package arguments_test
import (
"errors"
"os"
"path/filepath"
"testing"
"time"
locatorFakes "github.com/maxbrunsfeld/counterfeiter/locator/fakes"
terminalFakes "github.com/maxbrunsfeld/counterfeiter/terminal/fakes"
. "github.com/maxbrunsfeld/counterfeiter/arguments"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("parsing arguments", func() {
var subject ArgumentParser
var parsedArgs ParsedArguments
var args []string
var fail FailHandler
var cwd CurrentWorkingDir
var symlinkEvaler SymlinkEvaler
var fileStatReader FileStatReader
var ui *terminalFakes.FakeUI
var interfaceLocator *locatorFakes.FakeInterfaceLocator
var failWasCalled bool
// fake UI helper
var fakeUIBuffer = func() string {
var output string
for i := 0; i < ui.WriteLineCallCount(); i++ {
output = output + ui.WriteLineArgsForCall(i)
}
return output
}
JustBeforeEach(func() {
subject = NewArgumentParser(
fail,
cwd,
symlinkEvaler,
fileStatReader,
ui,
interfaceLocator,
)
parsedArgs = subject.ParseArguments(args...)
})
BeforeEach(func() {
failWasCalled = false
fail = func(_ string, _ ...interface{}) { failWasCalled = true }
cwd = func() string {
return "/home/test-user/workspace"
}
ui = new(terminalFakes.FakeUI)
interfaceLocator = new(locatorFakes.FakeInterfaceLocator)
symlinkEvaler = func(input string) (string, error) {
return input, nil
}
fileStatReader = func(filename string) (os.FileInfo, error) {
return fakeFileInfo(filename, true), nil
}
})
Describe("when a single argument is provided", func() {
BeforeEach(func() {
args = []string{"some/path"}
interfaceLocator.GetInterfacesFromFilePathReturns([]string{"Foo", "Bar"})
ui.ReadLineFromStdinReturns("1")
ui.TerminalIsTTYReturns(true)
})
Context("but the connecting terminal is not a TTY", func() {
BeforeEach(func() {
ui.TerminalIsTTYReturns(false)
})
It("should invoke the fail handler", func() {
Expect(failWasCalled).To(BeTrue())
})
})
It("prompts the user for which interface they want", func() {
Expect(fakeUIBuffer()).To(ContainSubstring("Which interface to counterfeit?"))
})
It("shows the user each interface found in the given filepath", func() {
Expect(fakeUIBuffer()).To(ContainSubstring("1. Foo"))
Expect(fakeUIBuffer()).To(ContainSubstring("2. Bar"))
})
It("asks its interface locator for valid interfaces", func() {
Expect(interfaceLocator.GetInterfacesFromFilePathCallCount()).To(Equal(1))
Expect(interfaceLocator.GetInterfacesFromFilePathArgsForCall(0)).To(Equal("/home/test-user/workspace/some/path"))
})
It("yields the interface name the user chose", func() {
Expect(parsedArgs.InterfaceName).To(Equal("Foo"))
})
Describe("when the user types an invalid option", func() {
BeforeEach(func() {
ui.ReadLineFromStdinReturns("garbage")
})
It("invokes its fail handler", func() {
Expect(failWasCalled).To(BeTrue())
})
})
})
Describe("when two arguments are provided", func() {
BeforeEach(func() {
args = []string{"some/path", "MySpecialInterface"}
})
It("indicates to not print to stdout", func() {
Expect(parsedArgs.PrintToStdOut).To(BeFalse())
})
It("provides a name for the fake implementing the interface", func() {
Expect(parsedArgs.FakeImplName).To(Equal("FakeMySpecialInterface"))
})
It("treats the second argument as the interface to counterfeit", func() {
Expect(parsedArgs.InterfaceName).To(Equal("MySpecialInterface"))
})
It("snake cases the filename for the output directory", func() {
Expect(parsedArgs.OutputPath).To(Equal(
filepath.Join(
parsedArgs.SourcePackageDir,
"fakes",
"fake_my_special_interface.go",
),
))
})
Describe("the source directory", func() {
It("should be an absolute path", func() {
Expect(filepath.IsAbs(parsedArgs.SourcePackageDir)).To(BeTrue())
})
Context("when the first arg is a path to a file", func() {
BeforeEach(func() {
fileStatReader = func(filename string) (os.FileInfo, error) {
return fakeFileInfo(filename, false), nil
}
})
It("should be the directory containing the file", func() {
Expect(parsedArgs.SourcePackageDir).ToNot(ContainSubstring("something.go"))
})
})
Context("when the file stat cannot be read", func() {
BeforeEach(func() {
fileStatReader = func(_ string) (os.FileInfo, error) {
return fakeFileInfo("", false), errors.New("submarine-shoutout")
}
})
It("should call its fail handler", func() {
Expect(failWasCalled).To(BeTrue())
})
})
})
})
Describe("when three arguments are provided", func() {
Context("and the third one is '-'", func() {
BeforeEach(func() {
args = []string{"some/path", "MySpecialInterface", "-"}
})
It("treats the second argument as the interface to counterfeit", func() {
Expect(parsedArgs.InterfaceName).To(Equal("MySpecialInterface"))
})
It("provides a name for the fake implementing the interface", func() {
Expect(parsedArgs.FakeImplName).To(Equal("FakeMySpecialInterface"))
})
It("indicates that the fake should be printed to stdout", func() {
Expect(parsedArgs.PrintToStdOut).To(BeTrue())
})
It("snake cases the filename for the output directory", func() {
Expect(parsedArgs.OutputPath).To(Equal(
filepath.Join(
parsedArgs.SourcePackageDir,
"fakes",
"fake_my_special_interface.go",
),
))
})
Describe("the source directory", func() {
It("should be an absolute path", func() {
Expect(filepath.IsAbs(parsedArgs.SourcePackageDir)).To(BeTrue())
})
Context("when the first arg is a path to a file", func() {
BeforeEach(func() {
fileStatReader = func(filename string) (os.FileInfo, error) {
return fakeFileInfo(filename, false), nil
}
})
It("should be the directory containing the file", func() {
Expect(parsedArgs.SourcePackageDir).ToNot(ContainSubstring("something.go"))
})
})
})
})
Context("and the third one is some random input", func() {
BeforeEach(func() {
args = []string{"some/path", "MySpecialInterface", "WHOOPS"}
})
It("indicates to not print to stdout", func() {
Expect(parsedArgs.PrintToStdOut).To(BeFalse())
})
})
})
})
func TestCounterfeiterCLI(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Argument Parser Suite")
}
func fakeFileInfo(filename string, isDir bool) os.FileInfo {
return testFileInfo{name: filename, isDir: isDir}
}
type testFileInfo struct {
name string
isDir bool
}
func (testFileInfo testFileInfo) Name() string {
return testFileInfo.name
}
func (testFileInfo testFileInfo) IsDir() bool {
return testFileInfo.isDir
}
func (testFileInfo testFileInfo) Size() int64 {
return 0
}
func (testFileInfo testFileInfo) Mode() os.FileMode {
return 0
}
func (testFileInfo testFileInfo) ModTime() time.Time {
return time.Now()
}
func (testFileInfo testFileInfo) Sys() interface{} {
return nil
}