]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Fetch the non-flat view to get the next-thread link
[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 re
12 import subprocess
13
14 import paperdoorknob
15
16 from testing.fakeserver import FakeGlowficServer
17 from fetch import DirectFetcher, FakeFetcher, Fetcher
18 from glowfic import ContentOnlyLayout, BesideIconLayout
19 from images import FakeImageStore
20 from spec import Spec
21 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
22
23 TIMEOUT = 8
24
25
26 class BaseTestProcess(ABC):
27
28 @abstractmethod
29 def url(self) -> str:
30 raise NotImplementedError()
31
32 @abstractmethod
33 def fetcher(self) -> Fetcher:
34 raise NotImplementedError()
35
36 def testProcess(self) -> None:
37 buf = io.BytesIO()
38 texifier = PandocTexifier('pandoc')
39 spec = Spec(
40 self.url(),
41 self.fetcher(),
42 FakeImageStore(),
43 lambda x: x,
44 lambda x: None,
45 texifier,
46 lambda x: x,
47 20,
48 ContentOnlyLayout,
49 'margin=20mm',
50 buf,
51 lambda _: None)
52 paperdoorknob.process(spec)
53 assert re.match(br'''\\documentclass{article}
54 (\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
55 \\begin{document}
56 (.|\n)*
57 \\glowhead{}{}{}{}This is \\href{https://glowfic.com}{glowfic}
58 \\glowhead{}{}{}{}You \\emph{sure}\?
59 \\glowhead{}{}{}{}Pretty sure.
60 \\end{document}
61 ''', buf.getvalue())
62
63 def testDirectTexifier(self) -> None:
64 texifier = VerifyingTexifier(
65 PandocTexifier('pandoc'), DirectTexifier())
66 buf = io.BytesIO()
67 spec = Spec(
68 self.url(),
69 self.fetcher(),
70 FakeImageStore(),
71 lambda x: x,
72 lambda x: None,
73 texifier,
74 lambda x: x,
75 20,
76 ContentOnlyLayout,
77 None,
78 buf,
79 lambda _: None)
80 paperdoorknob.process(spec)
81
82 def testPDF(self) -> None:
83 texifier = PandocTexifier('pandoc')
84 with open("test.tex", 'wb') as out:
85 spec = Spec(
86 self.url(),
87 self.fetcher(),
88 FakeImageStore(),
89 lambda x: x,
90 lambda x: None,
91 texifier,
92 lambda x: x,
93 20,
94 BesideIconLayout,
95 None,
96 out,
97 lambda _: None)
98 paperdoorknob.process(spec)
99 subprocess.run(['pdflatex', 'test.tex'],
100 stdin=subprocess.DEVNULL, check=True)
101
102
103 class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
104
105 def setUp(self) -> None:
106 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
107 self._server = self.enterContext(FakeGlowficServer())
108 self._port = self._server.port()
109
110 def url(self) -> str:
111 return f"http://localhost:{self._port}"
112
113 def fetcher(self) -> Fetcher:
114 return self._fetcher
115
116
117 class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
118
119 def url(self) -> str:
120 return 'fic'
121
122 def fetcher(self) -> Fetcher:
123 with open('testdata/this-is-glowfic.html', 'rb') as f:
124 html = f.read(9999)
125 return FakeFetcher({'fic': html, 'fic?view=flat': html})
126
127
128 if __name__ == '__main__':
129 unittest.main()