conftest.py 850 B

123456789101112131415161718192021222324
  1. import builtins
  2. import os
  3. import platform
  4. import re
  5. import unittest.mock
  6. def dyld_find_mock(lib_filename):
  7. # https://github.com/python/cpython/blob/master/Lib/ctypes/macholib/dyld.py#L116
  8. lib_name = re.match(r'^(lib\w+)\.\w+(\.\d+)?$', lib_filename).group(1)
  9. return os.path.join(os.sep, 'usr', 'lib', lib_name + '.dylib')
  10. _BUILTIN_IMPORT = builtins.__import__
  11. def import_mock(name, *args, **kwargs):
  12. if name == 'ctypes.macholib.dyld' and platform.system() != 'Darwin':
  13. dyld_module = unittest.mock.MagicMock()
  14. dyld_module.dyld_find = dyld_find_mock
  15. return dyld_module
  16. return _BUILTIN_IMPORT(name, *args, **kwargs)
  17. # required for tests/dlinfo_macosx_mock_test.py and doctests in dlinfo/_macosx.py.
  18. # @pytest.fixture(autouse=True) does not run before import statements.
  19. builtins.__import__ = import_mock