Debian Codegraph @

test/units/_internal/templating/test_jinja_plugins.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 import pytest
6
7 from ansible.errors import AnsibleUndefinedVariable
8 from ansible._internal._datatag._tags import TrustedAsTemplate
9 from ansible._internal._templating._engine import TemplateEngine
10
11 TRUST = TrustedAsTemplate()
12
13
14 @pytest.mark.parametrize("expr, expected", (
15 ('["ok", bogus_var, "also ok"] | select("defined")', ["ok", "also ok"]),
16 ('[{"a": "ok"}, {"a": bogus_var}, {"a": "also ok"}] | selectattr("a", "defined")', [{"a": "ok"}, {"a": "also ok"}]),
17 ('["ok", bogus_var, "also ok"] | reject("undefined")', ["ok", "also ok"]),
18 ('[{"a": "ok"}, {"a": bogus_var}, {"a": "also ok"}] | rejectattr("a", "undefined")', [{"a": "ok"}, {"a": "also ok"}]),
19 ('[["ok"], [bogus_var], ["also ok"]] | map("select", "defined")', [["ok"], [], ["also ok"]]),
20 ('"abc" is defined', True),
21 ('"abc" is undefined', False),
22 ('bogus_var is defined', False),
23 ('bogus_var is undefined', True),
24 ))
25 def test_wrapped_plugins_undefined_arg_passthru(expr: str, expected: t.Any) -> None:
26 assert TemplateEngine().evaluate_expression(TRUST.tag(expr)) == expected
27
28
29 @pytest.mark.parametrize("expr", (
30 "['abc'] | selectattr('a', 'equalto', bogus_var)",
31 "['abc'] | selectattr(attr=bogus_var)",
32 "['abc'] | select('sameas', bogus_var)",
33 "['abc'] | reject('sameas', bogus_var)",
34 "['abc'] | rejectattr('a', 'equalto', bogus_var)",
35 "['abc'] | rejectattr(attr=bogus_var)",
36 "['abc'] | map(bogus_var)",
37 "['abc'] | map(default=bogus_var)",
38 'bogus_var is string',
39 'bogus_var is float',
40 'bogus_var is integer',
41 'bogus_var is number',
42 'bogus_var is none',
43 'bogus_var is boolean',
44 'bogus_var is false',
45 'bogus_var is true',
46 'bogus_var is mapping',
47 'bogus_var is sameas(123)',
48 '123 is sameas(bogus_var)',
49 'bogus_var is escaped',
50 ))
51 def test_wrapped_plugins_undefined_arg_fail(expr: str) -> None:
52 with pytest.raises(AnsibleUndefinedVariable):
53 TemplateEngine().evaluate_expression(TRUST.tag(expr))