1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #12787: Correctly identify the template that does not exist when a template being extended includes another template that does not exist. Thanks to trigeek38 for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12792 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Karen Tracey
2010-03-16 14:34:57 +00:00
parent e89a5e06cf
commit 80e744945c
6 changed files with 117 additions and 36 deletions

View File

@@ -160,38 +160,106 @@ class Templates(unittest.TestCase):
# Turn TEMPLATE_DEBUG on, so that the origin file name will be kept with
# the compiled templates.
old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
old_loaders = loader.template_source_loaders
loader.template_source_loaders = (filesystem.Loader(),)
# We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
# point to a directory containing a 404.html file. Also that
# the file system and app directories loaders both inherit the
# load_template method from the BaseLoader class, so we only need
# to test one of them.
load_name = '404.html'
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
try:
loader.template_source_loaders = (filesystem.Loader(),)
# Aso test the cached loader, since it overrides load_template
cache_loader = cached.Loader(('',))
cache_loader._cached_loaders = loader.template_source_loaders
loader.template_source_loaders = (cache_loader,)
# We rely on the fact that runtests.py sets up TEMPLATE_DIRS to
# point to a directory containing a 404.html file. Also that
# the file system and app directories loaders both inherit the
# load_template method from the BaseLoader class, so we only need
# to test one of them.
load_name = '404.html'
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Template loaded by filesystem loader has incorrect name for debug page: %s' % template_name)
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
# Aso test the cached loader, since it overrides load_template
cache_loader = cached.Loader(('',))
cache_loader._cached_loaders = loader.template_source_loaders
loader.template_source_loaders = (cache_loader,)
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Template loaded through cached loader has incorrect name for debug page: %s' % template_name)
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td
template = loader.get_template(load_name)
template_name = template.nodelist[0].source[0].name
self.assertTrue(template_name.endswith(load_name),
'Cached template loaded through cached loader has incorrect name for debug page: %s' % template_name)
finally:
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td
def test_extends_include_missing_baseloader(self):
"""
Tests that the correct template is identified as not existing
when {% extends %} specifies a template that does exist, but
that template has an {% include %} of something that does not
exist. See #12787.
"""
# TEMPLATE_DEBUG must be true, otherwise the exception raised
# during {% include %} processing will be suppressed.
old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
old_loaders = loader.template_source_loaders
try:
# Test the base loader class via the app loader. load_template
# from base is used by all shipped loaders excepting cached,
# which has its own test.
loader.template_source_loaders = (app_directories.Loader(),)
load_name = 'test_extends_error.html'
tmpl = loader.get_template(load_name)
r = None
try:
r = tmpl.render(template.Context({}))
except template.TemplateSyntaxError, e:
settings.TEMPLATE_DEBUG = old_td
self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
finally:
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td
def test_extends_include_missing_cachedloader(self):
"""
Same as test_extends_include_missing_baseloader, only tests
behavior of the cached loader instead of BaseLoader.
"""
old_td, settings.TEMPLATE_DEBUG = settings.TEMPLATE_DEBUG, True
old_loaders = loader.template_source_loaders
try:
cache_loader = cached.Loader(('',))
cache_loader._cached_loaders = (app_directories.Loader(),)
loader.template_source_loaders = (cache_loader,)
load_name = 'test_extends_error.html'
tmpl = loader.get_template(load_name)
r = None
try:
r = tmpl.render(template.Context({}))
except template.TemplateSyntaxError, e:
self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
# For the cached loader, repeat the test, to ensure the first attempt did not cache a
# result that behaves incorrectly on subsequent attempts.
tmpl = loader.get_template(load_name)
try:
tmpl.render(template.Context({}))
except template.TemplateSyntaxError, e:
self.assertEqual(e.args[0], 'Caught TemplateDoesNotExist while rendering: missing.html')
self.assertEqual(r, None, 'Template rendering unexpectedly succeeded, produced: ->%r<-' % r)
finally:
loader.template_source_loaders = old_loaders
settings.TEMPLATE_DEBUG = old_td
def test_token_smart_split(self):
# Regression test for #7027
@@ -269,7 +337,7 @@ class Templates(unittest.TestCase):
if isinstance(vals[2], tuple):
normal_string_result = vals[2][0]
invalid_string_result = vals[2][1]
if '%s' in invalid_string_result:
if isinstance(invalid_string_result, basestring) and '%s' in invalid_string_result:
expected_invalid_str = 'INVALID %s'
invalid_string_result = invalid_string_result % vals[2][2]
template.invalid_var_format_string = True
@@ -530,10 +598,10 @@ class Templates(unittest.TestCase):
### EXCEPTIONS ############################################################
# Raise exception for invalid template name
'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateSyntaxError),
'exception01': ("{% extends 'nonexistent' %}", {}, template.TemplateDoesNotExist),
# Raise exception for invalid template name (in variable)
'exception02': ("{% extends nonexistent %}", {}, template.TemplateSyntaxError),
'exception02': ("{% extends nonexistent %}", {}, (template.TemplateSyntaxError, template.TemplateDoesNotExist)),
# Raise exception for extra {% extends %} tags
'exception03': ("{% extends 'inheritance01' %}{% block first %}2{% endblock %}{% extends 'inheritance16' %}", {}, template.TemplateSyntaxError),