Permalink
Browse files

Fixed #6430 -- Corrected the loading of templates from eggs, which w…

…as broken by the unicode merge. Thanks for the excellent report and patch, Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7565 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
freakboy3742 committed May 29, 2008
1 parent d4d5f4c commit d53e8f1285247a2a9579f30982086cc495c1098d
View
@@ -78,7 +78,7 @@ answer newbie questions, and generally made Django that much better:
brut.alll@gmail.com
btoll@bestweb.net
Jonathan Buchanan <jonathan.buchanan@gmail.com>
- Can Burak Çilingir <canburak@cs.bilgi.edu.tr>
+ Juan Manuel Caicedo <juan.manuel.caicedo@gmail.com>
Trevor Caira <trevor@caira.com>
Ricardo Javier Cárdenes Medina <ricardo.cardenes@gmail.com>
Graham Carlyle <graham.carlyle@maplecroft.net>
@@ -92,6 +92,7 @@ answer newbie questions, and generally made Django that much better:
ivan.chelubeev@gmail.com
Bryan Chow <bryan at verdjn dot com>
Michal Chruszcz <troll@pld-linux.org>
+ Can Burak Çilingir <canburak@cs.bilgi.edu.tr>
Ian Clelland <clelland@gmail.com>
Russell Cloran <russell@rucus.net>
colin@owlfish.com
@@ -18,7 +18,7 @@ def load_template_source(template_name, template_dirs=None):
pkg_name = 'templates/' + template_name
for app in settings.INSTALLED_APPS:
try:
- return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)).decode(settings.FILE_CHARSET)
+ return (resource_string(app, pkg_name).decode(settings.FILE_CHARSET), 'egg:%s:%s' % (app, pkg_name))
except:
pass
raise TemplateDoesNotExist, template_name
@@ -0,0 +1,88 @@
+# -*- coding: utf-8 -*-
+"""
+Test cases for the template loaders
+"""
+
+from django.conf import settings
+
+if __name__ == '__main__':
+ settings.configure()
+
+import unittest
+import sys
+import pkg_resources
+import imp
+import StringIO
+
+from django.template import TemplateDoesNotExist
+from django.template.loaders.eggs import load_template_source as lts_egg
+
+#Mock classes and objects for pkg_resources functions
+class MockProvider(pkg_resources.NullProvider):
+ def __init__(self, module):
+ pkg_resources.NullProvider.__init__(self, module)
+ self.module = module
+
+ def _has(self, path):
+ return path in self.module._resources
+
+ def _isdir(self,path):
+ return False
+
+ def get_resource_stream(self, manager, resource_name):
+ return self.module._resources[resource_name]
+
+ def _get(self, path):
+ return self.module._resources[path].read()
+
+class MockLoader(object): pass
+
+def create_egg(name, resources):
+ """
+ Creates a mock egg with a list of resources
+
+ name: The name of the module
+ resources: A dictionary of resources. Keys are the names and values the the data.
+ """
+ egg = imp.new_module(name)
+ egg.__loader__ = MockLoader()
+ egg._resources = resources
+ sys.modules[name] = egg
+
+
+class EggLoader(unittest.TestCase):
+ def setUp(self):
+ pkg_resources._provider_factories[MockLoader] = MockProvider
+
+ self.empty_egg = create_egg("egg_empty", {})
+ self.egg_1 = create_egg("egg_1", {
+ 'templates/y.html' : StringIO.StringIO("y"),
+ 'templates/x.txt' : StringIO.StringIO("x"),
+ })
+ settings.INSTALLED_APPS = []
+
+ def test_empty(self):
+ "Loading any template on an empty egg should fail"
+ settings.INSTALLED_APPS = ['egg_empty']
+ self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
+
+ def test_non_existing(self):
+ "Template loading fails if the template is not in the egg"
+ settings.INSTALLED_APPS = ['egg_1']
+ self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
+
+ def test_existing(self):
+ "A template can be loaded from an egg"
+ settings.INSTALLED_APPS = ['egg_1']
+ contents, template_name = lts_egg("y.html")
+ self.assertEqual(contents, "y")
+ self.assertEqual(template_name, "egg:egg_1:templates/y.html")
+
+ def test_not_installed(self):
+ "Loading an existent template from an egg not included in INSTALLED_APPS should fail"
+ settings.INSTALLED_APPS = []
+ self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
+
+
+if __name__ == "__main__":
+ unittest.main()
@@ -19,12 +19,15 @@
from unicode import unicode_tests
from context import context_tests
+
+from loaders import *
+
import filters
# Some other tests we would like to run
__test__ = {
- 'unicode': unicode_tests,
- 'context': context_tests,
+ 'unicode': unicode_tests,
+ 'context': context_tests
}
#################################

0 comments on commit d53e8f1

Please sign in to comment.