|
1 | 1 | from __future__ import absolute_import
|
2 | 2 |
|
3 |
| -import ast |
4 | 3 | import collections
|
5 | 4 | import logging
|
6 | 5 | import optparse
|
7 |
| -import os |
8 |
| -import os.path |
9 |
| -import sys |
10 | 6 |
|
11 | 7 | import pytest
|
12 | 8 | import pretend
|
@@ -40,139 +36,6 @@ def parse_args(self):
|
40 | 36 | return FakeOptParse
|
41 | 37 |
|
42 | 38 |
|
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 |
| - |
176 | 39 | def test_find_missing_reqs(monkeypatch):
|
177 | 40 | imported_modules = dict(
|
178 | 41 | spam=common.FoundModule('spam', 'site-spam/spam.py',
|
@@ -241,23 +104,6 @@ def test_main_no_spec(monkeypatch, caplog, fake_opts):
|
241 | 104 | assert fake_opts.error.calls
|
242 | 105 |
|
243 | 106 |
|
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 |
| - |
261 | 107 | @pytest.mark.parametrize(["verbose_cfg", "debug_cfg", "result"], [
|
262 | 108 | (False, False, ['warn']),
|
263 | 109 | (True, False, ['info', 'warn']),
|
|
0 commit comments