Skip to content

Commit bc7779d

Browse files
authored
Allow printing show_versions() to in-memory buffer to enable testing (#2399)
To increase code coverage of `__init__.py`, check the output of `pygmt.show_versions()` that is printed to an in-memory string buffer instead of stdout. Adapted from https://github.com/xarray-contrib/xbatcher/blob/v0.2.0/xbatcher/tests/test_print_versions.py
1 parent 8d165c1 commit bc7779d

2 files changed

Lines changed: 31 additions & 13 deletions

File tree

pygmt/__init__.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
lines, vectors, polygons, and symbols (pre-defined and customized).
1818
- Generating publication-quality illustrations and making animations.
1919
"""
20-
2120
import atexit as _atexit
21+
import sys
2222
from importlib.metadata import version
2323

2424
from pygmt import clib
@@ -80,7 +80,7 @@
8080
_atexit.register(_end)
8181

8282

83-
def print_clib_info():
83+
def print_clib_info(file=sys.stdout):
8484
"""
8585
Print information about the GMT shared library that we can find.
8686
@@ -93,10 +93,10 @@ def print_clib_info():
9393
with Session() as ses:
9494
for key in sorted(ses.info):
9595
lines.append(f" {key}: {ses.info[key]}")
96-
print("\n".join(lines))
96+
print("\n".join(lines), file=file)
9797

9898

99-
def show_versions():
99+
def show_versions(file=sys.stdout):
100100
"""
101101
Print various dependency versions which are useful when submitting bug
102102
reports.
@@ -112,7 +112,6 @@ def show_versions():
112112
import importlib
113113
import platform
114114
import subprocess
115-
import sys
116115

117116
def _get_module_version(modname):
118117
"""
@@ -168,19 +167,19 @@ def _get_ghostscript_version():
168167
"geopandas",
169168
]
170169

171-
print("PyGMT information:")
172-
print(f" version: {__version__}")
170+
print("PyGMT information:", file=file)
171+
print(f" version: {__version__}", file=file)
173172

174-
print("System information:")
173+
print("System information:", file=file)
175174
for key, val in sys_info.items():
176-
print(f" {key}: {val}")
175+
print(f" {key}: {val}", file=file)
177176

178-
print("Dependency information:")
177+
print("Dependency information:", file=file)
179178
for modname in deps:
180-
print(f" {modname}: {_get_module_version(modname)}")
181-
print(f" ghostscript: {_get_ghostscript_version()}")
179+
print(f" {modname}: {_get_module_version(modname)}", file=file)
180+
print(f" ghostscript: {_get_ghostscript_version()}", file=file)
182181

183-
print_clib_info()
182+
print_clib_info(file=file)
184183

185184

186185
def test(doctest=True, verbose=True, coverage=False, figures=True):

pygmt/tests/test_init.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Test functions in __init__.
3+
"""
4+
import io
5+
6+
import pygmt
7+
8+
9+
def test_show_versions():
10+
"""
11+
Check that pygmt.show_versions() reports version information from PyGMT,
12+
the operating system, dependencies and the GMT library.
13+
"""
14+
buf = io.StringIO()
15+
pygmt.show_versions(file=buf)
16+
assert "PyGMT information:" in buf.getvalue()
17+
assert "System information:" in buf.getvalue()
18+
assert "Dependency information:" in buf.getvalue()
19+
assert "GMT library information:" in buf.getvalue()

0 commit comments

Comments
 (0)