computer-playbook/tests/integration/test_jinja2_syntax.py

36 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# tests/integration/test_jinja2_syntax.py
import os
import unittest
from jinja2 import Environment, exceptions
class TestJinja2Syntax(unittest.TestCase):
def test_all_j2_templates_have_valid_syntax(self):
"""
Findet rekursiv alle .j2-Dateien ab Projekt-Root und versucht, sie zu parsen.
Ein SyntaxError in einem Template schlägt den Test fehl.
"""
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))
env = Environment()
failures = []
for root, _dirs, files in os.walk(project_root):
for fname in files:
if fname.endswith('.j2'):
path = os.path.join(root, fname)
with open(path, 'r', encoding='utf-8') as f:
src = f.read()
try:
env.parse(src)
except exceptions.TemplateSyntaxError as e:
failures.append(f"{path}:{e.lineno} {e.message}")
if failures:
self.fail("Gefundene Syntax-Fehler in Jinja2-Templates:\n" +
"\n".join(failures))
if __name__ == '__main__':
unittest.main()