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