1- from mammoth .docx .files import Files , InvalidFileReferenceError
1+ from mammoth .docx .files import ExternalFileAccessIsDisabledError , Files , InvalidFileReferenceError
22from ..testing import generate_test_path , assert_equal , assert_raises
33
44
5+ def test_when_external_file_access_is_disabled_then_opening_file_raises_error ():
6+ files = Files (None , external_file_access = False )
7+ error = assert_raises (ExternalFileAccessIsDisabledError , lambda : files .open ("/tmp/image.png" ))
8+ expected_message = (
9+ "could not open external image '/tmp/image.png', external file access is disabled"
10+ )
11+ assert_equal (expected_message , str (error ))
12+
13+
514def test_can_open_files_with_file_uri ():
615 path = generate_test_path ("tiny-picture.png" )
7- files = Files (None )
16+ files = Files (None , external_file_access = True )
817 with files .open ("file:///" + path ) as image_file :
918 contents = image_file .read ()
1019 assert_equal (bytes , type (contents ))
@@ -13,7 +22,7 @@ def test_can_open_files_with_file_uri():
1322
1423
1524def test_can_open_files_with_relative_uri ():
16- files = Files (generate_test_path ("" ))
25+ files = Files (generate_test_path ("" ), external_file_access = True )
1726 with files .open ("tiny-picture.png" ) as image_file :
1827 contents = image_file .read ()
1928 assert_equal (bytes , type (contents ))
@@ -22,7 +31,7 @@ def test_can_open_files_with_relative_uri():
2231
2332
2433def test_given_base_is_not_set_when_opening_relative_uri_then_error_is_raised ():
25- files = Files (None )
34+ files = Files (None , external_file_access = True )
2635 error = assert_raises (InvalidFileReferenceError , lambda : files .open ("not-a-real-file.png" ))
2736 expected_message = (
2837 "could not find external image 'not-a-real-file.png', fileobj has no name"
@@ -31,7 +40,7 @@ def test_given_base_is_not_set_when_opening_relative_uri_then_error_is_raised():
3140
3241
3342def test_error_is_raised_if_relative_uri_cannot_be_opened ():
34- files = Files ("/tmp" )
43+ files = Files ("/tmp" , external_file_access = True )
3544 error = assert_raises (InvalidFileReferenceError , lambda : files .open ("not-a-real-file.png" ))
3645 expected_message = (
3746 "could not open external image: 'not-a-real-file.png' (document directory: '/tmp')\n " +
@@ -41,7 +50,7 @@ def test_error_is_raised_if_relative_uri_cannot_be_opened():
4150
4251
4352def test_error_is_raised_if_file_uri_cannot_be_opened ():
44- files = Files ("/tmp" )
53+ files = Files ("/tmp" , external_file_access = True )
4554 error = assert_raises (InvalidFileReferenceError , lambda : files .open ("file:///not-a-real-file.png" ))
4655 expected_message = "could not open external image: 'file:///not-a-real-file.png' (document directory: '/tmp')\n "
4756 assert str (error ).startswith (expected_message )
0 commit comments