plots: Fix match_defs_renderers when plots are defined in nt format.

`plots.show` returned `definitions` in native `nt` format but sources in `posixpath` format so the matching was failing.

Closes #8689

- Add `test_show_plots_defined_with_native_os_path`.
This commit is contained in:
David de la Iglesia Castro
2023-03-06 19:16:29 +01:00
parent cb9d3529af
commit dde456c923
2 changed files with 40 additions and 5 deletions
+9 -5
View File
@@ -1,3 +1,4 @@
import os
from collections import defaultdict from collections import defaultdict
from typing import TYPE_CHECKING, Dict, List, Optional from typing import TYPE_CHECKING, Dict, List, Optional
@@ -5,7 +6,7 @@ import dpath
import dpath.options import dpath.options
from funcy import last 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 dvc.utils.plots import get_plot_id
from .convert import _get_converter from .convert import _get_converter
@@ -43,17 +44,20 @@ class PlotsData:
def get_definition_data(self, target_files, rev): def get_definition_data(self, target_files, rev):
result = {} 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 = ( file_content = (
self.data.get(rev, {}) self.data.get(rev, {})
.get("sources", {}) .get("sources", {})
.get("data", {}) .get("data", {})
.get(file, {}) .get(source_file, {})
.get("data", {}) .get("data", {})
) )
if file_content: if file_content:
result[file] = file_content result[definition_file] = file_content
return result return result
+31
View File
@@ -1,3 +1,4 @@
import json
import os import os
import pytest import pytest
@@ -389,3 +390,33 @@ def test_top_level_plots(
assert content == get_plot(result, "workspace", file=filename) assert content == get_plot(result, "workspace", file=filename)
else: else:
assert filename not in get_plot(result, "workspace") 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]