From 659c4ffe6ade2d8132873ae3f3f3a22df38fdb37 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 5 Dec 2026 11:06:47 +0000 Subject: Handle XML documents that are missing a root element --- tests/docx/docx_tests.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests/docx/docx_tests.py') diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py index ee6b928..c409e11 100644 --- a/tests/docx/docx_tests.py +++ b/tests/docx/docx_tests.py @@ -72,6 +72,39 @@ def test_error_is_raised_when_main_document_part_does_not_exist(): str(error), ) + +def test_when_main_document_part_is_empty_then_error_is_raised(): + fileobj = _create_zip({ + "word/document.xml": textwrap.dedent("""\ + + """), + }) + + error = assert_raises(IOError, lambda: docx.read(fileobj=fileobj)) + + assert_equal( + "Main document part is missing root element. Are you sure this is a valid .docx file?", + str(error), + ) + + +def test_when_part_other_than_main_document_is_empty_then_part_is_ignored(): + fileobj = _create_zip({ + "word/document.xml": textwrap.dedent("""\ + + + + + """), + "word/footnotes.xml": textwrap.dedent("""\ + + """), + }) + result = docx.read(fileobj=fileobj) + expected_document = documents.document([]) + assert_equal(expected_document, result.value) + + class PartPathsTests(object): def test_main_document_part_is_found_using_package_relationships(self): fileobj = _create_zip({ -- cgit v1.2.3