test_compile.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. import os
  2. import re
  3. from jinja2.environment import Environment
  4. from jinja2.loaders import DictLoader
  5. def test_filters_deterministic(tmp_path):
  6. src = "".join(f"{{{{ {i}|filter{i} }}}}" for i in range(10))
  7. env = Environment(loader=DictLoader({"foo": src}))
  8. env.filters.update(dict.fromkeys((f"filter{i}" for i in range(10)), lambda: None))
  9. env.compile_templates(tmp_path, zip=None)
  10. name = os.listdir(tmp_path)[0]
  11. content = (tmp_path / name).read_text("utf8")
  12. expect = [f"filters['filter{i}']" for i in range(10)]
  13. found = re.findall(r"filters\['filter\d']", content)
  14. assert found == expect
  15. def test_import_as_with_context_deterministic(tmp_path):
  16. src = "\n".join(f'{{% import "bar" as bar{i} with context %}}' for i in range(10))
  17. env = Environment(loader=DictLoader({"foo": src}))
  18. env.compile_templates(tmp_path, zip=None)
  19. name = os.listdir(tmp_path)[0]
  20. content = (tmp_path / name).read_text("utf8")
  21. expect = [f"'bar{i}': " for i in range(10)]
  22. found = re.findall(r"'bar\d': ", content)[:10]
  23. assert found == expect