From 243ad13d094431e1d977d7aea0dfd0dc94609787 Mon Sep 17 00:00:00 2001 From: Wei Ji Date: Wed, 20 Jan 2021 16:56:29 +1300 Subject: [PATCH] Monkeypatch test to check that GMTCLibNotFoundError is raised properly --- pygmt/clib/loading.py | 5 +++-- pygmt/tests/test_clib_loading.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pygmt/clib/loading.py b/pygmt/clib/loading.py index f6ffa681073..ffd85575d49 100644 --- a/pygmt/clib/loading.py +++ b/pygmt/clib/loading.py @@ -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 @@ -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") diff --git a/pygmt/tests/test_clib_loading.py b/pygmt/tests/test_clib_loading.py index ff6ed0692a0..44c4dfd4cff 100644 --- a/pygmt/tests/test_clib_loading.py +++ b/pygmt/tests/test_clib_loading.py @@ -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 @@ -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"