plots: make sure template is using data anchor (#4045)

Fixes #3969
This commit is contained in:
Ruslan Kuprieiev
2020-06-15 21:14:07 +03:00
committed by GitHub
parent c9391df313
commit b672622324
2 changed files with 32 additions and 1 deletions
+10
View File
@@ -12,6 +12,10 @@ class TemplateNotFoundError(DvcException):
super().__init__(f"Template '{path}' not found.")
class BadTemplateError(DvcException):
pass
class NoFieldInDataError(DvcException):
def __init__(self, field_name):
super().__init__(
@@ -39,6 +43,12 @@ class Template:
def render(self, data, props=None):
props = props or {}
if self.METRIC_DATA_ANCHOR not in self.content:
raise BadTemplateError(
f"Template '{self.filename}' is not using "
f"'{self.METRIC_DATA_ANCHOR}' anchor"
)
if props.get("x"):
Template._check_field_exists(data, props.get("x"))
if props.get("y"):
+22 -1
View File
@@ -16,7 +16,11 @@ from dvc.repo.plots.data import (
PlotMetricTypeError,
YAMLPlotData,
)
from dvc.repo.plots.template import NoFieldInDataError, TemplateNotFoundError
from dvc.repo.plots.template import (
BadTemplateError,
NoFieldInDataError,
TemplateNotFoundError,
)
def _write_csv(metric, filename, header=True):
@@ -405,6 +409,23 @@ def test_should_raise_on_no_template(tmp_dir, dvc, run_copy_metrics):
dvc.plots.show("metric.json", props=props)
def test_bad_template(tmp_dir, dvc, run_copy_metrics):
metric = [{"val": 2}, {"val": 3}]
_write_json(tmp_dir, metric, "metric_t.json")
run_copy_metrics(
"metric_t.json",
"metric.json",
plots_no_cache=["metric.json"],
commit="first run",
)
tmp_dir.gen("template.json", json.dumps({"a": "b", "c": "d"}))
with pytest.raises(BadTemplateError):
props = {"template": "template.json"}
dvc.plots.show("metric.json", props=props)
def test_plot_wrong_metric_type(tmp_dir, scm, dvc, run_copy_metrics):
tmp_dir.gen("metric_t.txt", "some text")
run_copy_metrics(