Better Diagnostic for LaTeX errors
This commit is contained in:
1 parent
0a453403cf
commit
32764c101b
2 files changed
+27
-8
No files matched your search
+9
-7
@@ -177,21 +177,23 @@ def compile_to_pdf(text, output_pdf_path):
|
|||||||
text=True,
|
text=True,
|
||||||
check=False)
|
check=False)
|
||||||
|
|
||||||
|
generated_pdf = os.path.join(temp_dir, pdf_filename)
|
||||||
|
pdf_generated = os.path.exists(generated_pdf)
|
||||||
|
if pdf_generated:
|
||||||
|
shutil.move(generated_pdf, output_pdf_path)
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
error_lines = [
|
error_lines = [
|
||||||
line.strip() for line in result.stdout.splitlines()
|
line.strip() for line in result.stdout.splitlines()
|
||||||
if line.lstrip().startswith("!")
|
if line.lstrip().startswith("!")
|
||||||
]
|
]
|
||||||
detail = f": {error_lines[0]}" if error_lines else ""
|
detail = f": {error_lines[0]}" if error_lines else ""
|
||||||
|
pdf_status = "a PDF was generated" if pdf_generated else "no PDF was generated"
|
||||||
print(
|
print(
|
||||||
f"Warning: LaTeX compilation failed for {output_pdf_path} "
|
f"Warning: LaTeX compilation reported errors for {output_pdf_path} "
|
||||||
f"(exit code {result.returncode}){detail}"
|
f"(exit code {result.returncode}; {pdf_status}){detail}"
|
||||||
)
|
)
|
||||||
|
elif not pdf_generated:
|
||||||
generated_pdf = os.path.join(temp_dir, pdf_filename)
|
|
||||||
if os.path.exists(generated_pdf):
|
|
||||||
shutil.move(generated_pdf, output_pdf_path)
|
|
||||||
else:
|
|
||||||
print(f"Warning: LaTeX compilation produced no PDF for {output_pdf_path}")
|
print(f"Warning: LaTeX compilation produced no PDF for {output_pdf_path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Compilation error for {output_pdf_path}: {e}")
|
print(f"Compilation error for {output_pdf_path}: {e}")
|
||||||
@@ -27,9 +27,26 @@ class CompileToPdfTests(unittest.TestCase):
|
|||||||
utils.compile_to_pdf("broken", output)
|
utils.compile_to_pdf("broken", output)
|
||||||
|
|
||||||
self.assertEqual(output.read_bytes(), b"partial")
|
self.assertEqual(output.read_bytes(), b"partial")
|
||||||
self.assertIn("Warning: LaTeX compilation failed", console.getvalue())
|
self.assertIn("Warning: LaTeX compilation reported errors", console.getvalue())
|
||||||
|
self.assertIn("a PDF was generated", console.getvalue())
|
||||||
self.assertIn("missing \\item", console.getvalue())
|
self.assertIn("missing \\item", console.getvalue())
|
||||||
|
|
||||||
|
def test_says_no_pdf_was_generated_when_latex_fails(self):
|
||||||
|
with tempfile.TemporaryDirectory() as temporary:
|
||||||
|
output = Path(temporary) / "result.pdf"
|
||||||
|
result = subprocess.CompletedProcess(
|
||||||
|
args=[], returncode=1, stdout="! Undefined control sequence.\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
console = io.StringIO()
|
||||||
|
with patch.object(utils.subprocess, "run", return_value=result), \
|
||||||
|
contextlib.redirect_stdout(console):
|
||||||
|
utils.compile_to_pdf("broken", output)
|
||||||
|
|
||||||
|
self.assertFalse(output.exists())
|
||||||
|
self.assertIn("no PDF was generated", console.getvalue())
|
||||||
|
self.assertIn("Undefined control sequence", console.getvalue())
|
||||||
|
|
||||||
def test_warns_when_no_pdf_is_produced(self):
|
def test_warns_when_no_pdf_is_produced(self):
|
||||||
with tempfile.TemporaryDirectory() as temporary:
|
with tempfile.TemporaryDirectory() as temporary:
|
||||||
output = Path(temporary) / "result.pdf"
|
output = Path(temporary) / "result.pdf"
|
||||||
|
|||||||
Reference in new issue
Block a user