-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimpall.py
executable file
·380 lines (283 loc) · 11 KB
/
impall.py
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env python3
"""
# 🛎 Test-import all modules 🛎
Individually and separately imports each Python module or file in a project and
reports warnings or failures at the end.
### Running impall as a unit test
Just inherit from the base class and it will
automatically find and import each file, like this.
import impall
class ImpAllTest(impall.ImpAllTest):
pass
(You can copy
[this file](https://github.com/rec/impall/blob/master/all_test.py)
into your project if you like.)
Tests are customized by overriding one of these following properties in the
derived class.
CLEAR_SYS_MODULES, EXCLUDE, FAILING, INCLUDE, MODULES, PATHS,
RAISE_EXCEPTIONS, and WARNINGS_ACTION.
For example, to turn warnings into errors, set the property
WARNINGS_ACTION in the derived class definition, like this.
class ImpAllTest(impall.ImpAllTest):
WARNINGS_ACTION = 'error'
## Running impall as a command-line utility
$ impall.py --warnings_action=error
$ impall.py -w error
The properties INCLUDE, EXCLUDE, and PROJECT_PATH can be
lists of strings, or a string separated with colons like
'foo.mod1:foo.mod2'
INCLUDE and EXCLUDE match modules, and also allow * as a wildcard.
A single * matches any module segment, and a double ** matches any
remaining segments. For example,
INCLUDE = 'foo', 'bar.*', 'baz.**'
* matches `foo` but not `foo.foo`
* matches `bar.foo` but not `bar` or `bar.foo.bar`
* matches `baz.foo` as well as `baz.foo.bar` but not `baz`
### A note on side-effects
to reduce side-effects, `sys.modules` is restored to its original
condition after each import if CLEAR_SYS_MODULES is true, but there might be
other side-effects from loading some specific module.
Use the EXCLUDE property to exclude modules with undesirable side
effects. In general, it is probably a bad idea to have significant
side-effects just from loading a module.
"""
import argparse
import fnmatch
import functools
import importlib
import os
import sys
import traceback
import typing as t
import unittest
import warnings
from types import ModuleType
__all__ = 'ImpAllTest', 'path_to_import'
CLEAR_SYS_MODULES = """
If `CLEAR_SYS_MODULES` IS `True`, the default, `sys.modules` is reset after each import.
This takes more time but finds more problems.
"""
EXCLUDE = """
A list of modules that will not be imported at all."""
FAILING = """
A list of modules that must fail.
This differs from EXCLUDE because modules in EXCLUDE aren't imported at
all, but failing modules must exist, are imported, and then must fail
when imported."""
INCLUDE = """
If non-empty, exactly the modules in the list will be loaded.
This is not recursive - you need to list each module you want to include."""
MODULES = """
If MODULES is False, search all subdirectories.
If MODULES is True, the default, do not search subdirectories that do not contain an
__init__.py file.
"""
PATHS = """
A list of paths to search from.
If empty, guess the project paths from the current directory."""
RAISE_EXCEPTIONS = """
If True, stop importing at the first exception and print a stack trace.
If False, the default, all exceptions will be caught and reported on at the end."""
WARNINGS_ACTION = """
Possible choices are: default, error, ignore, always, module, once
`warnings.simplefilter` is set to this value while testing: see
https://docs.python.org/3/library/warnings.html#the-warnings-filter
for more details."""
class ImpAllTest(unittest.TestCase):
CLEAR_SYS_MODULES = True
EXCLUDE = None
FAILING = ()
INCLUDE = None
MODULES = True
PATHS = None
RAISE_EXCEPTIONS = False
WARNINGS_ACTION = 'default'
@functools.cached_property
def _exc(self) -> t.Callable[[t.Any], bool]:
return _split_pattern(self.EXCLUDE or ())
@functools.cached_property
def _inc(self) -> t.Callable[[t.Any], bool]:
if self.INCLUDE is None:
return lambda s: True
else:
return _split_pattern(self.INCLUDE)
def test_all(self) -> None:
successes, failures = self.impall()
self.assertTrue(successes or failures, 'No tests were found')
expected = sorted(_split(self.FAILING))
failed = [(m, ex) for m, ex in failures if m not in expected]
failed_to_fail = [m for m in successes if m in expected]
if failed_to_fail:
print('Didn\'t fail when expected:', *sorted(failed_to_fail))
first = True
for module, ex in failed:
if first:
first = False
else:
print()
print(module + ':')
for line in ex.splitlines():
if 'File "<' not in line:
print(' ', line)
self.assertTrue(not failed, 'Some tests failed')
self.assertTrue(not failed_to_fail, 'Some tests failed to fail')
def impall(self) -> t.Tuple[t.List[str], t.List[t.Tuple[str, str]]]:
successes: t.List[str] = []
failures: t.List[t.Tuple[str, str]] = []
paths = _split(self.PATHS or path_to_import(os.getcwd())[0])
warnings.simplefilter(self.WARNINGS_ACTION) # type: ignore[arg-type]
for file in self._all_imports(paths):
self._import(file, successes, failures)
warnings.filters.pop(0) # type: ignore[attr-defined]
return successes, failures
def _all_imports(self, paths: t.Sequence[str]) -> t.Iterator[str]:
for path in paths:
for directory, sub_dirs, files in os.walk(path):
if directory != path and not self._accept_dir(directory):
sub_dirs.clear()
continue
if _is_python_dir(directory):
yield directory
for f in files:
if f.endswith('.py') and not _is_ignored(f):
yield os.path.join(directory, f)
def _import(self, file: str, successes: t.List[str], failures: t.List[t.Tuple[str, str]]) -> None:
root, module = path_to_import(file)
path = file[:-3] if file.endswith('.py') else file
rel = os.path.relpath(path, os.getcwd())
if not self._inc(rel) or self._exc(rel):
return
importlib.invalidate_caches()
file_path = os.path.relpath(file, os.getcwd())
if self.CLEAR_SYS_MODULES:
saved_modules = dict(sys.modules)
saved_path = sys.path[:]
sys.path.insert(0, root)
try:
importlib.import_module(module)
except Exception:
if self.RAISE_EXCEPTIONS:
raise
failures.append((file_path, traceback.format_exc()))
else:
successes.append(file_path)
finally:
if self.CLEAR_SYS_MODULES:
for k in set(sys.modules).difference(saved_modules):
del sys.modules[k]
sys.modules.update(saved_modules)
sys.path[:] = saved_path
def _accept_dir(self, directory: str) -> bool:
if self.MODULES:
return _is_python_dir(directory)
return not _is_ignored(directory)
@functools.lru_cache()
def path_to_import(path: str) -> t.Tuple[str, str]:
"""
Return a (path, module) pair that allows you to import the Python file or
directory at location path
"""
parts = []
if not os.path.exists(path):
raise FileNotFoundError(path)
path = str(path) # Might be a Path
if path.endswith('.py'):
path = path[:-3]
def isdir(p: str) -> bool:
return os.path.isdir(p) and not os.path.exists(p + '.py')
while not isdir(path) or _is_python_dir(path):
path, part = os.path.split(path)
if not part:
if path:
parts.append(path)
break
parts.append(part)
return path, '.'.join(reversed(parts))
def import_file(path: str) -> ModuleType:
"""
Given a path to a file or directory, imports it from the correct root
and returns the module
"""
root, module_path = path_to_import(path)
old_path = sys.path[:]
sys.path.insert(0, root or '.')
try:
return importlib.import_module(module_path)
finally:
sys.path[:] = old_path
_PROPERTIES = set(dir(ImpAllTest)) - set(dir(unittest.TestCase))
PROPERTIES = sorted(a for a in _PROPERTIES if a.isupper())
ENV_SEPARATOR = ':'
_NO = 'NO_'
def _is_ignored(path: str) -> bool:
b = os.path.basename(path)
return b.startswith('.') or (
b.startswith('__') and os.path.isdir(path) or b == '__init__.py'
)
def _is_python_dir(path: str) -> bool:
"""Return True if `path` is a directory containing an __init__.py file"""
init = os.path.join(path, '__init__.py')
return os.path.exists(init) and not _is_ignored(path)
def _split(s: t.Union[str, t.Sequence[str]]) -> t.Sequence[str]:
if not s:
return []
if isinstance(s, str):
return s.split(':')
return s
def _split_pattern(s: t.Union[str, t.Sequence[str]]) -> t.Callable[[t.Any], bool]:
parts = _split(s)
return lambda x: any(fnmatch.fnmatch(x, p) for p in parts)
def report() -> None:
"""Test all files in a directory from the command line"""
args = _parse_args()
test_case = ImpAllTest()
for attr, value in vars(args).items():
attr = attr.upper()
if attr.startswith(_NO):
attr = attr[len(_NO) :]
value = not value
default = getattr(test_case, attr, _NO)
if default is not _NO and (isinstance(value, bool) or value):
if isinstance(default, (list, tuple)) and isinstance(value, str):
value = value.split(ENV_SEPARATOR)
setattr(test_case, attr, value)
successes, failures = test_case.impall()
if successes:
print('Successes', *successes, sep='\n ')
print()
if failures:
fail = ['%s (%s)' % (m, e) for (m, e) in failures]
print('Failures', *fail, sep='\n ', file=sys.stderr)
print(file=sys.stderr)
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=_USAGE)
parser.add_argument('paths', nargs='*', default=[os.getcwd()])
kwds: t.Dict[str, t.Any]
for prop in PROPERTIES:
default = getattr(ImpAllTest, prop)
help = globals()[prop]
if isinstance(default, bool):
kwds = {'action': 'store_true'}
if default:
prop = _NO + prop
elif isinstance(default, (tuple, list)):
kwds = {'default': ':'.join(default)}
else:
kwds = {'default': default}
short, long = '-' + prop[0], '--' + prop
if short == '-N':
parser.add_argument(long, help=help, **kwds)
else:
parser.add_argument(short, long, help=help, **kwds)
return parser.parse_args()
_USAGE = """
impall.py [path ...path]
Individually and separately imports each Python file found on or below these
paths and reports on any failures.
With no arguments, impall imports all Python files found in
any Python directory (i.e. with a __init__.py file) below the current
directory.
"""
if __name__ == '__main__':
report()