Code

Ticket #18985: 18985-4.diff

File 18985-4.diff, 6.2 KB (added by claudep, 13 months ago)

Added test for module-level warnings, and let -W option take priority

Line 
1diff --git a/django/conf/__init__.py b/django/conf/__init__.py
2index b00c8d5..051ab70 100644
3--- a/django/conf/__init__.py
4+++ b/django/conf/__init__.py
5@@ -8,6 +8,7 @@ a list of all possible variables.
6 
7 import logging
8 import os
9+import sys
10 import time     # Needed for Windows
11 import warnings
12 
13@@ -56,14 +57,15 @@ class LazySettings(LazyObject):
14         """
15         Setup logging from LOGGING_CONFIG and LOGGING settings.
16         """
17-        try:
18-            # Route warnings through python logging
19-            logging.captureWarnings(True)
20-            # Allow DeprecationWarnings through the warnings filters
21-            warnings.simplefilter("default", DeprecationWarning)
22-        except AttributeError:
23-            # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
24-            pass
25+        if not sys.warnoptions:
26+            try:
27+                # Route warnings through python logging
28+                logging.captureWarnings(True)
29+                # Allow DeprecationWarnings through the warnings filters
30+                warnings.simplefilter("default", DeprecationWarning)
31+            except AttributeError:
32+                # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
33+                pass
34 
35         if self.LOGGING_CONFIG:
36             from django.utils.log import DEFAULT_LOGGING
37diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py
38index 48b330a..2b8e801 100644
39--- a/django/core/management/commands/test.py
40+++ b/django/core/management/commands/test.py
41@@ -1,3 +1,4 @@
42+import logging
43 import sys
44 import os
45 from optparse import make_option, OptionParser
46@@ -6,6 +7,7 @@ from django.conf import settings
47 from django.core.management.base import BaseCommand
48 from django.test.utils import get_runner
49 
50+
51 class Command(BaseCommand):
52     option_list = BaseCommand.option_list + (
53         make_option('--noinput',
54@@ -57,6 +59,21 @@ class Command(BaseCommand):
55                             version=self.get_version(),
56                             option_list=options)
57 
58+    def execute(self, *args, **options):
59+        if int(options['verbosity']) > 0:
60+            # ensure that deprecation warnings are displayed during testing
61+            # the following state is assumed:
62+            # logging.capturewarnings is true
63+            # a "default" level warnings filter has been added for
64+            # DeprecationWarning. See django.conf.LazySettings._configure_logging
65+            logger = logging.getLogger('py.warnings')
66+            handler = logging.StreamHandler()
67+            logger.addHandler(handler)
68+        super(Command, self).execute(*args, **options)
69+        if int(options['verbosity']) > 0:
70+            # remove the testing-specific handler
71+            logger.removeHandler(handler)
72+
73     def handle(self, *test_labels, **options):
74         from django.conf import settings
75         from django.test.utils import get_runner
76diff --git a/django/test/simple.py b/django/test/simple.py
77index 8faf1e4..bf0219d 100644
78--- a/django/test/simple.py
79+++ b/django/test/simple.py
80@@ -1,4 +1,3 @@
81-import logging
82 import unittest as real_unittest
83 
84 from django.conf import settings
85@@ -366,19 +365,7 @@ class DjangoTestSuiteRunner(object):
86         self.setup_test_environment()
87         suite = self.build_suite(test_labels, extra_tests)
88         old_config = self.setup_databases()
89-        if self.verbosity > 0:
90-            # ensure that deprecation warnings are displayed during testing
91-            # the following state is assumed:
92-            # logging.capturewarnings is true
93-            # a "default" level warnings filter has been added for
94-            # DeprecationWarning. See django.conf.LazySettings._configure_logging
95-            logger = logging.getLogger('py.warnings')
96-            handler = logging.StreamHandler()
97-            logger.addHandler(handler)
98         result = self.run_suite(suite)
99-        if self.verbosity > 0:
100-            # remove the testing-specific handler
101-            logger.removeHandler(handler)
102         self.teardown_databases(old_config)
103         self.teardown_test_environment()
104         return self.suite_result(suite, result)
105diff --git a/tests/regressiontests/test_runner/deprecation_app/tests.py b/tests/regressiontests/test_runner/deprecation_app/tests.py
106index e676c3e..6dee088 100644
107--- a/tests/regressiontests/test_runner/deprecation_app/tests.py
108+++ b/tests/regressiontests/test_runner/deprecation_app/tests.py
109@@ -2,6 +2,8 @@ import warnings
110 
111 from django.test import TestCase
112 
113+warnings.warn("module-level warning from deprecation_app", DeprecationWarning)
114+
115 class DummyTest(TestCase):
116     def test_warn(self):
117         warnings.warn("warning from test", DeprecationWarning)
118diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py
119index 5df421c..b5e4a1d 100644
120--- a/tests/regressiontests/test_runner/tests.py
121+++ b/tests/regressiontests/test_runner/tests.py
122@@ -298,7 +298,8 @@ class DeprecationDisplayTest(AdminScriptTestCase):
123     def test_runner_deprecation_verbosity_default(self):
124         args = ['test', '--settings=regressiontests.settings']
125         out, err = self.run_django_admin(args)
126-        self.assertTrue("DeprecationWarning: warning from test" in err)
127+        self.assertIn("DeprecationWarning: warning from test", err)
128+        self.assertIn("DeprecationWarning: module-level warning from deprecation_app", err)
129 
130     @unittest.skipIf(sys.version_info[:2] == (2, 6),
131         "On Python 2.6, DeprecationWarnings are visible anyway")
132diff --git a/tests/runtests.py b/tests/runtests.py
133index 8c56e27..90dc0f5 100755
134--- a/tests/runtests.py
135+++ b/tests/runtests.py
136@@ -1,4 +1,5 @@
137 #!/usr/bin/env python
138+import logging
139 import os
140 import shutil
141 import subprocess
142@@ -106,6 +107,12 @@ def setup(verbosity, test_labels):
143     # in our tests.
144     settings.MANAGERS = ("admin@djangoproject.com",)
145 
146+    if verbosity > 0:
147+        # Ensure py.warnings are piped through a verbose logging handler
148+        logger = logging.getLogger('py.warnings')
149+        handler = logging.StreamHandler()
150+        logger.addHandler(handler)
151+
152     # Load all the ALWAYS_INSTALLED_APPS.
153     # (This import statement is intentionally delayed until after we
154     # access settings because of the USE_I18N dependency.)