diff --git a/tests/test_4141.py b/tests/test_4141.py index bd13988f6..3588f2b38 100644 --- a/tests/test_4141.py +++ b/tests/test_4141.py @@ -1,19 +1,27 @@ import pymupdf - -import os.path +from pathlib import Path def test_4141(): - """survive missing /Resources object in a number of cases.""" - path = os.path.abspath(f"{__file__}/../../tests/resources/test_4141.pdf") - doc = pymupdf.open(path) - page = doc[0] - # make sure the right test file - assert doc.xref_get_key(page.xref, "Resources") == ("null", "null") - page.insert_htmlbox((100, 100, 200, 200), "Hallo") # will fail without the fix - doc.close() - doc = pymupdf.open(doc.name) - page = doc[0] - tw = pymupdf.TextWriter(page.rect) - tw.append((100, 100), "Hallo") - tw.write_text(page) # will fail without the fix + """Survive missing /Resources object in multiple situations.""" + + # Resolve the test file path + test_pdf = Path(__file__).resolve().parent.parent / "tests" / "resources" / "test_4141.pdf" + assert test_pdf.exists(), f"Missing test file: {test_pdf}" + + # Case 1: Insert HTML box + with pymupdf.open(test_pdf) as doc: + page = doc[0] + + # Ensure we are using the correct test file + assert doc.xref_get_key(page.xref, "Resources") == ("null", "null") + + # Should not fail after the fix + page.insert_htmlbox((100, 100, 200, 200), "Hallo") + + # Case 2: TextWriter operations after reopen + with pymupdf.open(test_pdf) as doc: + page = doc[0] + tw = pymupdf.TextWriter(page.rect) + tw.append((100, 100), "Hallo") + tw.write_text(page) # Should also not fail