-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
103 lines (86 loc) · 2.87 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import doctest
import importlib
import sys
import time
sys.path.insert(0, "./")
def zipapp_module():
from morebuiltins.zipapps.main import create_app
create_app(
"./morebuiltins",
output="./morebuiltins.pyz",
compressed=True,
)
def test_all():
# test local usage and pyz usage
for path in ["./", "./morebuiltins.pyz"]:
sys.modules.pop("morebuiltins")
sys.path.insert(0, path)
import morebuiltins
assert path.replace("./", "") in morebuiltins.__file__, morebuiltins.__file__
for name in morebuiltins.__all__:
module = importlib.import_module(name)
print(
time.strftime("%Y-%m-%d %H:%M:%S"),
"[TEST]",
module.__name__,
flush=True,
)
if hasattr(module, "test"):
module.test()
else:
result = doctest.testmod(module)
if result.failed:
raise RuntimeError
print(
time.strftime("%Y-%m-%d %H:%M:%S"),
"[PASS]",
module.__name__,
flush=True,
)
time.sleep(1)
print("all test ok", flush=True)
def make_docs():
import importlib
import re
from pathlib import Path
import morebuiltins
short_doc = ""
with open("doc.md", "w", encoding="u8") as f:
for index1, name in enumerate(morebuiltins.__all__, 1):
module = importlib.import_module(name)
name1 = f"## {index1}. {name}\n\n"
short_doc += name1
print(name1, file=f)
for index2, name in enumerate(module.__all__, 1):
member = vars(module)[name]
doc = member.__doc__
if doc:
doc = doc.lstrip()
title = f"{index1}.{index2} `{name}`"
lines = re.split("\n", doc, maxsplit=1)
head = "%s - %s" % (title, lines[0])
short_doc += head + "\n\n"
if len(lines) > 1:
tail = lines[1]
tail = f'```python\n{tail}\n```'
else:
tail = ""
line = f"\n{head}\n\n\n{tail}\n\n"
print(
line,
"\n---\n\n",
sep="",
file=f,
)
short_doc += "\n"
path = Path("README.md")
old_readme = path.read_text("u8")
start = old_readme.find("<!-- start -->\n") + len("<!-- start -->\n")
end = old_readme.find("<!-- end -->")
path.write_text(old_readme[:start] + short_doc + old_readme[end:], encoding="u8")
def main():
make_docs()
zipapp_module()
test_all()
if __name__ == "__main__":
main()