Merge "Allow using builtin functions in task templates"

This commit is contained in:
Jenkins 2015-01-22 12:18:06 +00:00 committed by Gerrit Code Review
commit b2c5217595
2 changed files with 11 additions and 1 deletions

View File

@ -102,10 +102,16 @@ def task_template_render(task_template, **kwargs):
:param kwargs: Dict with template arguments
:returns: rendered template str
"""
# NOTE(boris-42): We have to import __builtin__ to get full list of builtin
# functions (e.g. range()). Unfortunately __builtins__
# doesn't return them (when it is not main module)
import __builtin__
ast = jinja2.Environment().parse(task_template)
required_kwargs = jinja2.meta.find_undeclared_variables(ast)
missing = set(required_kwargs) - set(kwargs)
missing = set(required_kwargs) - set(kwargs) - set(dir(__builtin__))
# NOTE(boris-42): Removing variables that have default values from missing.
# Construction that won't be properly checked is
# {% set x = x or 1}

View File

@ -103,6 +103,10 @@ class APITestCase(test.TestCase):
self.assertEqual(
"5 = 5", api.task_template_render(template, a=2, b=3, c=5))
def test_task_template_render_builtin(self):
template = "{% for i in range(4) %}{{i}}{% endfor %}"
self.assertEqual("0123", api.task_template_render(template))
def test_task_template_render_missing_args(self):
self.assertRaises(TypeError, api.task_template_render, "{{a}}")