MCPcopy Create free account
hub / github.com/pymupdf/PyMuPDF / test_open

Function test_open

tests/test_general.py:1364–1461  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1362
1363
1364def test_open():
1365
1366 import re
1367 import textwrap
1368 import traceback
1369
1370 resources = relpath(os.path.abspath(f'{__file__}/../../tests/resources'))
1371
1372 # We convert all strings to use `/` instead of os.sep, which avoids
1373 # problems with regex's on windows.
1374 resources = resources.replace(os.sep, '/')
1375
1376 def check(filename=None, stream=None, filetype=None, exception=None):
1377 '''
1378 Checks we receive expected exception if specified.
1379 '''
1380 if isinstance(filename, str):
1381 filename = filename.replace(os.sep, '/')
1382 if exception:
1383 etype, eregex = exception
1384 if isinstance(eregex, (tuple, list)):
1385 # Treat as sequence of regexes to look for.
1386 eregex = '.*'.join(eregex)
1387 try:
1388 pymupdf.open(filename=filename, stream=stream, filetype=filetype)
1389 except etype as e:
1390 text = traceback.format_exc(limit=0)
1391 text = text.replace(os.sep, '/')
1392 text = textwrap.indent(text, ' ', lambda line: 1)
1393 assert re.search(eregex, text, re.DOTALL), \
1394 f'Incorrect exception text, expected {eregex=}, received:\n{text}'
1395 print(f'Received expected exception for {filename=} {stream=} {filetype=}:\n{text}')
1396 except Exception as e:
1397 assert 0, \
1398 f'Incorrect exception, expected {etype}, received {type(e)=}.'
1399 else:
1400 assert 0, f'Did not received exception, expected {etype=}. {filename=} {stream=} {filetype=} {exception=}'
1401 else:
1402 document = pymupdf.open(filename=filename, stream=stream, filetype=filetype)
1403 return document
1404
1405 check(f'{resources}/1.pdf')
1406
1407 check(f'{resources}/Bezier.epub')
1408
1409 path = 1234
1410 etype = TypeError
1411 eregex = re.escape(f'bad filename: type(filename)=<class \'int\'> filename={path}.')
1412 check(path, exception=(etype, eregex))
1413
1414 path = 'test_open-this-file-will-not-exist'
1415 etype = pymupdf.FileNotFoundError
1416 eregex = f'no such file: \'{path}\''
1417 check(path, exception=(etype, eregex))
1418
1419 path = resources
1420 etype = pymupdf.FileDataError
1421 eregex = re.escape(f'\'{path}\' is no file')

Callers

nothing calls this directly

Calls 2

relpathFunction · 0.70
checkFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…