Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added a ``checksetup`` management command for verifying Django compat…

…ibility.
  • Loading branch information...
commit 91f317c76d503b4bcae5a26c230425944dbf4ea8 1 parent 70d7e45
Daniel Lindsley authored June 13, 2013
0  django/core/compat_checks/__init__.py
No changes.
39  django/core/compat_checks/base.py
... ...
@@ -0,0 +1,39 @@
  1
+from __future__ import unicode_literals
  2
+import warnings
  3
+
  4
+from django.core.compat_checks import django_1_6_0
  5
+
  6
+
  7
+COMPAT_CHECKS = [
  8
+    # Add new modules at the top, so we keep things in descending order.
  9
+    # After two-three minor releases, old versions should get dropped.
  10
+    django_1_6_0,
  11
+]
  12
+
  13
+
  14
+def check_compatibility():
  15
+    """
  16
+    Runs through compatibility checks to warn the user with an existing install
  17
+    about changes in an up-to-date Django.
  18
+
  19
+    Modules should be located in ``django.core.compat_checks`` (typically one
  20
+    per release of Django) & must have a ``run_checks`` function that runs
  21
+    all the checks.
  22
+
  23
+    Returns a list of informational messages about incompatibilities.
  24
+    """
  25
+    messages = []
  26
+
  27
+    for check_module in COMPAT_CHECKS:
  28
+        check = getattr(check_module, u'run_checks', None)
  29
+
  30
+        if check is None:
  31
+            warnings.warn(
  32
+                u"The '%s' module lacks a " % check_module.__name__ +
  33
+                u"'run_checks' method, which is needed to verify compatibility."
  34
+            )
  35
+            continue
  36
+
  37
+        messages.extend(check())
  38
+
  39
+    return messages
37  django/core/compat_checks/django_1_6_0.py
... ...
@@ -0,0 +1,37 @@
  1
+from __future__ import unicode_literals
  2
+
  3
+
  4
+def check_test_runner():
  5
+    """
  6
+    Checks if the user has *not* overridden the ``TEST_RUNNER`` setting &
  7
+    warns them about the default behavior changes.
  8
+
  9
+    If the user has overridden that setting, we presume they know what they're
  10
+    doing & avoid generating a message.
  11
+    """
  12
+    from django.conf import settings
  13
+    new_default = u'django.test.runner.DiscoverRunner'
  14
+    test_runner_setting = getattr(settings, u'TEST_RUNNER', new_default)
  15
+
  16
+    if test_runner_setting == new_default:
  17
+        message = [
  18
+            u"You have not explicitly set 'TEST_RUNNER'. In Django 1.6,",
  19
+            u"there is a new test runner ('%s')" % new_default,
  20
+            u"by default. You should ensure your tests are still all",
  21
+            u"running & behaving as expected. See",
  22
+            u"https://docs.djangoproject.com/en/dev/releases/1.6/#discovery-of-tests-in-any-test-module",
  23
+            u"for more information.",
  24
+        ]
  25
+        return u' '.join(message)
  26
+
  27
+
  28
+def run_checks():
  29
+    """
  30
+    Required by the ``checksetup`` management command, this returns a list of
  31
+    messages from all the relevant check functions for this version of Django.
  32
+    """
  33
+    checks = [
  34
+        check_test_runner()
  35
+    ]
  36
+    # Filter out the ``None`` or empty strings.
  37
+    return [output for output in checks if output]
14  django/core/management/commands/checksetup.py
... ...
@@ -0,0 +1,14 @@
  1
+from __future__ import unicode_literals
  2
+import warnings
  3
+
  4
+from django.core.compat_checks.base import check_compatibility
  5
+from django.core.management.base import NoArgsCommand
  6
+
  7
+
  8
+class Command(NoArgsCommand):
  9
+    help = u"Checks your configuration's compatibility with this version " + \
  10
+           u"of Django."
  11
+
  12
+    def handle_noargs(self, **options):
  13
+        for message in check_compatibility():
  14
+            warnings.warn(message)
7  docs/releases/1.6.txt
@@ -121,6 +121,13 @@ GeoDjango now provides :ref:`form fields and widgets <ref-gis-forms-api>` for
121 121
 its geo-specialized fields. They are OpenLayers-based by default, but they can
122 122
 be customized to use any other JS framework.
123 123
 
  124
+``checksetup`` management command added for verifying compatibility
  125
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126
+
  127
+A ``checksetup`` management command was added, enabling you to verify if your
  128
+current configuration (currently oriented at settings) is compatible with the
  129
+current version of Django.
  130
+
124 131
 Minor features
125 132
 ~~~~~~~~~~~~~~
126 133
 
0  tests/compat_checks/__init__.py
No changes.
1  tests/compat_checks/models.py
... ...
@@ -0,0 +1 @@
  1
+# Stubby.
107  tests/compat_checks/tests.py
... ...
@@ -0,0 +1,107 @@
  1
+from django.core.compat_checks import base
  2
+from django.core.compat_checks import django_1_6_0
  3
+from django.core.management.commands import checksetup
  4
+from django.core.management import call_command
  5
+from django.test import TestCase
  6
+
  7
+
  8
+class StubCheckModule(object):
  9
+    # Has no ``run_checks`` attribute & will trigger a warning.
  10
+    __name__ = 'StubCheckModule'
  11
+
  12
+
  13
+class FakeWarnings(object):
  14
+    def __init__(self):
  15
+        self._warnings = []
  16
+
  17
+    def warn(self, message):
  18
+        self._warnings.append(message)
  19
+
  20
+
  21
+class CompatChecksTestCase(TestCase):
  22
+    def setUp(self):
  23
+        super(CompatChecksTestCase, self).setUp()
  24
+
  25
+        # We're going to override the list of checks to perform for test
  26
+        # consistency in the future.
  27
+        self.old_compat_checks = base.COMPAT_CHECKS
  28
+        base.COMPAT_CHECKS = [
  29
+            django_1_6_0,
  30
+        ]
  31
+
  32
+    def tearDown(self):
  33
+        # Restore what's supposed to be in ``COMPAT_CHECKS``.
  34
+        base.COMPAT_CHECKS = self.old_compat_checks
  35
+        super(CompatChecksTestCase, self).tearDown()
  36
+
  37
+    def test_check_test_runner_new_default(self):
  38
+        with self.settings(TEST_RUNNER='django.test.runner.DiscoverRunner'):
  39
+            result = django_1_6_0.check_test_runner()
  40
+            self.assertTrue("You have not explicitly set 'TEST_RUNNER'" in result)
  41
+
  42
+    def test_check_test_runner_overridden(self):
  43
+        with self.settings(TEST_RUNNER='myapp.test.CustomRunnner'):
  44
+            self.assertEqual(django_1_6_0.check_test_runner(), None)
  45
+
  46
+    def test_run_checks_new_default(self):
  47
+        with self.settings(TEST_RUNNER='django.test.runner.DiscoverRunner'):
  48
+            result = django_1_6_0.run_checks()
  49
+            self.assertEqual(len(result), 1)
  50
+            self.assertTrue("You have not explicitly set 'TEST_RUNNER'" in result[0])
  51
+
  52
+    def test_run_checks_overridden(self):
  53
+        with self.settings(TEST_RUNNER='myapp.test.CustomRunnner'):
  54
+            self.assertEqual(len(django_1_6_0.run_checks()), 0)
  55
+
  56
+    def test_check_compatibility(self):
  57
+        with self.settings(TEST_RUNNER='django.test.runner.DiscoverRunner'):
  58
+            result = base.check_compatibility()
  59
+            self.assertEqual(len(result), 1)
  60
+            self.assertTrue("You have not explicitly set 'TEST_RUNNER'" in result[0])
  61
+
  62
+        with self.settings(TEST_RUNNER='myapp.test.CustomRunnner'):
  63
+            self.assertEqual(len(base.check_compatibility()), 0)
  64
+
  65
+    def test_check_compatibility_warning(self):
  66
+        # First, we're patching over the ``COMPAT_CHECKS`` with a stub which
  67
+        # will trigger the warning.
  68
+        base.COMPAT_CHECKS = [
  69
+            StubCheckModule(),
  70
+        ]
  71
+
  72
+        # Next, we unfortunately have to patch out ``warnings``.
  73
+        old_warnings = base.warnings
  74
+        base.warnings = FakeWarnings()
  75
+
  76
+        self.assertEqual(len(base.warnings._warnings), 0)
  77
+
  78
+        with self.settings(TEST_RUNNER='myapp.test.CustomRunnner'):
  79
+            self.assertEqual(len(base.check_compatibility()), 0)
  80
+
  81
+        self.assertEqual(len(base.warnings._warnings), 1)
  82
+        self.assertTrue("The 'StubCheckModule' module lacks a 'run_checks'" in base.warnings._warnings[0])
  83
+
  84
+        # Restore the ``warnings``.
  85
+        base.warnings = old_warnings
  86
+
  87
+    def test_management_command(self):
  88
+        # Again, we unfortunately have to patch out ``warnings``. Different
  89
+        old_warnings = checksetup.warnings
  90
+        checksetup.warnings = FakeWarnings()
  91
+
  92
+        self.assertEqual(len(checksetup.warnings._warnings), 0)
  93
+
  94
+        # Should not produce any warnings.
  95
+        with self.settings(TEST_RUNNER='myapp.test.CustomRunnner'):
  96
+            call_command('checksetup')
  97
+
  98
+        self.assertEqual(len(checksetup.warnings._warnings), 0)
  99
+
  100
+        with self.settings(TEST_RUNNER='django.test.runner.DiscoverRunner'):
  101
+            call_command('checksetup')
  102
+
  103
+        self.assertEqual(len(checksetup.warnings._warnings), 1)
  104
+        self.assertTrue("You have not explicitly set 'TEST_RUNNER'" in checksetup.warnings._warnings[0])
  105
+
  106
+        # Restore the ``warnings``.
  107
+        base.warnings = old_warnings

0 notes on commit 91f317c

Please sign in to comment.
Something went wrong with that request. Please try again.