Skip to content

Commit

Permalink
launch_external_viewer: Use full path when opening the file in a web …
Browse files Browse the repository at this point in the history
…browser (#3647)
  • Loading branch information
seisman authored Nov 25, 2024
1 parent 6bd5e72 commit a29d513
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
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}")

0 comments on commit a29d513

Please sign in to comment.