14
14
from . import find_missing_reqs
15
15
16
16
17
+ @pytest .fixture
18
+ def fake_opts ():
19
+
20
+ class FakeOptParse :
21
+ class options :
22
+ paths = ['dummy' ]
23
+ verbose = True
24
+ version = False
25
+ ignore_files = []
26
+ ignore_mods = []
27
+ options = options ()
28
+ args = ['ham.py' ]
29
+
30
+ def __init__ (self , usage ):
31
+ pass
32
+
33
+ def add_option (* args , ** kw ):
34
+ pass
35
+
36
+ def parse_args (self ):
37
+ return (self .options , self .args )
38
+
39
+ return FakeOptParse
40
+
41
+
17
42
@pytest .mark .parametrize (["path" , "result" ], [
18
43
('/' , '' ),
19
44
('__init__.py' , '' ), # a top-level file like this has no package name
@@ -182,25 +207,8 @@ def test_find_missing_reqs(monkeypatch):
182
207
assert result == [('shrub' , [imported_modules ['shrub' ]])]
183
208
184
209
185
- def test_main_failure (monkeypatch , caplog ):
186
- class options :
187
- paths = ['dummy' ]
188
- verbose = True
189
- ignore_files = []
190
- ignore_mods = []
191
- options = options ()
192
-
193
- class FakeOptParse :
194
- def __init__ (self , usage ):
195
- pass
196
-
197
- def add_option (* args , ** kw ):
198
- pass
199
-
200
- def parse_args (self ):
201
- return (options , ['ham.py' ])
202
-
203
- monkeypatch .setattr (optparse , 'OptionParser' , FakeOptParse )
210
+ def test_main_failure (monkeypatch , caplog , fake_opts ):
211
+ monkeypatch .setattr (optparse , 'OptionParser' , fake_opts )
204
212
205
213
caplog .setLevel (logging .WARN )
206
214
@@ -214,32 +222,22 @@ def parse_args(self):
214
222
assert excinfo .value == 1
215
223
216
224
assert caplog .records ()[0 ].message == \
225
+ 'Missing requirements:'
226
+ assert caplog .records ()[1 ].message == \
217
227
'location.py:1 dist=missing module=missing'
218
228
219
229
220
- def test_main_no_spec (monkeypatch , caplog ):
221
- class FakeOptParse :
222
- def __init__ (self , usage ):
223
- pass
224
-
225
- def add_option (* args , ** kw ):
226
- pass
227
-
228
- def parse_args (self ):
229
- return (None , [])
230
-
231
- error = pretend .call_recorder (lambda * a : None )
232
-
233
- monkeypatch .setattr (optparse , 'OptionParser' , FakeOptParse )
234
- monkeypatch .setattr (find_missing_reqs , 'ignorer' ,
235
- pretend .call_recorder (lambda a : None ))
230
+ def test_main_no_spec (monkeypatch , caplog , fake_opts ):
231
+ fake_opts .args = []
232
+ monkeypatch .setattr (optparse , 'OptionParser' , fake_opts )
233
+ monkeypatch .setattr (fake_opts , 'error' ,
234
+ pretend .call_recorder (lambda s , e : None ), raising = False )
236
235
237
236
with pytest .raises (SystemExit ) as excinfo :
238
237
find_missing_reqs .main ()
239
238
assert excinfo .value == 2
240
239
241
- assert FakeOptParse .error .calls
242
- assert not find_missing_reqs .ignorer .calls
240
+ assert fake_opts .error .calls
243
241
244
242
245
243
@pytest .mark .parametrize (["ignore_cfg" , "candidate" , "result" ], [
@@ -267,6 +265,7 @@ def test_logging_config(monkeypatch, caplog, verbose_cfg, events, result):
267
265
class options :
268
266
paths = ['dummy' ]
269
267
verbose = verbose_cfg
268
+ version = False
270
269
ignore_files = []
271
270
ignore_mods = []
272
271
options = options ()
@@ -291,3 +290,12 @@ def parse_args(self):
291
290
292
291
messages = [r .message for r in caplog .records ()]
293
292
assert messages == result
293
+
294
+
295
+ def test_main_version (monkeypatch , caplog , fake_opts ):
296
+ fake_opts .options .version = True
297
+ monkeypatch .setattr (optparse , 'OptionParser' , fake_opts )
298
+
299
+ with pytest .raises (SystemExit ) as excinfo :
300
+ find_missing_reqs .main ()
301
+ assert excinfo .value == 'version'
0 commit comments