mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-05-29 20:39:09 -07:00
Enable writing plugins for jinja2 tests
This commit is contained in:
parent
6a8062baad
commit
1bf5224f82
13 changed files with 216 additions and 99 deletions
|
@ -39,6 +39,7 @@ from ansible.utils import to_bytes, to_unicode
|
|||
class Globals(object):
|
||||
|
||||
FILTERS = None
|
||||
TESTS = None
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
@ -54,10 +55,26 @@ def _get_filters():
|
|||
filters = {}
|
||||
for fp in plugins:
|
||||
filters.update(fp.filters())
|
||||
filters.update(_get_tests())
|
||||
Globals.FILTERS = filters
|
||||
|
||||
return Globals.FILTERS
|
||||
|
||||
def _get_tests():
|
||||
''' return test plugin instances '''
|
||||
|
||||
if Globals.TESTS is not None:
|
||||
return Globals.TESTS
|
||||
|
||||
from ansible import utils
|
||||
plugins = [ x for x in utils.plugins.test_loader.all()]
|
||||
tests = {}
|
||||
for tp in plugins:
|
||||
tests.update(tp.tests())
|
||||
Globals.TESTS = tests
|
||||
|
||||
return Globals.TESTS
|
||||
|
||||
def _get_extensions():
|
||||
''' return jinja2 extensions to load '''
|
||||
|
||||
|
@ -237,6 +254,7 @@ def template_from_file(basedir, path, vars, vault_password=None):
|
|||
|
||||
environment = jinja2.Environment(loader=loader, trim_blocks=True, extensions=_get_extensions())
|
||||
environment.filters.update(_get_filters())
|
||||
environment.tests.update(_get_tests())
|
||||
environment.globals['lookup'] = my_lookup
|
||||
environment.globals['finalize'] = my_finalize
|
||||
if fail_on_undefined:
|
||||
|
@ -351,6 +369,7 @@ def template_from_string(basedir, data, vars, fail_on_undefined=False):
|
|||
|
||||
environment = jinja2.Environment(trim_blocks=True, undefined=StrictUndefined, extensions=_get_extensions(), finalize=my_finalize)
|
||||
environment.filters.update(_get_filters())
|
||||
environment.tests.update(_get_tests())
|
||||
environment.template_class = J2Template
|
||||
|
||||
if '_original_file' in vars:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue