Skip to content

Commit 6154ede

Browse files
Merge branch 'main' into add-remote-earth-faaerror
2 parents 0e85bf6 + e5efc91 commit 6154ede

File tree

1 file changed

+17
-42
lines changed

1 file changed

+17
-42
lines changed

pygmt/tests/test_clib_loading.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import types
1111
from pathlib import PurePath
12+
from unittest import mock
1213

1314
import pytest
1415
from pygmt.clib.loading import (
@@ -70,24 +71,15 @@ def test_load_libgmt():
7071
check_libgmt(load_libgmt())
7172

7273

73-
def test_load_libgmt_fails(monkeypatch):
74+
def test_load_libgmt_fails():
7475
"""
7576
Test that GMTCLibNotFoundError is raised when GMT's shared library cannot be found.
7677
"""
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+
):
9183
with pytest.raises(GMTCLibNotFoundError):
9284
check_libgmt(load_libgmt())
9385

@@ -214,42 +206,25 @@ def test_brokenlib_brokenlib_workinglib(self):
214206
assert check_libgmt(load_libgmt(lib_fullnames=lib_fullnames)) is None
215207

216208

217-
class TestLibgmtCount:
209+
def test_libgmt_load_counter():
218210
"""
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.
220212
"""
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
242216
with Session() as lib:
243217
_ = lib
244218
with Session() as lib:
245219
_ = 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
247222

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.
249224
load_libgmt()
250-
assert self.counter == 1
225+
assert mock_cdll.call_count == 1
251226
load_libgmt()
252-
assert self.counter == 2
227+
assert mock_cdll.call_count == 2
253228

254229

255230
###############################################################################

0 commit comments

Comments
 (0)