|
9 | 9 | import sys
|
10 | 10 | import types
|
11 | 11 | from pathlib import PurePath
|
| 12 | +from unittest import mock |
12 | 13 |
|
13 | 14 | import pytest
|
14 | 15 | from pygmt.clib.loading import (
|
@@ -70,24 +71,15 @@ def test_load_libgmt():
|
70 | 71 | check_libgmt(load_libgmt())
|
71 | 72 |
|
72 | 73 |
|
73 |
| -def test_load_libgmt_fails(monkeypatch): |
| 74 | +def test_load_libgmt_fails(): |
74 | 75 | """
|
75 | 76 | Test that GMTCLibNotFoundError is raised when GMT's shared library cannot be found.
|
76 | 77 | """
|
77 |
| - with monkeypatch.context() as mpatch: |
78 |
| - if sys.platform == "win32": |
79 |
| - mpatch.setattr(ctypes.util, "find_library", lambda name: "fakegmt.dll") # noqa: ARG005 |
80 |
| - mpatch.setattr( |
81 |
| - sys, |
82 |
| - "platform", |
83 |
| - # Pretend to be on macOS if running on Linux, and vice versa |
84 |
| - "darwin" if sys.platform == "linux" else "linux", |
85 |
| - ) |
86 |
| - mpatch.setattr( |
87 |
| - subprocess, |
88 |
| - "check_output", |
89 |
| - lambda cmd, encoding: "libfakegmt.so", # noqa: ARG005 |
90 |
| - ) |
| 78 | + with ( |
| 79 | + mock.patch("ctypes.util.find_library", return_value="fakegmt.dll"), |
| 80 | + mock.patch("sys.platform", "darwin" if sys.platform == "linux" else "linux"), |
| 81 | + mock.patch("subprocess.check_output", return_value="libfakegmt.so"), |
| 82 | + ): |
91 | 83 | with pytest.raises(GMTCLibNotFoundError):
|
92 | 84 | check_libgmt(load_libgmt())
|
93 | 85 |
|
@@ -214,42 +206,25 @@ def test_brokenlib_brokenlib_workinglib(self):
|
214 | 206 | assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
|
215 | 207 |
|
216 | 208 |
|
217 |
| -class TestLibgmtCount: |
| 209 | +def test_libgmt_load_counter(): |
218 | 210 | """
|
219 |
| - Test that the GMT library is not repeatedly loaded in every session. |
| 211 | + Make sure that the GMT library is not loaded in every session. |
220 | 212 | """
|
221 |
| - |
222 |
| - loaded_libgmt = load_libgmt() # Load the GMT library and reuse it when necessary |
223 |
| - counter = 0 # Global counter for how many times ctypes.CDLL is called |
224 |
| - |
225 |
| - def _mock_ctypes_cdll_return(self, libname): # noqa: ARG002 |
226 |
| - """ |
227 |
| - Mock ctypes.CDLL to count how many times the function is called. |
228 |
| -
|
229 |
| - If ctypes.CDLL is called, the counter increases by one. |
230 |
| - """ |
231 |
| - self.counter += 1 # Increase the counter |
232 |
| - return self.loaded_libgmt |
233 |
| - |
234 |
| - def test_libgmt_load_counter(self, monkeypatch): |
235 |
| - """ |
236 |
| - Make sure that the GMT library is not loaded in every session. |
237 |
| - """ |
238 |
| - # Monkeypatch the ctypes.CDLL function |
239 |
| - monkeypatch.setattr(ctypes, "CDLL", self._mock_ctypes_cdll_return) |
240 |
| - |
241 |
| - # Create two sessions and check the global counter |
| 213 | + loaded_libgmt = load_libgmt() # Load the GMT library and reuse it when necessary. |
| 214 | + with mock.patch("ctypes.CDLL", return_value=loaded_libgmt) as mock_cdll: |
| 215 | + # Create two sessions and check the call count |
242 | 216 | with Session() as lib:
|
243 | 217 | _ = lib
|
244 | 218 | with Session() as lib:
|
245 | 219 | _ = lib
|
246 |
| - assert self.counter == 0 # ctypes.CDLL is not called after two sessions. |
| 220 | + # ctypes.CDLL is not called after two sessions. |
| 221 | + assert mock_cdll.call_count == 0 |
247 | 222 |
|
248 |
| - # Explicitly calling load_libgmt to make sure the mock function is correct |
| 223 | + # Explicitly calling load_libgmt to make sure the mock function is correct. |
249 | 224 | load_libgmt()
|
250 |
| - assert self.counter == 1 |
| 225 | + assert mock_cdll.call_count == 1 |
251 | 226 | load_libgmt()
|
252 |
| - assert self.counter == 2 |
| 227 | + assert mock_cdll.call_count == 2 |
253 | 228 |
|
254 | 229 |
|
255 | 230 | ###############################################################################
|
|
0 commit comments