Code

Ticket #12658: patch+tests.diff

File patch+tests.diff, 5.6 KB (added by boxm, 3 years ago)

Patch and testcase for issue, against 1.3

Line 
1diff --git a/webserver/lib/django-1.3/django/test/simple.py b/webserver/lib/django-1.3/django/test/simple.py
2index 1b26ebb..ac05604 100644
3--- a/webserver/lib/django-1.3/django/test/simple.py
4+++ b/webserver/lib/django-1.3/django/test/simple.py
5@@ -38,21 +38,22 @@ def get_tests(app_module):
6     except ImportError, e:
7         # Couldn't import tests.py. Was it due to a missing file, or
8         # due to an import error in a tests.py that actually exists?
9-        import os.path
10-        from imp import find_module
11-        try:
12-            mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
13-        except ImportError:
14-            # 'tests' module doesn't exist. Move on.
15+        # app_module either points to a models.py file, or models/__init__.py
16+        # Tests are therefore either in same directory, or one level up
17+        from django.utils.module_loading import module_has_submodule
18+        if app_module.__name__.split('.')[-1] == 'models':
19+            root_name = '.'.join(app_module.__name__.split('.')[:-1])
20+            __import__(root_name)
21+            app_root = sys.modules[root_name]
22+        else:
23+            app_root = app_module
24+
25+        if not module_has_submodule(app_root, 'tests'):
26             test_module = None
27+           
28         else:
29             # The module exists, so there must be an import error in the
30-            # test module itself. We don't need the module; so if the
31-            # module was a single file module (i.e., tests.py), close the file
32-            # handle returned by find_module. Otherwise, the test module
33-            # is a directory, and there is nothing to close.
34-            if mod[0]:
35-                mod[0].close()
36+            # test module itself.
37             raise
38     return test_module
39 
40diff --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
41new file mode 100644
42index 0000000..a455e83
43--- /dev/null
44+++ b/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/__init__.py
45@@ -0,0 +1,2 @@
46+# Example of directory model & test layout that causes issue #12658
47+# The tests raise an ImportError so this test is marked as invalid
48diff --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
49new file mode 100644
50index 0000000..e69de29
51diff --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
52new file mode 100644
53index 0000000..1054d18
54--- /dev/null
55+++ b/webserver/lib/django-1.3/tests/regressiontests/invalid_tests_importerror/tests/__init__.py
56@@ -0,0 +1,3 @@
57+# Tests that raise ImportError should not fail silently.
58+# This is a support fixture for test_get_tests
59+raise ImportError
60diff --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
61new file mode 100644
62index 0000000..e69de29
63diff --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
64new file mode 100644
65index 0000000..e69de29
66diff --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
67new file mode 100644
68index 0000000..0542fd3
69--- /dev/null
70+++ b/webserver/lib/django-1.3/tests/regressiontests/test_get_tests/tests.py
71@@ -0,0 +1,30 @@
72+import sys
73+
74+from django.core.exceptions import ImproperlyConfigured
75+from django.test import simple
76+from django.utils import unittest
77+from django.test.simple import get_tests
78+from django.db.models.loading import get_app
79+
80+TEST_APP_OK = 'test_model_and_tests_package.models'
81+TEST_APP_ERROR = 'invalid_tests_importerror.models'
82+
83+def load_test_app(name):
84+    """Load a non-registered app in a sibling directory to this test"""
85+    path = __package__.split('.')[:-1]
86+    name = '.'.join(path+[name])
87+    __import__(name)
88+    return sys.modules[name]
89+
90+class GetTestTests(unittest.TestCase):
91+    def test_get_tests(self):
92+        "Check we can find tests in directory"
93+        module = load_test_app(TEST_APP_OK)
94+        tests = get_tests(module)
95+        self.assert_(isinstance(tests, type(module)))
96+           
97+    def test_import_error(self):
98+        "Test for #12658 - tests with importerrors fail silently"
99+        module = load_test_app(TEST_APP_ERROR)
100+        self.assertRaises(ImportError, get_tests, module)
101+
102diff --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
103new file mode 100644
104index 0000000..3ad4f58
105--- /dev/null
106+++ b/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/__init__.py
107@@ -0,0 +1 @@
108+# Example of directory model & test layout that causes issue #12658
109diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/models/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/models/__init__.py
110new file mode 100644
111index 0000000..e69de29
112diff --git a/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/tests/__init__.py b/webserver/lib/django-1.3/tests/regressiontests/test_model_and_tests_package/tests/__init__.py
113new file mode 100644
114index 0000000..e69de29