Ticket #6430: egg_template_loader.2.patch

File egg_template_loader.2.patch, 3.6 KB (added by Juan Manuel Caicedo <juan.manuel.caicedo@…>, 17 years ago)

Updated patch for this bug, now includes a test case.

  • django/template/loaders/eggs.py

     
    1818        pkg_name = 'templates/' + template_name
    1919        for app in settings.INSTALLED_APPS:
    2020            try:
    21                 return (resource_string(app, pkg_name), 'egg:%s:%s ' % (app, pkg_name)).decode(settings.FILE_CHARSET)
     21                return (resource_string(app, pkg_name).decode(settings.FILE_CHARSET), 'egg:%s:%s' % (app, pkg_name))
    2222            except:
    2323                pass
    2424    raise TemplateDoesNotExist, template_name
  • tests/regressiontests/templates/loaders.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3Test cases for the template loaders
     4"""
     5
     6from django.conf import settings
     7
     8if __name__ == '__main__':
     9    settings.configure()
     10
     11import unittest
     12import sys
     13import pkg_resources
     14import imp
     15import StringIO
     16
     17from django.template import TemplateDoesNotExist
     18from django.template.loaders.eggs import load_template_source as lts_egg
     19
     20
     21#Mock classes and objects for pkg_resources functions
     22class MockProvider(pkg_resources.NullProvider):
     23
     24    def __init__(self, module):
     25        pkg_resources.NullProvider.__init__(self, module)
     26        self.module = module
     27
     28    def _has(self, path):
     29        return path in self.module._resources
     30
     31    def _isdir(self,path):
     32        return False
     33
     34    def get_resource_stream(self, manager, resource_name):
     35        return self.module._resources[resource_name]
     36
     37    def _get(self, path):
     38        return self.module._resources[path].read()
     39
     40class MockLoader(object): pass
     41
     42
     43
     44def create_egg(name, resources):
     45    """
     46    Creates a mock egg with a list of resources
     47   
     48    name: The name of the module
     49    resources: A dictionary of resources. Keys are the names and values the the data.
     50   
     51    """
     52    egg = imp.new_module(name)
     53    egg.__loader__ = MockLoader()
     54    egg._resources = resources
     55    sys.modules[name] = egg
     56
     57
     58class EggLoader(unittest.TestCase):
     59
     60    def setUp(self):
     61        pkg_resources._provider_factories[MockLoader] = MockProvider
     62       
     63        self.empty_egg = create_egg("egg_emtpy", {})
     64        self.egg_1 = create_egg("egg_1", {
     65            'templates/y.html' : StringIO.StringIO("y"),
     66            'templates/x.txt' : StringIO.StringIO("x"),
     67        })
     68        settings.INSTALLED_APPS = []
     69
     70   
     71    def test_emtpy(self):
     72        """
     73        Loading any template on an empty egg should fail
     74        """
     75        settings.INSTALLED_APPS = ['egg_emtpy']
     76        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
     77
     78    def test_non_existing(self):
     79        settings.INSTALLED_APPS = ['egg_emtpy']
     80        self.assertRaises(TemplateDoesNotExist, lts_egg, "not-existing.html")
     81   
     82    def test_existing(self):
     83        settings.INSTALLED_APPS = ['egg_1']
     84        contents, template_name = lts_egg("y.html")
     85        self.assertEqual(contents, "y")
     86        self.assertEqual(template_name, "egg:egg_1:templates/y.html")
     87   
     88    def test_not_installed(self):
     89        """
     90        Loading an existing template on a egg not included in INSTALLED_APPS
     91        should fail
     92        """
     93        settings.INSTALLED_APPS = []
     94        self.assertRaises(TemplateDoesNotExist, lts_egg, "y.html")
     95
     96   
     97if __name__ == "__main__":
     98    unittest.main()
Back to Top