Skip to content

Commit

Permalink
plots: Use URI for --open.
Browse files Browse the repository at this point in the history
We were previously using relpath in *all* cases, mainly to support WSL but relpath doesn't work
on macOS.

Added a check for using relpath *only* for WSL.

Fixes #6092
  • Loading branch information
daavoo committed Oct 27, 2021
1 parent b652670 commit ed59d7e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion dvc/command/plots.py
Expand Up @@ -68,8 +68,12 @@ def run(self):

if self.args.open:
import webbrowser
from platform import uname

opened = webbrowser.open(index_path)
if "Microsoft" in uname().release:
url = Path(rel) / "index.html"

opened = webbrowser.open(url)
if not opened:
ui.error_write(
"Failed to open. Please try opening it manually."
Expand Down
24 changes: 22 additions & 2 deletions tests/unit/command/test_plots.py
@@ -1,5 +1,6 @@
import os
import posixpath
from pathlib import Path

import pytest

Expand Down Expand Up @@ -149,12 +150,31 @@ def test_plots_diff_open(tmp_dir, dvc, mocker, capsys, plots_data):
mocker.patch("dvc.command.plots.render", return_value=index_path)

assert cmd.run() == 0
mocked_open.assert_called_once_with(index_path)
mocked_open.assert_called_once_with(index_path.as_uri())

out, _ = capsys.readouterr()
assert index_path.as_uri() in out


def test_plots_diff_open_WSL(tmp_dir, dvc, mocker, plots_data):
mocked_open = mocker.patch("webbrowser.open", return_value=True)
mocked_uname_result = mocker.MagicMock()
mocked_uname_result.release = "Microsoft"
mocker.patch("platform.uname", return_value=mocked_uname_result)

cli_args = parse_args(
["plots", "diff", "--targets", "plots.csv", "--open"]
)
cmd = cli_args.func(cli_args)
mocker.patch("dvc.repo.plots.diff.diff", return_value=plots_data)

index_path = tmp_dir / "dvc_plots" / "index.html"
mocker.patch("dvc.command.plots.render", return_value=index_path)

assert cmd.run() == 0
mocked_open.assert_called_once_with(Path("dvc_plots") / "index.html")


def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys, plots_data):
mocked_open = mocker.patch("webbrowser.open", return_value=False)
cli_args = parse_args(
Expand All @@ -167,7 +187,7 @@ def test_plots_diff_open_failed(tmp_dir, dvc, mocker, capsys, plots_data):

assert cmd.run() == 1
expected_url = tmp_dir / "dvc_plots" / "index.html"
mocked_open.assert_called_once_with(expected_url)
mocked_open.assert_called_once_with(expected_url.as_uri())

error_message = "Failed to open. Please try opening it manually."

Expand Down

0 comments on commit ed59d7e

Please sign in to comment.