-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·80 lines (67 loc) · 2.18 KB
/
test.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
#!/usr/bin/python
"""Test the main structure of the spec file modular parsing.
"""
from main import toplevel
from pathlib import Path
import os
import pytest
import sys
def run_source_tests(dir):
# Extract the example clib file in the README
# and temporarily add it to the tests folder.
with open(Path(dir, "README.md"), "r") as file:
file = file.read()
_, clib = file.split("```clib", 1)
clib, _ = clib.split("```", 1)
clib_path = Path(dir, "tests/specs/basic_awk_from_README.clib").resolve()
with open(clib_path, "w") as file:
file.write(clib)
readme_extract_cleared = False
clearing_message = False
for (message, paths) in [
(
"Testing basic clibate successes.",
("tests/specs/main.clib", "tests/input", "tests"),
),
(
"Meta-testing clibate, including tests failures and clibate errors.",
("tests/meta/main.clib", "tests", "tests"),
),
]:
print("\n{0}{1}{0}".format(" ".join(7 * "-"), message))
specs, input, sandbox = (Path(dir, p) for p in paths)
try:
if error := toplevel(specs, input, sandbox):
# Project testing failure.
message, code = error
print(message, file=sys.stderr)
break
except:
if not readme_extract_cleared:
print(
f"Expection caught while running tests, cleaning up {clib_path}..",
end="",
)
clearing_message = True
raise
finally:
if not readme_extract_cleared:
os.remove(clib_path)
if clearing_message:
print(" done.")
readme_extract_cleared = True
if __name__ == "__main__":
# Find project tests file relatively to this script.
dir = Path(__file__).parent
# Crawl doctests first.
if not pytest.main(
[
"--doctest-modules",
"-x",
str(dir),
"-s",
"--ignore",
Path(dir, "tests/meta"), # Avoid recursing there.
]
):
run_source_tests(dir)