Ticket #18985: 18985-4.diff

File 18985-4.diff, 6.2 KB (added by Claude Paroz, 12 years ago)

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

  • django/conf/__init__.py

    diff --git a/django/conf/__init__.py b/django/conf/__init__.py
    index b00c8d5..051ab70 100644
    a b a list of all possible variables.  
    88
    99import logging
    1010import os
     11import sys
    1112import time     # Needed for Windows
    1213import warnings
    1314
    class LazySettings(LazyObject):  
    5657        """
    5758        Setup logging from LOGGING_CONFIG and LOGGING settings.
    5859        """
    59         try:
    60             # Route warnings through python logging
    61             logging.captureWarnings(True)
    62             # Allow DeprecationWarnings through the warnings filters
    63             warnings.simplefilter("default", DeprecationWarning)
    64         except AttributeError:
    65             # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
    66             pass
     60        if not sys.warnoptions:
     61            try:
     62                # Route warnings through python logging
     63                logging.captureWarnings(True)
     64                # Allow DeprecationWarnings through the warnings filters
     65                warnings.simplefilter("default", DeprecationWarning)
     66            except AttributeError:
     67                # No captureWarnings on Python 2.6, DeprecationWarnings are on anyway
     68                pass
    6769
    6870        if self.LOGGING_CONFIG:
    6971            from django.utils.log import DEFAULT_LOGGING
  • django/core/management/commands/test.py

    diff --git a/django/core/management/commands/test.py b/django/core/management/commands/test.py
    index 48b330a..2b8e801 100644
    a b  
     1import logging
    12import sys
    23import os
    34from optparse import make_option, OptionParser
    from django.conf import settings  
    67from django.core.management.base import BaseCommand
    78from django.test.utils import get_runner
    89
     10
    911class Command(BaseCommand):
    1012    option_list = BaseCommand.option_list + (
    1113        make_option('--noinput',
    class Command(BaseCommand):  
    5759                            version=self.get_version(),
    5860                            option_list=options)
    5961
     62    def execute(self, *args, **options):
     63        if int(options['verbosity']) > 0:
     64            # ensure that deprecation warnings are displayed during testing
     65            # the following state is assumed:
     66            # logging.capturewarnings is true
     67            # a "default" level warnings filter has been added for
     68            # DeprecationWarning. See django.conf.LazySettings._configure_logging
     69            logger = logging.getLogger('py.warnings')
     70            handler = logging.StreamHandler()
     71            logger.addHandler(handler)
     72        super(Command, self).execute(*args, **options)
     73        if int(options['verbosity']) > 0:
     74            # remove the testing-specific handler
     75            logger.removeHandler(handler)
     76
    6077    def handle(self, *test_labels, **options):
    6178        from django.conf import settings
    6279        from django.test.utils import get_runner
  • django/test/simple.py

    diff --git a/django/test/simple.py b/django/test/simple.py
    index 8faf1e4..bf0219d 100644
    a b  
    1 import logging
    21import unittest as real_unittest
    32
    43from django.conf import settings
    class DjangoTestSuiteRunner(object):  
    366365        self.setup_test_environment()
    367366        suite = self.build_suite(test_labels, extra_tests)
    368367        old_config = self.setup_databases()
    369         if self.verbosity > 0:
    370             # ensure that deprecation warnings are displayed during testing
    371             # the following state is assumed:
    372             # logging.capturewarnings is true
    373             # a "default" level warnings filter has been added for
    374             # DeprecationWarning. See django.conf.LazySettings._configure_logging
    375             logger = logging.getLogger('py.warnings')
    376             handler = logging.StreamHandler()
    377             logger.addHandler(handler)
    378368        result = self.run_suite(suite)
    379         if self.verbosity > 0:
    380             # remove the testing-specific handler
    381             logger.removeHandler(handler)
    382369        self.teardown_databases(old_config)
    383370        self.teardown_test_environment()
    384371        return self.suite_result(suite, result)
  • tests/regressiontests/test_runner/deprecation_app/tests.py

    diff --git a/tests/regressiontests/test_runner/deprecation_app/tests.py b/tests/regressiontests/test_runner/deprecation_app/tests.py
    index e676c3e..6dee088 100644
    a b import warnings  
    22
    33from django.test import TestCase
    44
     5warnings.warn("module-level warning from deprecation_app", DeprecationWarning)
     6
    57class DummyTest(TestCase):
    68    def test_warn(self):
    79        warnings.warn("warning from test", DeprecationWarning)
  • tests/regressiontests/test_runner/tests.py

    diff --git a/tests/regressiontests/test_runner/tests.py b/tests/regressiontests/test_runner/tests.py
    index 5df421c..b5e4a1d 100644
    a b class DeprecationDisplayTest(AdminScriptTestCase):  
    298298    def test_runner_deprecation_verbosity_default(self):
    299299        args = ['test', '--settings=regressiontests.settings']
    300300        out, err = self.run_django_admin(args)
    301         self.assertTrue("DeprecationWarning: warning from test" in err)
     301        self.assertIn("DeprecationWarning: warning from test", err)
     302        self.assertIn("DeprecationWarning: module-level warning from deprecation_app", err)
    302303
    303304    @unittest.skipIf(sys.version_info[:2] == (2, 6),
    304305        "On Python 2.6, DeprecationWarnings are visible anyway")
  • tests/runtests.py

    diff --git a/tests/runtests.py b/tests/runtests.py
    index 8c56e27..90dc0f5 100755
    a b  
    11#!/usr/bin/env python
     2import logging
    23import os
    34import shutil
    45import subprocess
    def setup(verbosity, test_labels):  
    106107    # in our tests.
    107108    settings.MANAGERS = ("admin@djangoproject.com",)
    108109
     110    if verbosity > 0:
     111        # Ensure py.warnings are piped through a verbose logging handler
     112        logger = logging.getLogger('py.warnings')
     113        handler = logging.StreamHandler()
     114        logger.addHandler(handler)
     115
    109116    # Load all the ALWAYS_INSTALLED_APPS.
    110117    # (This import statement is intentionally delayed until after we
    111118    # access settings because of the USE_I18N dependency.)
Back to Top