Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow printing show_versions() to in-memory buffer to enable testing #2399

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()