From 8f3aefdec33f6cb4bdda142ff9f7fa423c0bebbd Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 25 Jul 2013 17:17:40 -0500 Subject: [PATCH] Fixed handling of template loader tests. Previously, the CachedLoaderTests were never run at all. --- .../{loaders.py => test_loaders.py} | 43 +++++++++++-------- tests/template_tests/tests.py | 14 +----- 2 files changed, 27 insertions(+), 30 deletions(-) rename tests/template_tests/{loaders.py => test_loaders.py} (86%) diff --git a/tests/template_tests/loaders.py b/tests/template_tests/test_loaders.py similarity index 86% rename from tests/template_tests/loaders.py rename to tests/template_tests/test_loaders.py index 497b422e7f..6fd17fd04b 100644 --- a/tests/template_tests/loaders.py +++ b/tests/template_tests/test_loaders.py @@ -11,10 +11,15 @@ if __name__ == '__main__': import imp import os.path -import pkg_resources import sys import unittest +try: + import pkg_resources +except ImportError: + pkg_resources = None + + from django.template import TemplateDoesNotExist, Context from django.template.loaders.eggs import Loader as EggLoader from django.template import loader @@ -24,23 +29,6 @@ from django.utils.six import StringIO # 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 @@ -57,8 +45,27 @@ def create_egg(name, resources): sys.modules[name] = egg +@unittest.skipUnless(pkg_resources, 'setuptools is not installed') class EggLoaderTest(unittest.TestCase): def setUp(self): + # Defined here b/c at module scope we may not have pkg_resources + 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() + pkg_resources._provider_factories[MockLoader] = MockProvider self.empty_egg = create_egg("egg_empty", {}) diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py index 6b8a106623..81f4682f16 100644 --- a/tests/template_tests/tests.py +++ b/tests/template_tests/tests.py @@ -8,8 +8,7 @@ if __name__ == '__main__': # before importing 'template'. settings.configure() -from datetime import date, datetime, timedelta -import time +from datetime import date, datetime import os import sys import traceback @@ -31,21 +30,12 @@ from django.test.utils import (setup_test_template_loader, from django.utils.encoding import python_2_unicode_compatible from django.utils.formats import date_format from django.utils._os import upath -from django.utils.translation import activate, deactivate, ugettext as _ +from django.utils.translation import activate, deactivate from django.utils.safestring import mark_safe from django.utils import six -from django.utils.tzinfo import LocalTimezone from i18n import TransRealMixin -try: - from .loaders import RenderToStringTest, EggLoaderTest -except ImportError as e: - if "pkg_resources" in e.args[0]: - pass # If setuptools isn't installed, that's fine. Just move on. - else: - raise - # NumPy installed? try: import numpy