|
1 | 1 | """
|
2 | 2 | Test the functions that load libgmt.
|
3 | 3 | """
|
| 4 | +import shutil |
4 | 5 | import subprocess
|
5 | 6 | import sys
|
| 7 | +import types |
| 8 | +from pathlib import PurePath |
6 | 9 |
|
7 | 10 | import pytest
|
8 |
| -from pygmt.clib.loading import check_libgmt, clib_names, load_libgmt |
| 11 | +from pygmt.clib.loading import check_libgmt, clib_full_names, clib_names, load_libgmt |
9 | 12 | from pygmt.exceptions import GMTCLibError, GMTCLibNotFoundError, GMTOSError
|
10 | 13 |
|
11 | 14 |
|
@@ -73,3 +76,122 @@ def test_clib_names():
|
73 | 76 | assert clib_names(freebsd) == ["libgmt.so"]
|
74 | 77 | with pytest.raises(GMTOSError):
|
75 | 78 | clib_names("meh")
|
| 79 | + |
| 80 | + |
| 81 | +############################################################################### |
| 82 | +# Tests for clib_full_names |
| 83 | +@pytest.fixture(scope="module", name="gmt_lib_names") |
| 84 | +def fixture_gmt_lib_names(): |
| 85 | + """ |
| 86 | + Return a list of the library names for the current operating system. |
| 87 | + """ |
| 88 | + return clib_names(sys.platform) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.fixture(scope="module", name="gmt_bin_dir") |
| 92 | +def fixture_gmt_bin_dir(): |
| 93 | + """ |
| 94 | + Return GMT's bin directory. |
| 95 | + """ |
| 96 | + return str(PurePath(shutil.which("gmt")).parent) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.fixture(scope="module", name="gmt_lib_realpath") |
| 100 | +def fixture_gmt_lib_realpath(): |
| 101 | + """ |
| 102 | + Return the real path of the GMT library. |
| 103 | + """ |
| 104 | + lib_realpath = subprocess.check_output( |
| 105 | + ["gmt", "--show-library"], encoding="utf-8" |
| 106 | + ).rstrip("\n") |
| 107 | + # On Windows, clib_full_names() returns paths with separator "\\", |
| 108 | + # but "gmt --show-library" returns paths with separator "/". |
| 109 | + # Use `str(PurePath(realpath)` to mimic the behavior of clib_full_names() |
| 110 | + return str(PurePath(lib_realpath)) |
| 111 | + |
| 112 | + |
| 113 | +def test_clib_full_names_gmt_library_path_undefined_path_empty( |
| 114 | + monkeypatch, gmt_lib_names |
| 115 | +): |
| 116 | + """ |
| 117 | + Make sure that clib_full_names() returns a generator with expected names |
| 118 | + when GMT_LIBRARY_PATH is undefined and PATH is empty. |
| 119 | + """ |
| 120 | + with monkeypatch.context() as mpatch: |
| 121 | + mpatch.delenv("GMT_LIBRARY_PATH", raising=False) |
| 122 | + mpatch.setenv("PATH", "") |
| 123 | + lib_fullpaths = clib_full_names() |
| 124 | + |
| 125 | + assert isinstance(lib_fullpaths, types.GeneratorType) |
| 126 | + assert list(lib_fullpaths) == gmt_lib_names |
| 127 | + |
| 128 | + |
| 129 | +def test_clib_full_names_gmt_library_path_defined_path_empty( |
| 130 | + monkeypatch, gmt_lib_names, gmt_lib_realpath |
| 131 | +): |
| 132 | + """ |
| 133 | + Make sure that clib_full_names() returns a generator with expected names |
| 134 | + when GMT_LIBRARY_PATH is defined and PATH is empty. |
| 135 | + """ |
| 136 | + with monkeypatch.context() as mpatch: |
| 137 | + mpatch.setenv("GMT_LIBRARY_PATH", str(PurePath(gmt_lib_realpath).parent)) |
| 138 | + mpatch.setenv("PATH", "") |
| 139 | + lib_fullpaths = clib_full_names() |
| 140 | + |
| 141 | + assert isinstance(lib_fullpaths, types.GeneratorType) |
| 142 | + assert list(lib_fullpaths) == [gmt_lib_realpath] + gmt_lib_names |
| 143 | + |
| 144 | + |
| 145 | +def test_clib_full_names_gmt_library_path_undefined_path_included( |
| 146 | + monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_dir |
| 147 | +): |
| 148 | + """ |
| 149 | + Make sure that clib_full_names() returns a generator with expected names |
| 150 | + when GMT_LIBRARY_PATH is undefined and PATH includes GMT's bin path. |
| 151 | + """ |
| 152 | + with monkeypatch.context() as mpatch: |
| 153 | + mpatch.delenv("GMT_LIBRARY_PATH", raising=False) |
| 154 | + mpatch.setenv("PATH", gmt_bin_dir) |
| 155 | + lib_fullpaths = clib_full_names() |
| 156 | + |
| 157 | + assert isinstance(lib_fullpaths, types.GeneratorType) |
| 158 | + # Windows: find_library() searches the library in PATH, so one more |
| 159 | + npath = 2 if sys.platform == "win32" else 1 |
| 160 | + assert list(lib_fullpaths) == [gmt_lib_realpath] * npath + gmt_lib_names |
| 161 | + |
| 162 | + |
| 163 | +def test_clib_full_names_gmt_library_path_defined_path_included( |
| 164 | + monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_dir |
| 165 | +): |
| 166 | + """ |
| 167 | + Make sure that clib_full_names() returns a generator with expected names |
| 168 | + when GMT_LIBRARY_PATH is defined and PATH includes GMT's bin path. |
| 169 | + """ |
| 170 | + with monkeypatch.context() as mpatch: |
| 171 | + mpatch.setenv("GMT_LIBRARY_PATH", str(PurePath(gmt_lib_realpath).parent)) |
| 172 | + mpatch.setenv("PATH", gmt_bin_dir) |
| 173 | + lib_fullpaths = clib_full_names() |
| 174 | + |
| 175 | + assert isinstance(lib_fullpaths, types.GeneratorType) |
| 176 | + # Windows: find_library() searches the library in PATH, so one more |
| 177 | + npath = 3 if sys.platform == "win32" else 2 |
| 178 | + assert list(lib_fullpaths) == [gmt_lib_realpath] * npath + gmt_lib_names |
| 179 | + |
| 180 | + |
| 181 | +def test_clib_full_names_gmt_library_path_incorrect_path_included( |
| 182 | + monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_dir |
| 183 | +): |
| 184 | + """ |
| 185 | + Make sure that clib_full_names() returns a generator with expected names |
| 186 | + when GMT_LIBRARY_PATH is defined but incorrect and PATH includes GMT's bin |
| 187 | + path. |
| 188 | + """ |
| 189 | + with monkeypatch.context() as mpatch: |
| 190 | + mpatch.setenv("GMT_LIBRARY_PATH", "/not/a/valid/library/path") |
| 191 | + mpatch.setenv("PATH", gmt_bin_dir) |
| 192 | + lib_fullpaths = clib_full_names() |
| 193 | + |
| 194 | + assert isinstance(lib_fullpaths, types.GeneratorType) |
| 195 | + # Windows: find_library() searches the library in PATH, so one more |
| 196 | + npath = 2 if sys.platform == "win32" else 1 |
| 197 | + assert list(lib_fullpaths) == [gmt_lib_realpath] * npath + gmt_lib_names |
0 commit comments