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