Skip to content

Commit

Permalink
plots: fix resolve template path bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pared authored and efiop committed Nov 2, 2021
1 parent a67d8a4 commit 22cc2a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
10 changes: 5 additions & 5 deletions dvc/repo/plots/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ def templates_dir(self):
@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 = [
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/render/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 22cc2a5

Please sign in to comment.