Fixed #18985 -- ensure module level deprecations are displayed

Also don't compete with -W CLI option.

Thanks to Aymeric Augustin for the catch, and Claude Paroz for the patch.
This commit is contained in:
Preston Holmes 2013-03-24 21:34:46 -07:00
parent 9c4882b391
commit e79b857a07
6 changed files with 40 additions and 22 deletions

View File

@ -8,6 +8,7 @@ a list of all possible variables.
import logging import logging
import os import os
import sys
import time # Needed for Windows import time # Needed for Windows
import warnings import warnings
@ -57,6 +58,7 @@ class LazySettings(LazyObject):
""" """
Setup logging from LOGGING_CONFIG and LOGGING settings. Setup logging from LOGGING_CONFIG and LOGGING settings.
""" """
if not sys.warnoptions:
try: try:
# Route warnings through python logging # Route warnings through python logging
logging.captureWarnings(True) logging.captureWarnings(True)

View File

@ -1,3 +1,4 @@
import logging
import sys import sys
import os import os
from optparse import make_option, OptionParser from optparse import make_option, OptionParser
@ -6,6 +7,7 @@ from django.conf import settings
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.test.utils import get_runner from django.test.utils import get_runner
class Command(BaseCommand): class Command(BaseCommand):
option_list = BaseCommand.option_list + ( option_list = BaseCommand.option_list + (
make_option('--noinput', make_option('--noinput',
@ -57,6 +59,21 @@ class Command(BaseCommand):
version=self.get_version(), version=self.get_version(),
option_list=options) option_list=options)
def execute(self, *args, **options):
if int(options['verbosity']) > 0:
# ensure that deprecation warnings are displayed during testing
# the following state is assumed:
# logging.capturewarnings is true
# a "default" level warnings filter has been added for
# DeprecationWarning. See django.conf.LazySettings._configure_logging
logger = logging.getLogger('py.warnings')
handler = logging.StreamHandler()
logger.addHandler(handler)
super(Command, self).execute(*args, **options)
if int(options['verbosity']) > 0:
# remove the testing-specific handler
logger.removeHandler(handler)
def handle(self, *test_labels, **options): def handle(self, *test_labels, **options):
from django.conf import settings from django.conf import settings
from django.test.utils import get_runner from django.test.utils import get_runner

View File

@ -1,4 +1,3 @@
import logging
import unittest as real_unittest import unittest as real_unittest
from django.conf import settings from django.conf import settings
@ -366,19 +365,7 @@ class DjangoTestSuiteRunner(object):
self.setup_test_environment() self.setup_test_environment()
suite = self.build_suite(test_labels, extra_tests) suite = self.build_suite(test_labels, extra_tests)
old_config = self.setup_databases() old_config = self.setup_databases()
if self.verbosity > 0:
# ensure that deprecation warnings are displayed during testing
# the following state is assumed:
# logging.capturewarnings is true
# a "default" level warnings filter has been added for
# DeprecationWarning. See django.conf.LazySettings._configure_logging
logger = logging.getLogger('py.warnings')
handler = logging.StreamHandler()
logger.addHandler(handler)
result = self.run_suite(suite) result = self.run_suite(suite)
if self.verbosity > 0:
# remove the testing-specific handler
logger.removeHandler(handler)
self.teardown_databases(old_config) self.teardown_databases(old_config)
self.teardown_test_environment() self.teardown_test_environment()
return self.suite_result(suite, result) return self.suite_result(suite, result)

View File

@ -1,4 +1,5 @@
#!/usr/bin/env python #!/usr/bin/env python
import logging
import os import os
import shutil import shutil
import subprocess import subprocess
@ -82,6 +83,14 @@ def setup(verbosity, test_labels):
settings.LANGUAGE_CODE = 'en' settings.LANGUAGE_CODE = 'en'
settings.SITE_ID = 1 settings.SITE_ID = 1
if verbosity > 0:
# Ensure any warnings captured to logging are piped through a verbose
# logging handler. If any -W options were passed explicitly on command
# line, warnings are not captured, and this has no effect.
logger = logging.getLogger('py.warnings')
handler = logging.StreamHandler()
logger.addHandler(handler)
# Load all the ALWAYS_INSTALLED_APPS. # Load all the ALWAYS_INSTALLED_APPS.
get_apps() get_apps()

View File

@ -2,6 +2,8 @@ import warnings
from django.test import TestCase from django.test import TestCase
warnings.warn("module-level warning from deprecation_app", DeprecationWarning)
class DummyTest(TestCase): class DummyTest(TestCase):
def test_warn(self): def test_warn(self):
warnings.warn("warning from test", DeprecationWarning) warnings.warn("warning from test", DeprecationWarning)

View File

@ -299,7 +299,8 @@ class DeprecationDisplayTest(AdminScriptTestCase):
def test_runner_deprecation_verbosity_default(self): def test_runner_deprecation_verbosity_default(self):
args = ['test', '--settings=test_project.settings'] args = ['test', '--settings=test_project.settings']
out, err = self.run_django_admin(args) out, err = self.run_django_admin(args)
self.assertTrue("DeprecationWarning: warning from test" in err) self.assertIn("DeprecationWarning: warning from test", err)
self.assertIn("DeprecationWarning: module-level warning from deprecation_app", err)
@unittest.skipIf(sys.version_info[:2] == (2, 6), @unittest.skipIf(sys.version_info[:2] == (2, 6),
"On Python 2.6, DeprecationWarnings are visible anyway") "On Python 2.6, DeprecationWarnings are visible anyway")