plots: fix resolve template path bug

Fix: #6854
This commit is contained in:
Paweł Redzyński
2021-11-02 16:49:56 +02:00
committed by Ruslan Kuprieiev
parent a67d8a44b3
commit 22cc2a5cbc
2 changed files with 31 additions and 5 deletions
+5 -5
View File
@@ -109,16 +109,16 @@ class PlotTemplates:
@staticmethod
def _find(templates, template_name):
for template in templates:
if (
template_name == template
or template_name + ".json" == template
if template.endswith(template_name) or template.endswith(
template_name + ".json"
):
return template
return None
def _find_in_project(self, name: str) -> Optional["StrPath"]:
if os.path.exists(name):
return name
full_path = os.path.abspath(name)
if os.path.exists(full_path):
return full_path
if os.path.exists(self.templates_dir):
templates = [
+26
View File
@@ -451,3 +451,29 @@ def test_find_vega(tmp_dir, dvc):
first(plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD
)
assert first(plot_content["layer"])["encoding"]["y"]["field"] == "y"
@pytest.mark.parametrize(
"template_path, target_name",
[
(os.path.join(".dvc", "plots", "template.json"), "template"),
(os.path.join(".dvc", "plots", "template.json"), "template.json"),
(
os.path.join(".dvc", "plots", "subdir", "template.json"),
os.path.join("subdir", "template.json"),
),
(
os.path.join(".dvc", "plots", "subdir", "template.json"),
os.path.join("subdir", "template"),
),
("template.json", "template.json"),
],
)
def test_should_resolve_template(tmp_dir, dvc, template_path, target_name):
os.makedirs(os.path.abspath(os.path.dirname(template_path)), exist_ok=True)
with open(template_path, "w", encoding="utf-8") as fd:
fd.write("template_content")
assert dvc.plots.templates._find_in_project(
target_name
) == os.path.abspath(template_path)