summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-10-06 06:37:53 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2008-10-06 06:37:53 +0000
commitb5ac7f79184ec69c37c65a8d09d16897cd708c00 (patch)
tree769801bdd843d3a60924747bb77b7585a4a41440 /tests
parentc201d14269204a95c1f95299490430b2d8206341 (diff)
[1.0.X] Added some better error reporting and path handling when creating template paths.
We now raise UnicodeDecodeError for non-UTF-8 bytestrings (thanks to Daniel Pope for diagnosing this was being swallowed by ValueError) and allow UTF-8 bytestrings as template directories. (The last bit is arguably a feature-add, but we allow UTF-8 bytestrings everywhere else, so I'm counting it as a bugfix.) Refs #8965. Backport of r9161 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9162 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
-rw-r--r--tests/regressiontests/templates/tests.py49
1 files changed, 30 insertions, 19 deletions
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index c69e7cdd1b..cdf56ab56c 100644
--- a/tests/regressiontests/templates/tests.py
+++ b/tests/regressiontests/templates/tests.py
@@ -92,34 +92,45 @@ class UTF8Class:
class Templates(unittest.TestCase):
def test_loaders_security(self):
def test_template_sources(path, template_dirs, expected_sources):
- # Fix expected sources so they are normcased and abspathed
- expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources]
- # Test app_directories loader
- sources = app_directories.get_template_sources(path, template_dirs)
- self.assertEqual(list(sources), expected_sources)
- # Test filesystem loader
- sources = filesystem.get_template_sources(path, template_dirs)
- self.assertEqual(list(sources), expected_sources)
+ if isinstance(expected_sources, list):
+ # Fix expected sources so they are normcased and abspathed
+ expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources]
+ # Test the two loaders (app_directores and filesystem).
+ func1 = lambda p, t: list(app_directories.get_template_sources(p, t))
+ func2 = lambda p, t: list(filesystem.get_template_sources(p, t))
+ for func in (func1, func2):
+ if isinstance(expected_sources, list):
+ self.assertEqual(func(path, template_dirs), expected_sources)
+ else:
+ self.assertRaises(expected_sources, func, path, template_dirs)
template_dirs = ['/dir1', '/dir2']
test_template_sources('index.html', template_dirs,
['/dir1/index.html', '/dir2/index.html'])
- test_template_sources('/etc/passwd', template_dirs,
- [])
+ test_template_sources('/etc/passwd', template_dirs, [])
test_template_sources('etc/passwd', template_dirs,
['/dir1/etc/passwd', '/dir2/etc/passwd'])
- test_template_sources('../etc/passwd', template_dirs,
- [])
- test_template_sources('../../../etc/passwd', template_dirs,
- [])
+ test_template_sources('../etc/passwd', template_dirs, [])
+ test_template_sources('../../../etc/passwd', template_dirs, [])
test_template_sources('/dir1/index.html', template_dirs,
['/dir1/index.html'])
test_template_sources('../dir2/index.html', template_dirs,
['/dir2/index.html'])
- test_template_sources('/dir1blah', template_dirs,
- [])
- test_template_sources('../dir1blah', template_dirs,
- [])
+ test_template_sources('/dir1blah', template_dirs, [])
+ test_template_sources('../dir1blah', template_dirs, [])
+
+ # UTF-8 bytestrings are permitted.
+ test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs,
+ [u'/dir1/Ångström', u'/dir2/Ångström'])
+ # Unicode strings are permitted.
+ test_template_sources(u'Ångström', template_dirs,
+ [u'/dir1/Ångström', u'/dir2/Ångström'])
+ test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström'])
+ test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'],
+ [u'/Straße/Ångström'])
+ # Invalid UTF-8 encoding in bytestrings is not. Should raise a
+ # semi-useful error message.
+ test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError)
# Case insensitive tests (for win32). Not run unless we're on
# a case insensitive operating system.
@@ -372,7 +383,7 @@ class Templates(unittest.TestCase):
# Numbers as filter arguments should work
'filter-syntax19': ('{{ var|truncatewords:1 }}', {"var": "hello world"}, "hello ..."),
-
+
#filters should accept empty string constants
'filter-syntax20': ('{{ ""|default_if_none:"was none" }}', {}, ""),