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