-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpytest_ast_back_to_python.py
61 lines (43 loc) · 1.67 KB
/
pytest_ast_back_to_python.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
# -*- coding: utf-8 -*-
from __future__ import print_function
from _pytest.assertion.rewrite import rewrite_asserts
from _pytest.monkeypatch import MonkeyPatch
import codegen
def pytest_addoption(parser):
group = parser.getgroup('ast-back-to-python')
group.addoption(
'--show-ast-as-python',
action='store_true',
dest='ast_as_python',
default=False,
help='Show how assertion rewriting recoded the AST.'
)
def pytest_configure(config):
config._ast_as_python = AstAsPython()
config.pluginmanager.register(config._ast_as_python)
def make_replacement_rewrite_asserts(store):
def replacement_rewrite_asserts(mod, *args, **kwargs):
rewrite_asserts(mod, *args, **kwargs)
store.append(codegen.to_source(mod))
return replacement_rewrite_asserts
class AstAsPython(object):
def __init__(self):
self.store = []
def pytest_configure(self, config):
if not config.getoption('ast_as_python'):
return
mp = MonkeyPatch()
mp.setattr(
'_pytest.assertion.rewrite.rewrite_asserts',
make_replacement_rewrite_asserts(self.store))
# written pyc files will bypass our patch, so disable reading them
mp.setattr(
'_pytest.assertion.rewrite._read_pyc',
lambda source, pyc, trace=None: None)
config._cleanup.append(mp.undo)
def pytest_terminal_summary(self, terminalreporter):
if not terminalreporter.config.getoption('ast_as_python'):
return
for source in self.store:
terminalreporter._tw.sep("=", "Rewritten AST as Python")
terminalreporter.write(source)