Skip to content

Commit

Permalink
add strict mode to Jinja renders
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed May 17, 2024
1 parent 982f844 commit b414a3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/ibek/gen_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from typing import List, Sequence, Tuple

from jinja2 import Template
from jinja2 import StrictUndefined, Template

from ibek.utils import UTILS

Expand All @@ -31,7 +31,13 @@ def create_db_script(

templates = renderer.render_database(extra_databases)

return Template(jinja_txt).render(templates=templates)
try:
return Template(jinja_txt).render(
templates=templates, undefined=StrictUndefined
)
except Exception:
print(f"ERROR RENDERING DATABASE TEMPLATE:\n{templates}")
raise


def create_boot_script(entities: Sequence[Entity]) -> str:
Expand Down
4 changes: 2 additions & 2 deletions src/ibek/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pathlib import Path
from typing import Any, Dict, Mapping

from jinja2 import Template
from jinja2 import StrictUndefined, Template


@dataclass
Expand Down Expand Up @@ -110,7 +110,7 @@ def render(self, context: Any, template_text: Any) -> str:
return template_text

try:
jinja_template = Template(template_text)
jinja_template = Template(template_text, undefined=StrictUndefined)
return jinja_template.render(
context,
__utils__=self,
Expand Down
31 changes: 31 additions & 0 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
Some unit tests for ibek.
"""

import dataclasses

import jinja2
import pytest

from ibek.args import IdArg, ObjectArg
from ibek.commands import semver_compare
from ibek.ioc import id_to_entity
from ibek.ioc_factory import IocFactory
from ibek.support import EntityDefinition, Support
from ibek.utils import UTILS


def test_object_references(entity_factory):
Expand Down Expand Up @@ -58,3 +64,28 @@ def test_compare():
assert semver_compare("1.1.1", "==1.1.1")
assert semver_compare("1.1.1", ">=1.1.0")
assert not semver_compare("1.1.1", ">=1.1.2")


@dataclasses.dataclass
class Person:
name: str
age: int


def test_strict():
"""
validate that bad references in Jinja are caught
"""
p = Person("giles", 59)

my_template = "{{ person.name ~ ' of age ' ~ person.age }}"
text = UTILS.render({"person": p}, my_template)
assert text == "giles of age 59"

my_template = "{{ person.name ~ ' of age ' ~ height }}"
with pytest.raises(jinja2.exceptions.UndefinedError):
text = UTILS.render({"person": p}, my_template)

my_template = "{{ person.name ~ ' of age ' ~ person.height }}"
with pytest.raises(jinja2.exceptions.UndefinedError):
text = UTILS.render({"person": p}, my_template)

0 comments on commit b414a3c

Please sign in to comment.