-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make compile_templates deterministic
Python3 doesn't keep insertion order for set(), so this sorts some places for deterministic output for compiled template.
- Loading branch information
Showing
4 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import os | ||
import re | ||
|
||
from jinja2 import Environment | ||
from jinja2 import loaders | ||
|
||
|
||
def test_filters_deterministic(tmp_path): | ||
src = "".join(f"{{{{ {i}|filter{i} }}}}" for i in range(10)) | ||
env = Environment(loader=loaders.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 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=loaders.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 |