forked from zedr/clean-code-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
57 lines (42 loc) · 1.41 KB
/
conftest.py
File metadata and controls
57 lines (42 loc) · 1.41 KB
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
# content of conftest.py
import datetime
import re
import typing
import pytest
code_rxp = re.compile('```python(.*?)```', re.DOTALL | re.MULTILINE)
class FakeTimeModule:
def sleep(self, seconds):
pass
def fake_print(*args, **kwargs):
pass
def pytest_collect_file(parent, path):
if path.basename == "README.md":
return ReadmeFile.from_parent(parent, fspath=path)
class ReadmeFile(pytest.File):
def collect(self):
raw = self.fspath.open().read()
for idx, code in enumerate(code_rxp.findall(raw), 1):
yield ReadmeItem.from_parent(
self, name=str(idx), spec=code.strip()
)
class ReadmeItem(pytest.Item):
def __init__(self, name, parent, spec):
super().__init__(name, parent)
self.spec = spec
def runtest(self):
builtins = {
'typing': typing,
'time': FakeTimeModule(),
'datetime': datetime,
'print': fake_print
}
byte_code = compile(self.spec, '<inline>', 'exec')
exec(byte_code, builtins)
def repr_failure(self, excinfo, **kwargs):
""" called when self.runtest() raises an exception. """
return (
f"Code snippet {self.name} raised an error: {excinfo.value}. "
f"The executed code was: {self.spec}"
)
def reportinfo(self):
return self.fspath, 0, "usecase: {}".format(self.name)