Skip to content

Commit d797afc

Browse files
committed
Separate out common tests
1 parent fb26d4b commit d797afc

File tree

2 files changed

+161
-154
lines changed

2 files changed

+161
-154
lines changed

tests/test_common.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
from __future__ import absolute_import
2+
3+
import ast
4+
import logging
5+
import os.path
6+
import sys
7+
8+
import pytest
9+
import pretend
10+
11+
from pip_check_reqs import common
12+
13+
14+
@pytest.mark.parametrize(["path", "result"], [
15+
('/', ''),
16+
('__init__.py', ''), # a top-level file like this has no package name
17+
('/__init__.py', ''), # no package name
18+
('spam/__init__.py', 'spam'),
19+
('spam/__init__.pyc', 'spam'),
20+
('spam/__init__.pyo', 'spam'),
21+
('ham/spam/__init__.py', 'ham/spam'),
22+
('/ham/spam/__init__.py', '/ham/spam'),
23+
])
24+
def test_is_package_file(path, result):
25+
assert common.is_package_file(path) == result
26+
27+
28+
def test_FoundModule():
29+
fm = common.FoundModule('spam', 'ham')
30+
assert fm.modname == 'spam'
31+
assert fm.filename == os.path.realpath('ham')
32+
assert fm.locations == []
33+
assert str(fm) == 'FoundModule("spam")'
34+
35+
36+
@pytest.mark.parametrize(["stmt", "result"], [
37+
('import ast', ['ast']),
38+
('import ast, sys', ['ast', 'sys']),
39+
('from sys import version', ['sys']),
40+
('from os import path', ['os']),
41+
('import distutils.command.check', ['distutils']),
42+
('import spam', []), # don't break because bad programmer
43+
])
44+
def test_ImportVisitor(stmt, result):
45+
class options:
46+
def ignore_mods(self, modname):
47+
return False
48+
vis = common.ImportVisitor(options())
49+
vis.set_location('spam.py')
50+
vis.visit(ast.parse(stmt))
51+
result = vis.finalise()
52+
assert set(result.keys()) == set(result)
53+
54+
55+
def test_pyfiles_file(monkeypatch):
56+
monkeypatch.setattr(os.path, 'abspath',
57+
pretend.call_recorder(lambda x: '/spam/ham.py'))
58+
59+
assert list(common.pyfiles('spam')) == ['/spam/ham.py']
60+
61+
62+
def test_pyfiles_file_no_dice(monkeypatch):
63+
monkeypatch.setattr(os.path, 'abspath',
64+
pretend.call_recorder(lambda x: '/spam/ham'))
65+
66+
with pytest.raises(ValueError):
67+
list(common.pyfiles('spam'))
68+
69+
70+
def test_pyfiles_package(monkeypatch):
71+
monkeypatch.setattr(os.path, 'abspath',
72+
pretend.call_recorder(lambda x: '/spam'))
73+
monkeypatch.setattr(os.path, 'isdir',
74+
pretend.call_recorder(lambda x: True))
75+
walk_results = [
76+
('spam', [], ['__init__.py', 'spam', 'ham.py']),
77+
('spam/dub', [], ['bass.py', 'dropped']),
78+
]
79+
monkeypatch.setattr(os, 'walk',
80+
pretend.call_recorder(lambda x: walk_results))
81+
82+
assert list(common.pyfiles('spam')) == \
83+
['spam/__init__.py', 'spam/ham.py', 'spam/dub/bass.py']
84+
85+
86+
@pytest.mark.parametrize(["ignore_ham", "ignore_hashlib", "expect", "locs"], [
87+
(False, False, ['ast', 'os', 'hashlib'], [('spam.py', 2), ('ham.py', 2)]),
88+
(False, True, ['ast', 'os'], [('spam.py', 2), ('ham.py', 2)]),
89+
(True, False, ['ast'], [('spam.py', 2)]),
90+
(True, True, ['ast'], [('spam.py', 2)]),
91+
])
92+
def test_find_imported_modules(monkeypatch, caplog, ignore_ham, ignore_hashlib,
93+
expect, locs):
94+
monkeypatch.setattr(common, 'pyfiles',
95+
pretend.call_recorder(lambda x: ['spam.py', 'ham.py']))
96+
97+
if sys.version_info[0] == 2:
98+
# py2 will find sys module but py3k won't
99+
expect.append('sys')
100+
101+
class FakeFile():
102+
contents = [
103+
'from os import path\nimport ast, hashlib',
104+
'from __future__ import braces\nimport ast, sys\n'
105+
'from . import friend',
106+
]
107+
108+
def __init__(self, filename):
109+
pass
110+
111+
def read(self):
112+
return self.contents.pop()
113+
114+
def __enter__(self):
115+
return self
116+
117+
def __exit__(self, *args):
118+
pass
119+
monkeypatch.setattr(common, 'open', FakeFile, raising=False)
120+
121+
caplog.setLevel(logging.INFO)
122+
123+
class options:
124+
paths = ['dummy']
125+
verbose = True
126+
127+
@staticmethod
128+
def ignore_files(path):
129+
if path == 'ham.py' and ignore_ham:
130+
return True
131+
return False
132+
133+
@staticmethod
134+
def ignore_mods(module):
135+
if module == 'hashlib' and ignore_hashlib:
136+
return True
137+
return False
138+
139+
result = common.find_imported_modules(options)
140+
assert set(result) == set(expect)
141+
assert result['ast'].locations == locs
142+
143+
if ignore_ham:
144+
assert caplog.records()[0].message == 'ignoring: ham.py'
145+
146+
147+
@pytest.mark.parametrize(["ignore_cfg", "candidate", "result"], [
148+
([], 'spam', False),
149+
([], 'ham', False),
150+
(['spam'], 'spam', True),
151+
(['spam'], 'spam.ham', False),
152+
(['spam'], 'eggs', False),
153+
(['spam*'], 'spam', True),
154+
(['spam*'], 'spam.ham', True),
155+
(['spam*'], 'eggs', False),
156+
(['spam'], '/spam', True),
157+
])
158+
def test_ignorer(monkeypatch, ignore_cfg, candidate, result):
159+
monkeypatch.setattr(os.path, 'relpath', lambda s: s.lstrip('/'))
160+
ignorer = common.ignorer(ignore_cfg)
161+
assert ignorer(candidate) == result

tests/test_find_missing_reqs.py

Lines changed: 0 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
from __future__ import absolute_import
22

3-
import ast
43
import collections
54
import logging
65
import optparse
7-
import os
8-
import os.path
9-
import sys
106

117
import pytest
128
import pretend
@@ -40,139 +36,6 @@ def parse_args(self):
4036
return FakeOptParse
4137

4238

43-
@pytest.mark.parametrize(["path", "result"], [
44-
('/', ''),
45-
('__init__.py', ''), # a top-level file like this has no package name
46-
('/__init__.py', ''), # no package name
47-
('spam/__init__.py', 'spam'),
48-
('spam/__init__.pyc', 'spam'),
49-
('spam/__init__.pyo', 'spam'),
50-
('ham/spam/__init__.py', 'ham/spam'),
51-
('/ham/spam/__init__.py', '/ham/spam'),
52-
])
53-
def test_is_package_file(path, result):
54-
assert common.is_package_file(path) == result
55-
56-
57-
def test_FoundModule():
58-
fm = common.FoundModule('spam', 'ham')
59-
assert fm.modname == 'spam'
60-
assert fm.filename == os.path.realpath('ham')
61-
assert fm.locations == []
62-
assert str(fm) == 'FoundModule("spam")'
63-
64-
65-
@pytest.mark.parametrize(["stmt", "result"], [
66-
('import ast', ['ast']),
67-
('import ast, sys', ['ast', 'sys']),
68-
('from sys import version', ['sys']),
69-
('from os import path', ['os']),
70-
('import distutils.command.check', ['distutils']),
71-
('import spam', []), # don't break because bad programmer
72-
])
73-
def test_ImportVisitor(stmt, result):
74-
class options:
75-
def ignore_mods(self, modname):
76-
return False
77-
vis = common.ImportVisitor(options())
78-
vis.set_location('spam.py')
79-
vis.visit(ast.parse(stmt))
80-
result = vis.finalise()
81-
assert set(result.keys()) == set(result)
82-
83-
84-
def test_pyfiles_file(monkeypatch):
85-
monkeypatch.setattr(os.path, 'abspath',
86-
pretend.call_recorder(lambda x: '/spam/ham.py'))
87-
88-
assert list(common.pyfiles('spam')) == ['/spam/ham.py']
89-
90-
91-
def test_pyfiles_file_no_dice(monkeypatch):
92-
monkeypatch.setattr(os.path, 'abspath',
93-
pretend.call_recorder(lambda x: '/spam/ham'))
94-
95-
with pytest.raises(ValueError):
96-
list(common.pyfiles('spam'))
97-
98-
99-
def test_pyfiles_package(monkeypatch):
100-
monkeypatch.setattr(os.path, 'abspath',
101-
pretend.call_recorder(lambda x: '/spam'))
102-
monkeypatch.setattr(os.path, 'isdir',
103-
pretend.call_recorder(lambda x: True))
104-
walk_results = [
105-
('spam', [], ['__init__.py', 'spam', 'ham.py']),
106-
('spam/dub', [], ['bass.py', 'dropped']),
107-
]
108-
monkeypatch.setattr(os, 'walk',
109-
pretend.call_recorder(lambda x: walk_results))
110-
111-
assert list(common.pyfiles('spam')) == \
112-
['spam/__init__.py', 'spam/ham.py', 'spam/dub/bass.py']
113-
114-
115-
@pytest.mark.parametrize(["ignore_ham", "ignore_hashlib", "expect", "locs"], [
116-
(False, False, ['ast', 'os', 'hashlib'], [('spam.py', 2), ('ham.py', 2)]),
117-
(False, True, ['ast', 'os'], [('spam.py', 2), ('ham.py', 2)]),
118-
(True, False, ['ast'], [('spam.py', 2)]),
119-
(True, True, ['ast'], [('spam.py', 2)]),
120-
])
121-
def test_find_imported_modules(monkeypatch, caplog, ignore_ham, ignore_hashlib,
122-
expect, locs):
123-
monkeypatch.setattr(common, 'pyfiles',
124-
pretend.call_recorder(lambda x: ['spam.py', 'ham.py']))
125-
126-
if sys.version_info[0] == 2:
127-
# py2 will find sys module but py3k won't
128-
expect.append('sys')
129-
130-
class FakeFile():
131-
contents = [
132-
'from os import path\nimport ast, hashlib',
133-
'from __future__ import braces\nimport ast, sys\n'
134-
'from . import friend',
135-
]
136-
137-
def __init__(self, filename):
138-
pass
139-
140-
def read(self):
141-
return self.contents.pop()
142-
143-
def __enter__(self):
144-
return self
145-
146-
def __exit__(self, *args):
147-
pass
148-
monkeypatch.setattr(common, 'open', FakeFile, raising=False)
149-
150-
caplog.setLevel(logging.INFO)
151-
152-
class options:
153-
paths = ['dummy']
154-
verbose = True
155-
156-
@staticmethod
157-
def ignore_files(path):
158-
if path == 'ham.py' and ignore_ham:
159-
return True
160-
return False
161-
162-
@staticmethod
163-
def ignore_mods(module):
164-
if module == 'hashlib' and ignore_hashlib:
165-
return True
166-
return False
167-
168-
result = common.find_imported_modules(options)
169-
assert set(result) == set(expect)
170-
assert result['ast'].locations == locs
171-
172-
if ignore_ham:
173-
assert caplog.records()[0].message == 'ignoring: ham.py'
174-
175-
17639
def test_find_missing_reqs(monkeypatch):
17740
imported_modules = dict(
17841
spam=common.FoundModule('spam', 'site-spam/spam.py',
@@ -241,23 +104,6 @@ def test_main_no_spec(monkeypatch, caplog, fake_opts):
241104
assert fake_opts.error.calls
242105

243106

244-
@pytest.mark.parametrize(["ignore_cfg", "candidate", "result"], [
245-
([], 'spam', False),
246-
([], 'ham', False),
247-
(['spam'], 'spam', True),
248-
(['spam'], 'spam.ham', False),
249-
(['spam'], 'eggs', False),
250-
(['spam*'], 'spam', True),
251-
(['spam*'], 'spam.ham', True),
252-
(['spam*'], 'eggs', False),
253-
(['spam'], '/spam', True),
254-
])
255-
def test_ignorer(monkeypatch, ignore_cfg, candidate, result):
256-
monkeypatch.setattr(os.path, 'relpath', lambda s: s.lstrip('/'))
257-
ignorer = common.ignorer(ignore_cfg)
258-
assert ignorer(candidate) == result
259-
260-
261107
@pytest.mark.parametrize(["verbose_cfg", "debug_cfg", "result"], [
262108
(False, False, ['warn']),
263109
(True, False, ['info', 'warn']),

0 commit comments

Comments
 (0)