diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-06 18:08:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-06 18:08:58 +0100 |
| commit | 256806a3dd10ccdfb1295181d09af7655c05fa29 (patch) | |
| tree | 682532ac1eaf1718c03430e5b61e20dfe6b03d7d | |
| parent | cff7a81ce9c49971ca94fc8fe74430338e563836 (diff) | |
Capture errors on trying to open file URIs
| -rw-r--r-- | mammoth/docx/files.py | 20 | ||||
| -rw-r--r-- | tests/docx/files_tests.py | 23 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 2 | ||||
| -rw-r--r-- | tests/testing.py | 9 |
4 files changed, 41 insertions, 13 deletions
diff --git a/mammoth/docx/files.py b/mammoth/docx/files.py index bde488d..dc5146e 100644 --- a/mammoth/docx/files.py +++ b/mammoth/docx/files.py @@ -19,17 +19,17 @@ class Files(object): pass def open(self, uri): - if _is_absolute(uri): - return contextlib.closing(urlopen(uri)) - elif self._base is not None: - try: + try: + if _is_absolute(uri): + return contextlib.closing(urlopen(uri)) + elif self._base is not None: return open(os.path.join(self._base, uri), "rb") - except IOError as error: - message = "could not open external image '{0}' from document directory '{1}'\n{2}".format( - uri, self._base, str(error)) - raise InvalidFileReferenceError(message) - else: - raise InvalidFileReferenceError("could not find external image '{0}', fileobj has no name".format(uri)) + else: + raise InvalidFileReferenceError("could not find external image '{0}', fileobj has no name".format(uri)) + except IOError as error: + message = "could not open external image: '{0}' (document directory: '{1}')\n{2}".format( + uri, self._base, str(error)) + raise InvalidFileReferenceError(message) def _is_absolute(url): diff --git a/tests/docx/files_tests.py b/tests/docx/files_tests.py index c1e15dc..56dc69a 100644 --- a/tests/docx/files_tests.py +++ b/tests/docx/files_tests.py @@ -1,7 +1,7 @@ from nose.tools import istest, assert_equal -from mammoth.docx.files import Files -from ..testing import test_path +from mammoth.docx.files import Files, InvalidFileReferenceError +from ..testing import test_path, assert_raises @istest @@ -23,3 +23,22 @@ def can_open_files_with_relative_uri(): assert_equal(bytes, type(contents)) with open(test_path("tiny-picture.png"), "rb") as source_file: assert_equal(source_file.read(), contents) + + +@istest +def error_is_raised_if_relative_uri_cannot_be_opened(): + files = Files("/tmp") + error = assert_raises(InvalidFileReferenceError, lambda: files.open("not-a-real-file.png")) + expected_message = ( + "could not open external image: 'not-a-real-file.png' (document directory: '/tmp')\n" + + "[Errno 2] No such file or directory: '/tmp/not-a-real-file.png'" + ) + assert_equal(expected_message, str(error)) + + +@istest +def error_is_raised_if_file_uri_cannot_be_opened(): + files = Files("/tmp") + error = assert_raises(InvalidFileReferenceError, lambda: files.open("file:///not-a-real-file.png")) + expected_message = "could not open external image: 'file:///not-a-real-file.png' (document directory: '/tmp')\n" + assert str(error).startswith(expected_message) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index f29ae0f..906f49b 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -96,7 +96,7 @@ def warn_if_images_stored_outside_of_document_are_not_found(): with open(document_path, "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) - expected_warning = "could not open external image 'tiny-picture.png' from document directory '{0}'\n[Errno 2] No such file or directory".format(temp_dir.path) + expected_warning = "could not open external image: 'tiny-picture.png'" assert_equal("warning", result.messages[0].type) assert result.messages[0].message.startswith(expected_warning), "message was: " + result.messages[0].message assert_equal(1, len(result.messages)) diff --git a/tests/testing.py b/tests/testing.py index d70ded6..c5013dc 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -7,3 +7,12 @@ from nose.tools import nottest def test_path(path): this_dir = os.path.dirname(__file__) return os.path.join(this_dir, "test-data", path) + + +def assert_raises(exception, func): + try: + func() + assert False, "Expected " + exception.__name__ + except exception as error: + return error + |
