-
-
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
121 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,118 @@ | ||
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): | ||
env = Environment( | ||
loader=loaders.DictLoader( | ||
{ | ||
"foo": """ | ||
{{ 1|filter1 }} | ||
{{ 2|filter2 }} | ||
""" | ||
} | ||
) | ||
) | ||
|
||
env.filters["filter1"] = lambda value: value | ||
env.filters["filter2"] = 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: | ||
assert f.read().splitlines() == [ | ||
"from __future__ import generator_stop", | ||
"from jinja2.runtime import LoopContext, Macro, Markup," | ||
" Namespace, TemplateNotFound, TemplateReference," | ||
" TemplateRuntimeError, Undefined, concat, escape, identity," | ||
" internalcode, markup_join, missing, str_join", | ||
"name = 'foo'", | ||
"", | ||
"def root(context, missing=missing):", | ||
" resolve = context.resolve_or_missing", | ||
" undefined = environment.undefined", | ||
" cond_expr_undefined = Undefined", | ||
" if 0: yield None", | ||
" try:", | ||
" t_1 = environment.filters['filter1']", | ||
" except KeyError:", | ||
" @internalcode", | ||
" def t_1(*unused):", | ||
' raise TemplateRuntimeError("No filter named' | ||
" 'filter1' found.\")", | ||
" try:", | ||
" t_2 = environment.filters['filter2']", | ||
" except KeyError:", | ||
" @internalcode", | ||
" def t_2(*unused):", | ||
' raise TemplateRuntimeError("No filter named' | ||
" 'filter2' found.\")", | ||
" pass", | ||
" yield '\\n1\\n2'", | ||
"", | ||
"blocks = {}", | ||
"debug_info = ''", | ||
] | ||
|
||
def test_import_as_with_context(self): | ||
env = Environment( | ||
loader=loaders.DictLoader( | ||
{ | ||
"foo": """ | ||
{% import "bar" as bar with context %} | ||
{% import "baz" as baz with context %} | ||
""" | ||
} | ||
) | ||
) | ||
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: | ||
assert f.read().splitlines() == [ | ||
"from __future__ import generator_stop", | ||
"from jinja2.runtime import LoopContext, Macro, Markup," | ||
" Namespace, TemplateNotFound, TemplateReference," | ||
" TemplateRuntimeError, Undefined, concat, escape, identity," | ||
" internalcode, markup_join, missing, str_join", | ||
"name = 'foo'", | ||
"", | ||
"def root(context, missing=missing):", | ||
" resolve = context.resolve_or_missing", | ||
" undefined = environment.undefined", | ||
" cond_expr_undefined = Undefined", | ||
" if 0: yield None", | ||
" l_0_bar = l_0_baz = missing", | ||
" pass", | ||
" yield '\\n'", | ||
" l_0_bar = context.vars['bar'] = environment.get_template(" | ||
"'bar', 'foo').make_module(context.get_all(), True, {'bar':" | ||
" l_0_bar, 'baz': l_0_baz})", | ||
" context.exported_vars.discard('bar')", | ||
" yield '\\n'", | ||
" l_0_baz = context.vars['baz'] = environment.get_template(" | ||
"'baz', 'foo').make_module(context.get_all(), True, {'bar':" | ||
" l_0_bar, 'baz': l_0_baz})", | ||
" context.exported_vars.discard('baz')", | ||
"", | ||
"blocks = {}", | ||
"debug_info = '2=13&3=16'", | ||
] |