Skip to content

Commit b3f09de

Browse files
committed
Improve tests
1 parent e03e3c1 commit b3f09de

File tree

1 file changed

+95
-60
lines changed

1 file changed

+95
-60
lines changed

pygmt/tests/test_clib_loading.py

Lines changed: 95 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -65,96 +65,131 @@ def test_clib_names():
6565
clib_names("meh")
6666

6767

68-
def test_clib_full_names(monkeypatch):
68+
@pytest.fixture(scope="module", name="os_name")
69+
def fixture_os_name():
6970
"""
70-
Make sure that clib_full_names() returns a generator with expected length
71-
for different cases.
71+
Return the name of the current operating system.
7272
"""
73-
os_name = sys.platform
73+
return sys.platform
7474

75-
# 1. GMT_LIBRARY_PATH and PATH are undefined
75+
76+
@pytest.fixture(scope="module", name="gmt_lib_names")
77+
def fixture_gmt_lib_names(os_name):
78+
"""
79+
Return a list of the library names for the current operating system.
80+
"""
81+
return clib_names(os_name)
82+
83+
84+
@pytest.fixture(scope="module", name="gmt_bin_realpath")
85+
def fixture_gmt_bin_realpath():
86+
"""
87+
Return the real path of GMT's "gmt" command.
88+
"""
89+
return shutil.which("gmt")
90+
91+
92+
@pytest.fixture(scope="module", name="gmt_lib_realpath")
93+
def fixture_gmt_lib_realpath():
94+
"""
95+
Return the real path of the GMT library.
96+
"""
97+
return subprocess.check_output(["gmt", "--show-library"], encoding="utf-8").rstrip(
98+
"\n"
99+
)
100+
101+
102+
def test_clib_full_names_gmt_library_path_undefined_path_empty(
103+
monkeypatch, gmt_lib_names
104+
):
105+
"""
106+
Make sure that clib_full_names() returns a generator with expected names
107+
when GMT_LIBRARY_PATH is undefined and PATH is empty.
108+
"""
76109
with monkeypatch.context() as mpatch:
77110
mpatch.delenv("GMT_LIBRARY_PATH", raising=False)
78111
mpatch.setenv("PATH", "")
79112

80113
lib_fullpaths = clib_full_names()
81114
assert isinstance(lib_fullpaths, types.GeneratorType)
115+
assert list(lib_fullpaths) == gmt_lib_names
82116

83-
lib_fullpaths = list(lib_fullpaths)
84-
if os_name.startswith("linux"):
85-
assert lib_fullpaths == ["libgmt.so"]
86-
elif os_name == "darwin":
87-
assert lib_fullpaths == ["libgmt.dylib"]
88-
elif os_name == "win32":
89-
assert lib_fullpaths == ["gmt.dll", "gmt_w64.dll", "gmt_w32.dll"]
90117

91-
bin_realpath = shutil.which("gmt")
92-
lib_realpath = subprocess.check_output(
93-
[bin_realpath, "--show-library"], encoding="utf-8"
94-
).rstrip("\n")
95-
96-
# 2. GMT_LIBRARY_PATH is defined but PATH is undefined
118+
def test_clib_full_names_gmt_library_path_defined_path_empty(
119+
monkeypatch, gmt_lib_names, gmt_lib_realpath
120+
):
121+
"""
122+
Make sure that clib_full_names() returns a generator with expected names
123+
when GMT_LIBRARY_PATH is defined and PATH is empty.
124+
"""
97125
with monkeypatch.context() as mpatch:
98-
mpatch.setenv("GMT_LIBRARY_PATH", os.path.dirname(lib_realpath))
126+
mpatch.setenv("GMT_LIBRARY_PATH", os.path.dirname(gmt_lib_realpath))
99127
mpatch.setenv("PATH", "")
100128

129+
lib_fullpaths = clib_full_names()
130+
assert isinstance(lib_fullpaths, types.GeneratorType)
131+
assert list(lib_fullpaths) == [gmt_lib_realpath] + gmt_lib_names
132+
133+
134+
def test_clib_full_names_gmt_library_path_undefined_path_included(
135+
monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_realpath, os_name
136+
):
137+
"""
138+
Make sure that clib_full_names() returns a generator with expected names
139+
when GMT_LIBRARY_PATH is undefined and PATH includes GMT's bin path.
140+
"""
141+
with monkeypatch.context() as mpatch:
142+
mpatch.delenv("GMT_LIBRARY_PATH", raising=False)
143+
mpatch.setenv("PATH", os.path.dirname(gmt_bin_realpath))
144+
101145
lib_fullpaths = clib_full_names()
102146
assert isinstance(lib_fullpaths, types.GeneratorType)
103147

104148
lib_fullpaths = list(lib_fullpaths)
105-
if os_name.startswith("linux"):
106-
assert lib_fullpaths == [lib_realpath, "libgmt.so"]
107-
elif os_name == "darwin":
108-
assert lib_fullpaths == [lib_realpath, "libgmt.dylib"]
149+
if os_name.startswith("linux") or os_name == "darwin":
150+
assert lib_fullpaths == [gmt_lib_realpath] + gmt_lib_names
109151
elif os_name == "win32":
110-
assert lib_fullpaths == [
111-
lib_realpath,
112-
"gmt.dll",
113-
"gmt_w64.dll",
114-
"gmt_w32.dll",
115-
]
116-
117-
# 3. GMT_LIBRARY_PATH is undefined but PATH is defined
152+
# On Windows: we also call find_library() to find the library in PATH
153+
assert lib_fullpaths == [gmt_lib_realpath] * 2 + gmt_lib_names
154+
155+
156+
def test_clib_full_names_gmt_library_path_defined_path_included(
157+
monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_realpath, os_name
158+
):
159+
"""
160+
Make sure that clib_full_names() returns a generator with expected names
161+
when GMT_LIBRARY_PATH is defined and PATH includes GMT's bin path.
162+
"""
118163
with monkeypatch.context() as mpatch:
119-
mpatch.delenv("GMT_LIBRARY_PATH", raising=False)
120-
mpatch.setenv("PATH", os.path.dirname(bin_realpath))
164+
mpatch.setenv("GMT_LIBRARY_PATH", os.path.dirname(gmt_lib_realpath))
165+
mpatch.setenv("PATH", os.path.dirname(gmt_bin_realpath))
121166

122167
lib_fullpaths = clib_full_names()
123168
assert isinstance(lib_fullpaths, types.GeneratorType)
124169

125170
lib_fullpaths = list(lib_fullpaths)
126-
if os_name.startswith("linux"):
127-
assert lib_fullpaths == [lib_realpath, "libgmt.so"]
128-
elif os_name == "darwin":
129-
assert lib_fullpaths == [lib_realpath, "libgmt.dylib"]
171+
if os_name.startswith("linux") or os_name == "darwin":
172+
assert lib_fullpaths == [gmt_lib_realpath] * 2 + gmt_lib_names
130173
elif os_name == "win32":
131-
assert lib_fullpaths == [
132-
lib_realpath,
133-
lib_realpath,
134-
"gmt.dll",
135-
"gmt_w64.dll",
136-
"gmt_w32.dll",
137-
]
138-
139-
# 4. both GMT_LIBRARY_PATH and PATH are defined
174+
assert lib_fullpaths == [gmt_lib_realpath] * 3 + gmt_lib_names
175+
176+
177+
def test_clib_full_names_gmt_library_path_incorrect_path_included(
178+
monkeypatch, gmt_lib_names, gmt_lib_realpath, gmt_bin_realpath, os_name
179+
):
180+
"""
181+
Make sure that clib_full_names() returns a generator with expected names
182+
when GMT_LIBRARY_PATH is defined but incorrect and PATH includes GMT's bin path.
183+
"""
140184
with monkeypatch.context() as mpatch:
141-
mpatch.setenv("GMT_LIBRARY_PATH", os.path.dirname(lib_realpath))
142-
mpatch.setenv("PATH", os.path.dirname(bin_realpath))
185+
mpatch.setenv("GMT_LIBRARY_PATH", "/not/a/valid/library/path")
186+
mpatch.setenv("PATH", os.path.dirname(gmt_bin_realpath))
143187

144188
lib_fullpaths = clib_full_names()
145189
assert isinstance(lib_fullpaths, types.GeneratorType)
146190

147191
lib_fullpaths = list(lib_fullpaths)
148-
if os_name.startswith("linux"):
149-
assert lib_fullpaths == [lib_realpath, lib_realpath, "libgmt.so"]
150-
elif os_name == "darwin":
151-
assert lib_fullpaths == [lib_realpath, lib_realpath, "libgmt.dylib"]
192+
if os_name.startswith("linux") or os_name == "darwin":
193+
assert lib_fullpaths == [gmt_lib_realpath] + gmt_lib_names
152194
elif os_name == "win32":
153-
assert lib_fullpaths == [
154-
lib_realpath,
155-
lib_realpath,
156-
lib_realpath,
157-
"gmt.dll",
158-
"gmt_w64.dll",
159-
"gmt_w32.dll",
160-
]
195+
assert lib_fullpaths == [gmt_lib_realpath] * 2 + gmt_lib_names

0 commit comments

Comments
 (0)