Make templates more faithful to the original file in terms of indentation, newlines, no deserialisation of things like < or >. More test coverage

This commit is contained in:
Miguel Jacq 2026-06-20 15:28:48 +10:00
parent 3d53d4fb30
commit 49fee7afe4
Signed by: mig5
GPG key ID: 03906B4110AAD3B8
12 changed files with 579 additions and 52 deletions

View file

@ -7,6 +7,7 @@ import yaml
from jinjaturtle.core import (
parse_config,
analyze_loops,
flatten_config,
generate_ansible_yaml,
generate_jinja2_template,
@ -204,6 +205,36 @@ def test_yaml_loop_preserves_following_top_level_comments(tmp_path: Path):
assert yaml.safe_load(rendered) == yaml.safe_load(text)
def test_yaml_scalar_loop_preserves_quoted_list_items(tmp_path: Path):
from jinja2 import Template
text = textwrap.dedent(
"""
files:
- 'spec/unit/puppet/provider/dsc_base_provider/dsc_base_provider_spec.rb'
- 'spec/unit/pwsh/util_spec.rb'
- 'spec/unit/pwsh/windows_powershell_spec.rb'
"""
).lstrip()
path = tmp_path / "quoted-list.yaml"
path.write_text(text, encoding="utf-8")
fmt, parsed = parse_config(path)
loop_candidates = analyze_loops(fmt, parsed)
flat_items = flatten_config(fmt, parsed, loop_candidates)
defaults = yaml.safe_load(
generate_ansible_yaml("role", flat_items, loop_candidates)
)
template = generate_jinja2_template(
fmt, parsed, "role", original_text=text, loop_candidates=loop_candidates
)
rendered = Template(template).render(**defaults)
assert loop_candidates
assert "- '{{" in template
assert rendered == text
def test_yaml_bool_scalars_render_with_yaml_spelling(tmp_path: Path):
from jinja2 import Template