-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconftest.py
85 lines (69 loc) · 2.33 KB
/
conftest.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
import doctest
import pathlib
from doctest import OutputChecker
ROOT = pathlib.Path(__file__).parent
PATH = ROOT / "serialize" / "testsuite"
GENERATED_PATH = PATH / "generated"
TEST_README = GENERATED_PATH / "test_readme.py"
README = ROOT / "README.md"
def setup_test_readme():
GENERATED_PATH.mkdir(exist_ok=True)
INDENT = " " * 4
with TEST_README.open("w") as out, README.open("r") as readme:
mode = None
output = []
output.append("def test_all():\n")
for i, line in enumerate(readme.readlines()):
output.append("\n")
if mode is None and line.strip() == "```python":
mode = "first_line"
output[i] = INDENT + "# line %04d" % i
# output[i] = 'def test_line_%04d():\n' % i
continue
elif line.strip() == "```":
continue
elif mode == "first_line":
if line.strip() == "":
mode = None
output[i - 1] = "\n"
continue
if line.strip().startswith(">>>"):
mode = "doctest"
output[i - 2] = (
output[i - 1][:-1] + " " + output[i - 2]
) # move the def line one line up
output[i - 1] = ' """\n'
else:
mode = "test"
if mode in ("doctest", "test"):
output[i] = " " + line
else:
pass
# output[i] = '# %s' % line
output.append(INDENT + '"""\n')
out.writelines(output)
def pytest_sessionstart(session):
try:
setup_test_readme()
except ImportError:
pass
def rm_tree(pth):
for child in pth.glob("*"):
if child.is_file():
child.unlink()
else:
rm_tree(child)
pth.rmdir()
def pytest_sessionfinish(session, exitstatus):
try:
rm_tree(GENERATED_PATH)
pass
except FileNotFoundError:
pass
class HexOutputChecker(OutputChecker):
def check_output(self, want, got, optionflags):
if want.startswith("b'") or want.startswith('b"'):
return True
else:
return OutputChecker.check_output(self, want, got, optionflags)
doctest.OutputChecker = HexOutputChecker