tests.py (1204B)
1 import os 2 import subprocess 3 import unittest 4 5 import retronews 6 7 TC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tests") 8 9 10 class TestHtmlRender(unittest.TestCase): 11 maxDiff = None 12 13 def checkRendering(self, name: str): 14 html_path = os.path.join(TC_DIR, f"{name}.html") 15 out_path = os.path.join(TC_DIR, f"{name}.out") 16 17 with open(html_path) as fp: 18 html = fp.read() 19 20 actual = retronews.html_render(html) 21 22 if not os.path.exists(out_path): 23 with open(out_path, "w") as fp: 24 fp.write(actual) 25 return 26 27 cmd = ["diff", "-Nru", "--color=always", out_path, "-"] 28 proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True) 29 stdout, stderr = proc.communicate(input=actual) 30 31 if proc.returncode != 0: 32 self.fail(f"Unexpected rendering output\n{stdout}") 33 34 35 def setup_test_cases(): 36 tcs = [x.split(".")[0] for x in sorted(os.listdir(TC_DIR)) if x.endswith(".html")] 37 38 for tc in tcs: 39 setattr(TestHtmlRender, tc, lambda self, tc=tc: self.checkRendering(tc)) 40 41 42 if __name__ == "__main__": 43 setup_test_cases() 44 unittest.main()