Skip to content

Commit

Permalink
Monkeypatch test to check that GMTCLibNotFoundError is raised properly
Browse files Browse the repository at this point in the history
  • Loading branch information
weiji14 committed Jan 20, 2021
1 parent 20728f9 commit 243ad13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pygmt/clib/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def clib_names(os_name):
elif os_name.startswith("freebsd"): # FreeBSD
libnames = ["libgmt.so"]
else:
raise GMTOSError(f'Operating system "{sys.platform}" not supported.')
raise GMTOSError(f'Operating system "{os_name}" not supported.')
return libnames


Expand Down Expand Up @@ -114,8 +114,9 @@ def clib_full_names(env=None):
lib_fullpath = sp.check_output(
["gmt", "--show-library"], encoding="utf-8"
).rstrip("\n")
assert os.path.exists(lib_fullpath)
yield lib_fullpath
except FileNotFoundError: # command not found
except (FileNotFoundError, AssertionError): # command not found
pass

# Search for DLLs in PATH (done by calling "find_library")
Expand Down
18 changes: 18 additions & 0 deletions pygmt/tests/test_clib_loading.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""
Test the functions that load libgmt
"""
import subprocess
import sys

import pytest
from pygmt.clib.loading import check_libgmt, clib_names, load_libgmt
from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError
Expand All @@ -17,6 +20,21 @@ def test_load_libgmt():
check_libgmt(load_libgmt())


@pytest.mark.skipif(sys.platform == "win32", reason="run on UNIX platforms only")
def test_load_libgmt_fails(monkeypatch):
"""
Test that GMTCLibNotFoundError is raised when GMT's shared library cannot
be found.
"""
with monkeypatch.context() as mpatch:
mpatch.setattr(sys, "platform", "win32") # pretend to be on Windows
mpatch.setattr(
subprocess, "check_output", lambda cmd, encoding: "libfakegmt.so"
)
with pytest.raises(GMTCLibNotFoundError):
check_libgmt(load_libgmt())


def test_load_libgmt_with_a_bad_library_path(monkeypatch):
"Test that loading still works when given a bad library path."
# Set a fake "GMT_LIBRARY_PATH"
Expand Down

0 comments on commit 243ad13

Please sign in to comment.