]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Put Texifier in spec
[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(texifier),
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 This is \\href{https://glowfic.com}{glowfic}
58
59 You \\emph{sure}\?
60
61 Pretty sure.
62
63 \\end{document}
64 ''', buf.getvalue())
65
66 def testDirectTexifier(self) -> None:
67 texifier = VerifyingTexifier(
68 PandocTexifier('pandoc'), DirectTexifier())
69 buf = io.BytesIO()
70 spec = Spec(
71 self.url(),
72 self.fetcher(),
73 FakeImageStore(),
74 lambda x: x,
75 lambda x: None,
76 texifier,
77 lambda x: x,
78 20,
79 ContentOnlyLayout(texifier),
80 None,
81 buf,
82 lambda _: None)
83 paperdoorknob.process(spec)
84
85 def testPDF(self) -> None:
86 texifier = PandocTexifier('pandoc')
87 with open("test.tex", 'wb') as out:
88 spec = Spec(
89 self.url(),
90 self.fetcher(),
91 FakeImageStore(),
92 lambda x: x,
93 lambda x: None,
94 texifier,
95 lambda x: x,
96 20,
97 BesideIconLayout(texifier),
98 None,
99 out,
100 lambda _: None)
101 paperdoorknob.process(spec)
102 subprocess.run(['pdflatex', 'test.tex'],
103 stdin=subprocess.DEVNULL, check=True)
104
105
106 class TestProcessFromWebserver(BaseTestProcess, unittest.TestCase):
107
108 def setUp(self) -> None:
109 self._fetcher = self.enterContext(DirectFetcher(TIMEOUT))
110 self._server = self.enterContext(FakeGlowficServer())
111 self._port = self._server.port()
112
113 def url(self) -> str:
114 return f"http://localhost:{self._port}"
115
116 def fetcher(self) -> Fetcher:
117 return self._fetcher
118
119
120 class TestProcessFromFakeFetcher(BaseTestProcess, unittest.TestCase):
121
122 def url(self) -> str:
123 return 'fic'
124
125 def fetcher(self) -> Fetcher:
126 with open('testdata/this-is-glowfic.html', 'rb') as f:
127 return FakeFetcher({'fic?view=flat': f.read(9999)})
128
129
130 if __name__ == '__main__':
131 unittest.main()