Ticket #12658: patch+tests.diff

File patch+tests.diff, 5.6 KB (added by Malcolm Box, 14 years ago)

Patch and testcase for issue, against 1.3

  • webserver/lib/django-1.3/django/test/simple.py

    diff --git a/webserver/lib/django-1.3/django/test/simple.py b/webserver/lib/django-1.3/django/test/simple.py
    index 1b26ebb..ac05604 100644
    a b def get_tests(app_module):  
    3838    except ImportError, e:
    3939        # Couldn't import tests.py. Was it due to a missing file, or
    4040        # due to an import error in a tests.py that actually exists?
    41         import os.path
    42         from imp import find_module
    43         try:
    44             mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
    45         except ImportError:
    46             # 'tests' module doesn't exist. Move on.
     41        # app_module either points to a models.py file, or models/__init__.py
     42        # Tests are therefore either in same directory, or one level up
     43        from django.utils.module_loading import module_has_submodule
     44        if app_module.__name__.split('.')[-1] == 'models':
     45            root_name = '.'.join(app_module.__name__.split('.')[:-1])
     46            __import__(root_name)
     47            app_root = sys.modules[root_name]
     48        else:
     49            app_root = app_module
     50
     51        if not module_has_submodule(app_root, 'tests'):
    4752            test_module = None
     53           
    4854        else:
    4955            # The module exists, so there must be an import error in the
    50             # test module itself. We don't need the module; so if the
    51             # module was a single file module (i.e., tests.py), close the file
    52             # handle returned by find_module. Otherwise, the test module
    53             # is a directory, and there is nothing to close.
    54             if mod[0]:
    55                 mod[0].close()
     56            # test module itself.
    5657            raise
    5758    return test_module
    5859
  • new file webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/__init__.py

    diff --git a/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/__init__.py
    new file mode 100644
    index 0000000..a455e83
    - +  
     1# Example of directory model & test layout that causes issue #12658
     2# The tests raise an ImportError so this test is marked as invalid
  • new file webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/tests/__init__.py

    diff --git a/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/models/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/models/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/tests/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/tests/__init__.py
    new file mode 100644
    index 0000000..1054d18
    - +  
     1# Tests that raise ImportError should not fail silently.
     2# This is a support fixture for test_get_tests
     3raise ImportError
  • new file webserver/lib/django-1.3/tests/regressiontests/test_get_tests/tests.py

    diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/models.py b/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/tests.py b/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/tests.py
    new file mode 100644
    index 0000000..0542fd3
    - +  
     1import sys
     2
     3from django.core.exceptions import ImproperlyConfigured
     4from django.test import simple
     5from django.utils import unittest
     6from django.test.simple import get_tests
     7from django.db.models.loading import get_app
     8
     9TEST_APP_OK = 'test_model_and_tests_package.models'
     10TEST_APP_ERROR = 'invalid_tests_importerror.models'
     11
     12def load_test_app(name):
     13    """Load a non-registered app in a sibling directory to this test"""
     14    path = __package__.split('.')[:-1]
     15    name = '.'.join(path+[name])
     16    __import__(name)
     17    return sys.modules[name]
     18
     19class GetTestTests(unittest.TestCase):
     20    def test_get_tests(self):
     21        "Check we can find tests in directory"
     22        module = load_test_app(TEST_APP_OK)
     23        tests = get_tests(module)
     24        self.assert_(isinstance(tests, type(module)))
     25           
     26    def test_import_error(self):
     27        "Test for #12658 - tests with importerrors fail silently"
     28        module = load_test_app(TEST_APP_ERROR)
     29        self.assertRaises(ImportError, get_tests, module)
     30
  • new file webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/__init__.py

    diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/__init__.py
    new file mode 100644
    index 0000000..3ad4f58
    - +  
     1# Example of directory model & test layout that causes issue #12658
Back to Top