From dde456c923efefe916d2232b2475da4181bfec50 Mon Sep 17 00:00:00 2001 From: David de la Iglesia Castro Date: Wed, 1 Mar 2023 16:49:52 +0100 Subject: [PATCH] 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`. --- dvc/render/match.py | 14 +++++++++----- tests/func/plots/test_show.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/dvc/render/match.py b/dvc/render/match.py index 14acf367b..196f47f65 100644 --- a/dvc/render/match.py +++ b/dvc/render/match.py @@ -1,3 +1,4 @@ +import os from collections import defaultdict from typing import TYPE_CHECKING, Dict, List, Optional @@ -5,7 +6,7 @@ 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 @@ -43,17 +44,20 @@ class PlotsData: 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 diff --git a/tests/func/plots/test_show.py b/tests/func/plots/test_show.py index 4a3d36a1d..0d49745ab 100644 --- a/tests/func/plots/test_show.py +++ b/tests/func/plots/test_show.py @@ -1,3 +1,4 @@ +import json import os import pytest @@ -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]