Skip to content
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
3 changes: 2 additions & 1 deletion pygmt/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import time
import webbrowser
from collections.abc import Iterable, Mapping, Sequence
from pathlib import Path
from typing import Any, Literal

import xarray as xr
Expand Down Expand Up @@ -580,7 +581,7 @@ def launch_external_viewer(fname: str, waiting: float = 0):
case "win32": # Windows
os.startfile(fname) # type:ignore[attr-defined] # noqa: S606
case _: # Fall back to the browser if can't recognize the operating system.
webbrowser.open_new_tab(f"file://{fname}")
webbrowser.open_new_tab(f"file://{Path(fname).resolve()}")
if waiting > 0:
# Preview images will be deleted when a GMT modern-mode session ends, but the
# external viewer program may take a few seconds to open the images.
Expand Down
11 changes: 7 additions & 4 deletions pygmt/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,16 @@ def test_launch_external_viewer_win32():
mock_startfile.assert_called_once_with("preview.png")


def test_launch_external_viewer_unknown_os():
@pytest.mark.parametrize("fname", ["preview.png", "/full/path/to/preview.png"])
def test_launch_external_viewer_unknown_os(fname):
"""
Test that launch_external_viewer uses the webbrowser module as a fallback.
"""
with (
patch("webbrowser.open_new_tab") as mock_open,
patch("sys.platform", "unknown"),
patch("webbrowser.open_new_tab") as mock_open,
):
launch_external_viewer("preview.png")
mock_open.assert_called_once_with("file://preview.png")
launch_external_viewer(fname)
fullpath = Path(fname).resolve()
assert fullpath.is_absolute()
mock_open.assert_called_once_with(f"file://{fullpath}")