Skip to content

Commit

Permalink
Allow printing show_versions() to in-memory buffer to enable testing (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
weiji14 authored Mar 6, 2023
1 parent 8d165c1 commit bc7779d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
25 changes: 12 additions & 13 deletions pygmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
lines, vectors, polygons, and symbols (pre-defined and customized).
- Generating publication-quality illustrations and making animations.
"""

import atexit as _atexit
import sys
from importlib.metadata import version

from pygmt import clib
Expand Down Expand Up @@ -80,7 +80,7 @@
_atexit.register(_end)


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


def show_versions():
def show_versions(file=sys.stdout):
"""
Print various dependency versions which are useful when submitting bug
reports.
Expand All @@ -112,7 +112,6 @@ def show_versions():
import importlib
import platform
import subprocess
import sys

def _get_module_version(modname):
"""
Expand Down Expand Up @@ -168,19 +167,19 @@ def _get_ghostscript_version():
"geopandas",
]

print("PyGMT information:")
print(f" version: {__version__}")
print("PyGMT information:", file=file)
print(f" version: {__version__}", file=file)

print("System information:")
print("System information:", file=file)
for key, val in sys_info.items():
print(f" {key}: {val}")
print(f" {key}: {val}", file=file)

print("Dependency information:")
print("Dependency information:", file=file)
for modname in deps:
print(f" {modname}: {_get_module_version(modname)}")
print(f" ghostscript: {_get_ghostscript_version()}")
print(f" {modname}: {_get_module_version(modname)}", file=file)
print(f" ghostscript: {_get_ghostscript_version()}", file=file)

print_clib_info()
print_clib_info(file=file)


def test(doctest=True, verbose=True, coverage=False, figures=True):
Expand Down
19 changes: 19 additions & 0 deletions pygmt/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Test functions in __init__.
"""
import io

import pygmt


def test_show_versions():
"""
Check that pygmt.show_versions() reports version information from PyGMT,
the operating system, dependencies and the GMT library.
"""
buf = io.StringIO()
pygmt.show_versions(file=buf)
assert "PyGMT information:" in buf.getvalue()
assert "System information:" in buf.getvalue()
assert "Dependency information:" in buf.getvalue()
assert "GMT library information:" in buf.getvalue()

0 comments on commit bc7779d

Please sign in to comment.