]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Reify Chunk
[paperdoorknob] / paperdoorknob_test.py
1 # paperdoorknob: Print glowfic
2 #
3 # This program is free software: you can redistribute it and/or modify it
4 # under the terms of the GNU General Public License as published by the
5 # Free Software Foundation, version 3.
6
7
8 from abc import ABC, abstractmethod
9 import unittest
10 import io
11 import subprocess
12
13 import paperdoorknob
14
15 from testing.fakeserver import FakeGlowficServer
16 from domfilter import ApplyDOMFilters
17 from fetch import DirectFetcher, FakeFetcher, Fetcher
18 from images import FakeImageStore
19 from spec import Spec
20 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
21
22 TIMEOUT = 8
23
24
25 class BaseTestProcess(ABC):
26
27 @abstractmethod
28 def url(self) -> str:
29 raise NotImplementedError()
30
31 @abstractmethod
32 def fetcher(self) -> Fetcher:
33 raise NotImplementedError()
34
35 def testProcess(self) -> None:
36 buf = io.BytesIO()
37 spec = Spec(
38 self.url(),
39 self.fetcher(),
40 FakeImageStore(),
41 lambda x: x,
42 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
43 PandocTexifier('pandoc'),
44 'margin=20mm',
45 buf)
46 paperdoorknob.process(spec)
47 assert buf.getvalue() == b'''\\documentclass{article}
48 \\usepackage[margin=20mm]{geometry}
49 \\begin{document}
50 This is glowfic
51 You \\emph{sure}?
52 Pretty sure.
53 \\end{document}
54 '''
55
56 def testDirectTexifier(self) -> None:
57 texifier = VerifyingTexifier(
58 PandocTexifier('pandoc'), DirectTexifier())
59 buf = io.BytesIO()
60 spec = Spec(
61 self.url(),
62 self.fetcher(),
63 FakeImageStore(),
64 lambda x: x,
65 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
66 texifier,
67 None,
68 buf)
69 paperdoorknob.process(spec)
70
71 def testPDF(self) -> None:
72 with open("test.tex", 'wb') as out:
73 spec = Spec(
74 self.url(),
75 self.fetcher(),
76 FakeImageStore(),
77 lambda x: x,
78 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
79 PandocTexifier('pandoc'),
80 None,
81 out)
82 paperdoorknob.process(spec)
83 subprocess.run(['pdflatex', 'test.tex'],
84 stdin=subprocess.DEVNULL, check=True)
85
86
87 class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
88
89 def setUp(self) -> None:
90 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
91 self._server = self.enterContext(FakeGlowficServer())
92 self._port = self._server.port()
93
94 def url(self) -> str:
95 return f"http://localhost:{self._port}"
96
97 def fetcher(self) -> Fetcher:
98 return self._fetcher
99
100
101 class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
102
103 def url(self) -> str:
104 return 'fic'
105
106 def fetcher(self) -> Fetcher:
107 with open('testdata/this-is-glowfic.html', 'rb') as f:
108 return FakeFetcher({'fic': f.read(9999)})
109
110
111 if __name__ == '__main__':
112 unittest.main()