1
0

dlinfo_macosx_mock_test.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import ctypes
  2. import ctypes.util
  3. import importlib
  4. import os
  5. import types
  6. import unittest.mock
  7. import pytest
  8. import dlinfo
  9. # pylint: disable=redefined-outer-name
  10. @pytest.fixture
  11. def dlinfo_module_mac() -> types.ModuleType:
  12. with unittest.mock.patch('sys.platform', 'darwin'):
  13. dlinfo_module = importlib.reload(dlinfo)
  14. assert dlinfo_module.DLInfo.__module__ == 'dlinfo._macosx'
  15. return dlinfo_module
  16. @pytest.mark.parametrize('lib_name', [
  17. 'SegFault',
  18. 'c',
  19. 'dl',
  20. 'python_grasp',
  21. ])
  22. def test_dlinfo_path(dlinfo_module_mac, lib_name):
  23. lib_filename = ctypes.util.find_library(lib_name)
  24. if not lib_filename:
  25. pytest.xfail(f"lib{lib_name} not found")
  26. lib = ctypes.cdll.LoadLibrary(lib_filename)
  27. lib_info = dlinfo_module_mac.DLInfo(lib)
  28. if os.path.exists(lib_info.path): # mac
  29. assert os.path.isabs(lib_info.path)
  30. assert lib_filename == os.path.basename(lib_info.path)
  31. else: # dyld_find mock
  32. assert lib_info.path == f"/usr/lib/lib{lib_name}.dylib"