Debian Codegraph @

test/units/plugins/lookup/test_template.py

raw
1 from __future__ import annotations
2
3 import pathlib
4
5 from ansible._internal._templating._utils import Omit
6 from ansible.parsing.dataloader import DataLoader
7 from ansible.template import Templar, trust_as_template
8
9
10 def test_no_finalize_marker_passthru(tmp_path: pathlib.Path) -> None:
11 """Return an Undefined marker from a template lookup to ensure that the internal templating operation does not finalize its result."""
12 template_path = tmp_path / 'template.txt'
13 template_path.write_text("{{ bogusvar }}")
14
15 templar = Templar(loader=DataLoader(), variables=dict(template_path=str(template_path)))
16
17 assert templar.template(trust_as_template('{{ lookup("template", template_path) | default("pass") }}')) == "pass"
18
19
20 def test_no_finalize_omit_passthru(tmp_path: pathlib.Path) -> None:
21 """Return an Omit scalar from a template lookup to ensure that the internal templating operation does not finalize its result."""
22 template_path = tmp_path / 'template.txt'
23 template_path.write_text("{{ omitted }}")
24
25 data = dict(omitted=trust_as_template("{{ omit }}"), template_path=str(template_path))
26
27 # The result from the lookup should be an Omit value, since the result of the template lookup's internal templating call should not be finalized.
28 # If it were, finalize would trip the Omit and raise an error about a top-level template result resolving to an Omit scalar.
29 res = Templar(loader=DataLoader(), variables=data).template(trust_as_template("{{ lookup('template', template_path) | type_debug }}"))
30
31 assert res == type(Omit).__name__