49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import contextlib
|
|
import io
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from copienator import utils
|
|
|
|
|
|
class CompileToPdfTests(unittest.TestCase):
|
|
def test_warns_when_latex_fails_even_if_partial_pdf_exists(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
output = Path(temporary) / "result.pdf"
|
|
|
|
def failed_run(*_args, **kwargs):
|
|
(Path(kwargs["cwd"]) / "text.pdf").write_bytes(b"partial")
|
|
return subprocess.CompletedProcess(
|
|
args=[], returncode=1,
|
|
stdout="! LaTeX Error: Something's wrong--perhaps a missing \\item.\n",
|
|
)
|
|
|
|
console = io.StringIO()
|
|
with patch.object(utils.subprocess, "run", side_effect=failed_run), \
|
|
contextlib.redirect_stdout(console):
|
|
utils.compile_to_pdf("broken", output)
|
|
|
|
self.assertEqual(output.read_bytes(), b"partial")
|
|
self.assertIn("Warning: LaTeX compilation failed", console.getvalue())
|
|
self.assertIn("missing \\item", console.getvalue())
|
|
|
|
def test_warns_when_no_pdf_is_produced(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
output = Path(temporary) / "result.pdf"
|
|
result = subprocess.CompletedProcess(args=[], returncode=0, stdout="")
|
|
|
|
console = io.StringIO()
|
|
with patch.object(utils.subprocess, "run", return_value=result), \
|
|
contextlib.redirect_stdout(console):
|
|
utils.compile_to_pdf("valid", output)
|
|
|
|
self.assertFalse(output.exists())
|
|
self.assertIn("produced no PDF", console.getvalue())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|