From 256806a3dd10ccdfb1295181d09af7655c05fa29 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 6 Sep 2015 18:08:58 +0100 Subject: Capture errors on trying to open file URIs --- tests/docx/files_tests.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'tests/docx') 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) -- cgit v1.2.3