Skip to content

Commit 9268b1d

Browse files
committed
Add in tests for find_extra_reqs
1 parent 7462fd1 commit 9268b1d

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

tests/test_find_extra_reqs.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
from __future__ import absolute_import
2+
3+
import collections
4+
import logging
5+
import optparse
6+
7+
import pytest
8+
import pretend
9+
10+
from pip_check_reqs import find_extra_reqs, common
11+
12+
13+
@pytest.fixture
14+
def fake_opts():
15+
16+
class FakeOptParse:
17+
class options:
18+
paths = ['dummy']
19+
verbose = False
20+
debug = False
21+
version = False
22+
ignore_files = []
23+
ignore_mods = []
24+
options = options()
25+
args = ['ham.py']
26+
27+
def __init__(self, usage):
28+
pass
29+
30+
def add_option(*args, **kw):
31+
pass
32+
33+
def parse_args(self):
34+
return (self.options, self.args)
35+
36+
return FakeOptParse
37+
38+
39+
def test_find_extra_reqs(monkeypatch):
40+
imported_modules = dict(
41+
spam=common.FoundModule('spam', 'site-spam/spam.py',
42+
[('ham.py', 1)]),
43+
shrub=common.FoundModule('shrub', 'site-spam/shrub.py',
44+
[('ham.py', 3)]),
45+
ignore=common.FoundModule('ignore', 'ignore.py',
46+
[('ham.py', 2)])
47+
)
48+
monkeypatch.setattr(common, 'find_imported_modules',
49+
pretend.call_recorder(lambda a: imported_modules))
50+
51+
FakeDist = collections.namedtuple('FakeDist', ['project_name'])
52+
installed_distributions = map(FakeDist, ['spam', 'pass'])
53+
monkeypatch.setattr(find_extra_reqs, 'get_installed_distributions',
54+
pretend.call_recorder(lambda: installed_distributions))
55+
packages_info = [
56+
dict(name='spam', location='site-spam', files=['spam/__init__.py',
57+
'spam/shrub.py']),
58+
dict(name='shrub', location='site-spam', files=['shrub.py']),
59+
dict(name='pass', location='site-spam', files=['pass.py']),
60+
]
61+
62+
monkeypatch.setattr(find_extra_reqs, 'search_packages_info',
63+
pretend.call_recorder(lambda x: packages_info))
64+
65+
FakeReq = collections.namedtuple('FakeReq', ['name'])
66+
requirements = [FakeReq('foobar')]
67+
monkeypatch.setattr(find_extra_reqs, 'parse_requirements',
68+
pretend.call_recorder(lambda a, session=None: requirements))
69+
70+
result = find_extra_reqs.find_extra_reqs(None)
71+
assert result == ['foobar']
72+
73+
74+
def test_main_failure(monkeypatch, caplog, fake_opts):
75+
monkeypatch.setattr(optparse, 'OptionParser', fake_opts)
76+
77+
caplog.setLevel(logging.WARN)
78+
79+
monkeypatch.setattr(find_extra_reqs, 'find_extra_reqs', lambda x: [
80+
'extra'
81+
])
82+
83+
with pytest.raises(SystemExit) as excinfo:
84+
find_extra_reqs.main()
85+
assert excinfo.value == 1
86+
87+
assert caplog.records()[0].message == \
88+
'Extra requirements:'
89+
assert caplog.records()[1].message == \
90+
'extra in requirements.txt'
91+
92+
93+
def test_main_no_spec(monkeypatch, caplog, fake_opts):
94+
fake_opts.args = []
95+
monkeypatch.setattr(optparse, 'OptionParser', fake_opts)
96+
monkeypatch.setattr(fake_opts, 'error',
97+
pretend.call_recorder(lambda s, e: None), raising=False)
98+
99+
with pytest.raises(SystemExit) as excinfo:
100+
find_extra_reqs.main()
101+
assert excinfo.value == 2
102+
103+
assert fake_opts.error.calls
104+
105+
106+
@pytest.mark.parametrize(["verbose_cfg", "debug_cfg", "result"], [
107+
(False, False, ['warn']),
108+
(True, False, ['info', 'warn']),
109+
(False, True, ['debug', 'info', 'warn']),
110+
(True, True, ['debug', 'info', 'warn']),
111+
])
112+
def test_logging_config(monkeypatch, caplog, verbose_cfg, debug_cfg, result):
113+
class options:
114+
paths = ['dummy']
115+
verbose = verbose_cfg
116+
debug = debug_cfg
117+
version = False
118+
ignore_files = []
119+
ignore_mods = []
120+
options = options()
121+
122+
class FakeOptParse:
123+
def __init__(self, usage):
124+
pass
125+
126+
def add_option(*args, **kw):
127+
pass
128+
129+
def parse_args(self):
130+
return (options, ['ham.py'])
131+
132+
monkeypatch.setattr(optparse, 'OptionParser', FakeOptParse)
133+
134+
monkeypatch.setattr(find_extra_reqs, 'find_extra_reqs', lambda x: [])
135+
find_extra_reqs.main()
136+
137+
for event in [(logging.DEBUG, 'debug'), (logging.INFO, 'info'),
138+
(logging.WARN, 'warn')]:
139+
find_extra_reqs.log.log(*event)
140+
141+
messages = [r.message for r in caplog.records()]
142+
# first message is always the usage message
143+
if verbose_cfg or debug_cfg:
144+
assert messages[1:] == result
145+
else:
146+
assert messages == result
147+
148+
149+
def test_main_version(monkeypatch, caplog, fake_opts):
150+
fake_opts.options.version = True
151+
monkeypatch.setattr(optparse, 'OptionParser', fake_opts)
152+
153+
with pytest.raises(SystemExit) as excinfo:
154+
find_extra_reqs.main()
155+
assert excinfo.value == 'version'

0 commit comments

Comments
 (0)