-
-
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
80 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,76 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from jinja2 import Environment | ||
from jinja2 import loaders | ||
|
||
|
||
class TestCompileTemplate: | ||
archive = None | ||
|
||
def setup(self): | ||
self.archive = tempfile.mkdtemp() | ||
|
||
def teardown(self): | ||
shutil.rmtree(self.archive) | ||
|
||
def test_filters(self): | ||
num_filter = 10 | ||
|
||
env = Environment( | ||
loader=loaders.DictLoader( | ||
{ | ||
"foo": "".join( | ||
"{{ %d|filter%d }}" % (i, i) for i in range(num_filter) | ||
) | ||
} | ||
) | ||
) | ||
|
||
for i in range(num_filter): | ||
env.filters["filter%d" % i] = lambda value: value | ||
|
||
env.compile_templates(self.archive, zip=None, ignore_errors=False) | ||
|
||
files = os.listdir(self.archive) | ||
assert len(files) == 1 | ||
|
||
with open(os.path.join(self.archive, files[0])) as f: | ||
content = f.read() | ||
|
||
for i in range(num_filter): | ||
idx = content.find("environment.filters['filter%d']" % i) | ||
assert idx >= 0 | ||
content = content[idx:] | ||
|
||
def test_import_as_with_context(self): | ||
num_import = 10 | ||
|
||
env = Environment( | ||
loader=loaders.DictLoader( | ||
{ | ||
"foo": "\n".join( | ||
'{%% import "bar" as bar%d with context %%}' % i | ||
for i in range(num_import) | ||
) | ||
} | ||
) | ||
) | ||
env.compile_templates(self.archive, zip=None, ignore_errors=False) | ||
|
||
files = os.listdir(self.archive) | ||
assert len(files) == 1 | ||
|
||
with open(os.path.join(self.archive, files[0])) as f: | ||
content = next( | ||
filter(lambda line: "make_module" in line, f.read().splitlines()) | ||
) | ||
|
||
for i in range(num_import): | ||
print(i) | ||
print(content) | ||
|
||
idx = content.find("'bar%d': " % i) | ||
assert idx >= 0 | ||
content = content[idx:] |