]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Support _ in URLs
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
b25a2f90
SW
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
38621839 8from abc import ABC, abstractmethod
b25a2f90 9import unittest
36ae1d5f 10import io
4aa87092 11import re
07f9b178 12import subprocess
23f31879 13
b25a2f90 14import paperdoorknob
23f31879 15
41b11505 16from testing.fakeserver import FakeGlowficServer
38621839 17from fetch import DirectFetcher, FakeFetcher, Fetcher
37221176 18from glowfic import ContentOnlyLayout, BesideIconLayout
4b7d905e 19from images import FakeImageStore
23f31879 20from spec import Spec
f1dec720 21from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
b25a2f90 22
b25a2f90
SW
23TIMEOUT = 8
24
25
38621839
SW
26class BaseTestProcess(ABC):
27
28 @abstractmethod
29 def url(self) -> str:
30 raise NotImplementedError()
31
32 @abstractmethod
33 def fetcher(self) -> Fetcher:
34 raise NotImplementedError()
b25a2f90 35
36ae1d5f 36 def testProcess(self) -> None:
38621839
SW
37 buf = io.BytesIO()
38 spec = Spec(
39 self.url(),
40 self.fetcher(),
4b7d905e 41 FakeImageStore(),
929db576 42 lambda x: x,
4640c55a 43 lambda x: None,
131deef1 44 lambda x: x,
d2a41ff4 45 ContentOnlyLayout(PandocTexifier('pandoc')),
e10b5b6f 46 'margin=20mm',
c594c8bf
SW
47 buf,
48 lambda _: None)
38621839 49 paperdoorknob.process(spec)
4aa87092
SW
50 assert re.match(br'''\\documentclass{article}
51(\\usepackage{[a-z]+}\n)+\\usepackage\[margin=20mm\]{geometry}
07f9b178 52\\begin{document}
228bb7bb 53\\newcommand{\\href}\[2\]{#2\\footnote{\\detokenize{#1}}}
b2549764 54This is \\href{https://glowfic.com}{glowfic}
85bcacb0 55
4aa87092 56You \\emph{sure}\?
85bcacb0 57
07f9b178 58Pretty sure.
23dabdf5 59
07f9b178 60\\end{document}
4aa87092 61''', buf.getvalue())
07f9b178 62
f1dec720
SW
63 def testDirectTexifier(self) -> None:
64 texifier = VerifyingTexifier(
65 PandocTexifier('pandoc'), DirectTexifier())
38621839 66 buf = io.BytesIO()
8be20b9d
SW
67 spec = Spec(
68 self.url(),
69 self.fetcher(),
4b7d905e 70 FakeImageStore(),
8be20b9d 71 lambda x: x,
4640c55a 72 lambda x: None,
131deef1 73 lambda x: x,
d2a41ff4 74 ContentOnlyLayout(texifier),
e10b5b6f 75 None,
c594c8bf
SW
76 buf,
77 lambda _: None)
38621839 78 paperdoorknob.process(spec)
f1dec720 79
07f9b178 80 def testPDF(self) -> None:
38621839
SW
81 with open("test.tex", 'wb') as out:
82 spec = Spec(
83 self.url(),
84 self.fetcher(),
4b7d905e 85 FakeImageStore(),
929db576 86 lambda x: x,
4640c55a 87 lambda x: None,
131deef1 88 lambda x: x,
37221176 89 BesideIconLayout(PandocTexifier('pandoc'), 20),
e10b5b6f 90 None,
c594c8bf
SW
91 out,
92 lambda _: None)
38621839 93 paperdoorknob.process(spec)
23f31879
SW
94 subprocess.run(['pdflatex', 'test.tex'],
95 stdin=subprocess.DEVNULL, check=True)
36ae1d5f 96
b25a2f90 97
38621839
SW
98class 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
112class 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:
1452f8d3 119 return FakeFetcher({'fic?view=flat': f.read(9999)})
38621839
SW
120
121
b25a2f90
SW
122if __name__ == '__main__':
123 unittest.main()