-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
44 lines (29 loc) · 1.18 KB
/
tests.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
import os
import subprocess
import unittest
import retronews
TC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests")
class TestHtmlRender(unittest.TestCase):
maxDiff = None
def checkRendering(self, name: str):
html_path = os.path.join(TC_DIR, f"{name}.html")
out_path = os.path.join(TC_DIR, f"{name}.out")
with open(html_path) as fp:
html = fp.read()
actual = retronews.html_render(html)
if not os.path.exists(out_path):
with open(out_path, "w") as fp:
fp.write(actual)
return
cmd = ["diff", "-Nru", "--color=always", out_path, "-"]
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
stdout, stderr = proc.communicate(input=actual)
if proc.returncode != 0:
self.fail(f"Unexpected rendering output\n{stdout}")
def setup_test_cases():
tcs = [x.split(".")[0] for x in sorted(os.listdir(TC_DIR)) if x.endswith(".html")]
for tc in tcs:
setattr(TestHtmlRender, tc, lambda self, tc=tc: self.checkRendering(tc))
if __name__ == "__main__":
setup_test_cases()
unittest.main()