From 93e07b02624f668aa9300c048cde05bb416dda35 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 23 Jan 2018 21:43:30 +0000 Subject: Raise error when main document part does not exist --- mammoth/docx/__init__.py | 2 +- tests/docx/docx_tests.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 8ecf8c9..b155cff 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -77,7 +77,7 @@ def _find_document_filename(zip_file, relationships): ] + ["word/document.xml"] valid_targets = list(filter(lambda target: zip_file.exists(target), targets)) if len(valid_targets) == 0: - return None + raise IOError("Could not find main document part. Are you sure this is a valid .docx file?") else: return valid_targets[0] diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py index 8bd5b36..d450424 100644 --- a/tests/docx/docx_tests.py +++ b/tests/docx/docx_tests.py @@ -5,7 +5,7 @@ import zipfile from nose.tools import istest, assert_equal from mammoth import docx, documents -from ..testing import test_path +from ..testing import assert_raises, test_path @istest @@ -62,6 +62,23 @@ def main_document_is_found_using_package_relationships(): assert_equal(expected_document, result.value) +@istest +def error_is_raised_when_main_document_part_does_not_exist(): + fileobj = _create_zip({ + "_rels/.rels": textwrap.dedent("""\ + + + + + """), + }) + error = assert_raises(IOError, lambda: docx.read(fileobj=fileobj)) + assert_equal( + "Could not find main document part. Are you sure this is a valid .docx file?", + str(error), + ) + + def _create_zip(files): fileobj = io.BytesIO() -- cgit v1.2.3