diff --git a/copienator/utils.py b/copienator/utils.py index 74acd59..d0ba2c9 100644 --- a/copienator/utils.py +++ b/copienator/utils.py @@ -177,21 +177,23 @@ def compile_to_pdf(text, output_pdf_path): text=True, 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: error_lines = [ line.strip() for line in result.stdout.splitlines() if line.lstrip().startswith("!") ] detail = f": {error_lines[0]}" if error_lines else "" + pdf_status = "a PDF was generated" if pdf_generated else "no PDF was generated" print( - f"Warning: LaTeX compilation failed for {output_pdf_path} " - f"(exit code {result.returncode}){detail}" + f"Warning: LaTeX compilation reported errors for {output_pdf_path} " + f"(exit code {result.returncode}; {pdf_status}){detail}" ) - - generated_pdf = os.path.join(temp_dir, pdf_filename) - if os.path.exists(generated_pdf): - shutil.move(generated_pdf, output_pdf_path) - else: + elif not pdf_generated: print(f"Warning: LaTeX compilation produced no PDF for {output_pdf_path}") except Exception as e: print(f"Compilation error for {output_pdf_path}: {e}") diff --git a/tests/test_utils_compile.py b/tests/test_utils_compile.py index 0763df3..abf48f1 100644 --- a/tests/test_utils_compile.py +++ b/tests/test_utils_compile.py @@ -27,9 +27,26 @@ class CompileToPdfTests(unittest.TestCase): utils.compile_to_pdf("broken", output) 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()) + 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): with tempfile.TemporaryDirectory() as temporary: output = Path(temporary) / "result.pdf"