def test_cygwin(self, mock_os, mock_load_library, mock_find_library, mock_open): """Tests finding the DLL when running within Cygwin. Args: self (TestLibrary): the ``TestLibrary`` instance mock_os (Mock): a mocked version of the ``os`` module mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): a mocked call to ``ctypes`` find library mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = None directories = [ 'C:\\Program Files (x86)\\SEGGER\\JLinkARM\\JLinkARM.dll', 'C:\\Program Files (x86)\\SEGGER\\JLink_V500l\\JLinkARM.dll' ] self.mock_directories(mock_os, directories, '\\') lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with(library.Library.WINDOWS_32_JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) self.assertEqual(1, mock_load_library.call_count)
def test_dll_getter(self, mock_load_library, mock_find_library, mock_open): """Tests that the ``.dll()`` getter returns the set ``DLL``. Args: self (TestLibrary): the ``TestLibrary`` instance mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = self.lib_path mock_load_library.return_value = 0xDEADBEEF lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) mock_open.assert_called_with(self.lib_path, 'rb') mock_load_library.assert_called_once() self.assertEqual(0xDEADBEEF, lib.dll())
def test_darwin_empty(self, mock_os, mock_load_library, mock_find_library, mock_open): """Tests finding the DLL on Darwin through the SEGGER application for V6.0.0+. Args: self (TestLibrary): the ``TestLibrary`` instance mock_os (Mock): a mocked version of the ``os`` module mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): a mocked call to ``ctypes`` find library mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = None directories = [ '/Applications/SEGGER/JLink/' ] self.mock_directories(mock_os, directories, '/') lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) self.assertEqual(0, mock_load_library.call_count)
def test_initialize_windows_32bit(self, mock_ctypes, mock_find_library, mock_open): """Tests creating a library on a Windows machine with 32bit Python. Args: self (TestLibrary): the ``TestLibrary`` instance mock_ctypes (Mock): a mocked version of the ctypes library mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_windll = mock.Mock() mock_windll.__getitem__ = mock.Mock() mock_cdll = mock.Mock() mock_cdll.__getitem__ = mock.Mock() mock_ctypes.windll = mock_windll mock_ctypes.cdll = mock_cdll mock_find_library.return_value = self.lib_path lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with(library.Library.WINDOWS_32_JLINK_SDK_NAME) mock_open.assert_called_with(self.lib_path, 'rb') mock_cdll.LoadLibrary.assert_called_once() mock_windll.LoadLibrary.assert_called_once()
def test_linux_4_98_e(self, mock_os, mock_load_library, mock_find_library, mock_open, mock_is_os_64bit): """Tests finding the DLL on Linux through the SEGGER application for V4.98E-. Args: self (TestLibrary): the ``TestLibrary`` instance mock_os (Mock): a mocked version of the ``os`` module mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): a mocked call to ``ctypes`` find library mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = None directories = [ '/opt/SEGGER/JLink_Linux_V498e_i386/libjlinkarm.so', ] self.mock_directories(mock_os, directories, '/') lib = library.Library() lib.unload = mock.Mock() load_library_args, load_libary_kwargs = mock_load_library.call_args self.assertEqual(directories[0], lib._path)
def test_initialiaze_no(self, mock_isdir, mock_load_library, mock_find_library, mock_open): """Tests creating a library when the default DLL does not exist. Args: self (TestLibrary): the ``TestLibrary`` instance mock_isdir (Mock): mock for mocking the call to ``os.path.isdir`` mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the clal to ``open()`` Returns: ``None`` """ mock_isdir.return_value = False mock_find_library.return_value = None lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with( library.Library.JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) self.assertEqual(0, mock_load_library.call_count)
def test_linux_6_10_0_32bit(self, mock_os, mock_load_library, mock_find_library, mock_open, mock_is_os_64bit): """Tests finding the DLL on Linux through the SEGGER application for V6.0.0+ on 32 bit linux. Args: self (TestLibrary): the ``TestLibrary`` instance mock_os (Mock): a mocked version of the ``os`` module mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): a mocked call to ``ctypes`` find library mock_open (Mock): mock for mocking the call to ``open()`` mock_is_os_64bit (Mock): mock for mocking the call to ``is_os_64bit``, returns False Returns: ``None`` """ mock_find_library.return_value = None directories = [ '/opt/SEGGER/JLink_Linux_V610d_x86_64/libjlinkarm_x86.so.6.10', '/opt/SEGGER/JLink_Linux_V610d_x86_64/libjlinkarm.so.6.10', ] self.mock_directories(mock_os, directories, '/') lib = library.Library() lib.unload = mock.Mock() load_library_args, load_libary_kwargs = mock_load_library.call_args self.assertEqual(directories[0], lib._path) directories = [ '/opt/SEGGER/JLink_Linux_V610d_x86_64/libjlinkarm.so.6.10', ] self.mock_directories(mock_os, directories, '/') lib = library.Library() lib.unload = mock.Mock() load_library_args, load_libary_kwargs = mock_load_library.call_args self.assertEqual(None, lib._path)
def test_unload_windows(self, mock_remove, mock_ctypes): """Tests unloading the library on Windows. Args: self (TestLibrary): the ``TestLibrary`` instance mock_remove (Mock): the mocked call to ``os.remove()`` mock_ctypes (Mock): mocked ``ctypes`` module Returns: ``None`` """ lib = library.Library('') self.assertTrue(lib.unload()) self.assertEqual(2, mock_ctypes.windll.kernel32.FreeLibrary.call_count) mock_remove.assert_called_once()
def test_unload_no_library(self, mock_remove, mock_ctypes): """Tests unloading the library when no DLL is loaded. Args: self (TestLibrary): the ``TestLibrary`` instance mock_remove (Mock): the mocked call to ``os.remove()`` mock_ctypes (Mock): mocked ``ctypes`` module Returns: ``None`` """ lib = library.Library('') setattr(lib, '_lib', None) setattr(lib, '_temp', None) self.assertFalse(lib.unload()) mock_remove.assert_not_called()
def test_unload_darwin_linux(self, mock_remove, mock_ctypes): """Tests unloading the library on Darwin and Linux platforms. Args: self (TestLibrary): the ``TestLibrary`` instance mock_remove (Mock): the mocked call to ``os.remove()`` mock_ctypes (Mock): mocked ``ctypes`` module Returns: ``None`` """ lib = library.Library('') self.assertTrue(lib.unload()) mock_remove.assert_called_once() self.assertEqual(None, lib._lib) self.assertEqual(None, lib._temp)
def test_initialize_default(self, mock_load_library, mock_find_library, mock_open): """Tests creating a library and finding the default DLL. Args: self (TestLibrary): the ``TestLibrary`` instance mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = self.lib_path lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with(library.Library.JLINK_SDK_NAME) mock_open.assert_called_with(self.lib_path, 'rb') mock_load_library.assert_called_once()
def test_initialize_with_path(self, mock_load_library, mock_find_library, mock_open): """Tests creating a library when passing in a DLL path. Args: self (TestLibrary): the ``TestLibrary`` instance mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = None lib = library.Library(self.lib_path) lib.unload = mock.Mock() self.assertEqual(0, mock_find_library.call_count) mock_open.assert_called_with(self.lib_path, 'rb') mock_load_library.assert_called_once()
def test_windows_jlinkarm(self, mock_os, mock_ctypes, mock_find_library, mock_open): """Tests finding the DLL on Windows through the SEGGER JLinkARM folder. Args: self (TestLibrary): the ``TestLibrary`` instance mock_os (Mock): a mocked version of the ``os`` module mock_ctypes (Mock): a mocked version of the ctypes library mock_find_library (Mock): a mocked call to ``ctypes`` find library mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_windll = mock.Mock() mock_windll.__getitem__ = mock.Mock() mock_cdll = mock.Mock() mock_cdll.__getitem__ = mock.Mock() mock_ctypes.windll = mock_windll mock_ctypes.cdll = mock_cdll mock_find_library.return_value = None directories = [ 'C:\\Program Files (x86)\\SEGGER\\JLinkARM\\JLinkARM.dll' ] self.mock_directories(mock_os, directories, '\\') lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with( library.Library.WINDOWS_JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) self.assertEqual(1, mock_windll.LoadLibrary.call_count) self.assertEqual(1, mock_cdll.LoadLibrary.call_count)
def test_load(self, mock_load_library, mock_find_library, mock_open): """Tests that we can pass in a path to a DLL to load. If the path is valid, loads the given ``DLL``, otherwise stays the same. Args: self (TestLibrary): the ``TestLibrary`` instance mock_load_library (Mock): a mocked version of the library loader mock_find_library (Mock): mock for mocking the ``ctypes.util.find_library()`` call mock_open (Mock): mock for mocking the call to ``open()`` Returns: ``None`` """ mock_find_library.return_value = self.lib_path lib = library.Library() lib.unload = mock.Mock() mock_find_library.assert_called_once_with( library.Library.JLINK_SDK_NAME) self.assertEqual(1, mock_find_library.call_count) mock_open.assert_called_with(self.lib_path, 'rb') self.assertEqual(1, mock_load_library.call_count) new_path = '\\' lib.load(new_path) mock_open.assert_called_with(new_path, 'rb') self.assertEqual(2, mock_load_library.call_count) lib.load(None) mock_open.assert_called_with(new_path, 'rb') self.assertEqual(3, mock_load_library.call_count)