def test_import_as_with_context_deterministic(tmp_path): src = "\n".join(f'{{% import "bar" as bar{i} with context %}}' for i in range(10)) env = Environment(loader=DictLoader({"foo": src})) env.compile_templates(tmp_path, zip=None) name = os.listdir(tmp_path)[0] content = (tmp_path / name).read_text("utf8") expect = [f"'bar{i}': " for i in range(10)] found = re.findall(r"'bar\d': ", content)[:10] assert found == expect
def test_filters_deterministic(tmp_path): src = "".join(f"{{{{ {i}|filter{i} }}}}" for i in range(10)) env = Environment(loader=DictLoader({"foo": src})) env.filters.update( dict.fromkeys((f"filter{i}" for i in range(10)), lambda: None)) env.compile_templates(tmp_path, zip=None) name = os.listdir(tmp_path)[0] content = (tmp_path / name).read_text("utf8") expect = [f"filters['filter{i}']" for i in range(10)] found = re.findall(r"filters\['filter\d']", content) assert found == expect
def TestOneInput(data): fdp = atheris.FuzzedDataProvider(data) template_str = fdp.ConsumeString(sys.maxsize) tmp_path = "/tmp/mytemplates" temp_file = os.path.join(tmp_path, "template.jinja2") if not os.path.isdir(tmp_path): os.mkdir(tmp_path) if os.path.isfile(temp_file): os.remove(temp_file) with open(temp_file, "wb") as fd: fd.write(data) env = Environment(loader=DictLoader({"foo": template_str})) try: env.compile_templates(tmp_path, zip=None) except RecursionError: return return