Skip to content

Commit

Permalink
plots: Fix match_defs_renderers when plots are defined in nt format.
Browse files Browse the repository at this point in the history
`plots.show` returned `definitions` in native `nt` format but sources in `posixpath` format so the matching was failing.

Closes iterative#8689

- Add `test_show_plots_defined_with_native_os_path`.
  • Loading branch information
daavoo committed Mar 6, 2023
1 parent cb9d352 commit dde456c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
14 changes: 9 additions & 5 deletions dvc/render/match.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from collections import defaultdict
from typing import TYPE_CHECKING, Dict, List, Optional

import dpath
import dpath.options
from funcy import last

from dvc.repo.plots import infer_data_sources
from dvc.repo.plots import _normpath, infer_data_sources
from dvc.utils.plots import get_plot_id

from .convert import _get_converter
Expand Down Expand Up @@ -43,17 +44,20 @@ def group_definitions(self):

def get_definition_data(self, target_files, rev):
result = {}
for file in target_files:
for definition_file in target_files:
if os.name == "nt":
source_file = _normpath(definition_file).replace("\\", "/")
else:
source_file = definition_file
file_content = (
self.data.get(rev, {})
.get("sources", {})
.get("data", {})
.get(file, {})
.get(source_file, {})
.get("data", {})
)
if file_content:
result[file] = file_content

result[definition_file] = file_content
return result


Expand Down
31 changes: 31 additions & 0 deletions tests/func/plots/test_show.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os

import pytest
Expand Down Expand Up @@ -389,3 +390,33 @@ def test_top_level_plots(
assert content == get_plot(result, "workspace", file=filename)
else:
assert filename not in get_plot(result, "workspace")


def test_show_plots_defined_with_native_os_path(tmp_dir, dvc, scm, capsys):
"""Regression test for #8689"""
top_level_plot = os.path.join("subdir", "top_level_plot.csv")
stage_plot = os.path.join("subdir", "stage_plot.csv")
(tmp_dir / "subdir").mkdir()
(tmp_dir / top_level_plot).write_text("foo,bar\n1,2")
(tmp_dir / stage_plot).write_text("foo,bar\n1,2")
(tmp_dir / "dvc.yaml").dump({"plots": [top_level_plot]})

dvc.stage.add(name="foo", plots=[stage_plot], cmd="echo foo")

plots = dvc.plots.show()

# sources are in posixpath format
sources = plots["workspace"]["sources"]["data"]
assert sources["subdir/top_level_plot.csv"]["data"] == [{"foo": "1", "bar": "2"}]
assert sources["subdir/stage_plot.csv"]["data"] == [{"foo": "1", "bar": "2"}]
# definitions are in native os format
definitions = plots["workspace"]["definitions"]["data"]
assert top_level_plot in definitions["dvc.yaml"]["data"]
assert stage_plot in definitions[""]["data"]

capsys.readouterr()
assert main(["plots", "show", "--json"]) == 0
out, _ = capsys.readouterr()
json_out = json.loads(out)
assert json_out[f"dvc.yaml::{top_level_plot}"]
assert json_out[stage_plot]

0 comments on commit dde456c

Please sign in to comment.