Skip to content

Commit

Permalink
Merge pull request #124 from gatherhealth/master
Browse files Browse the repository at this point in the history
Fix optparse fallback logic in Django 1.8+
  • Loading branch information
ticosax committed Jan 4, 2016
2 parents 5560a21 + 66bb6a8 commit d827664
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions configurations/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ def create_parser(self, prog_name, subcommand):
if isinstance(parser, OptionParser):
# in case the option_list is set the create_parser
# will actually return a OptionParser for backward
# compatibility. It uses BaseCommand.use_argparse
# to decide that, which checks for the option_list list
base.BaseCommand.option_list += configuration_options
# compatibility. In that case we should tack our
# options on to the end of the parser on the way out.
for option in configuration_options:
parser.add_option(option)
else:
# probably argparse, let's not import argparse though
parser.add_argument(CONFIGURATION_ARGUMENT,
Expand Down
Empty file added tests/management/__init__.py
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions tests/management/commands/old_optparse_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from optparse import make_option
from django.core.management.base import BaseCommand


class Command(BaseCommand):

# Used by a specific test to see how unupgraded
# management commands play with configurations.
# See the test code for more details.

option_list = BaseCommand.option_list + (
make_option('--arg1', action='store_true'),
)

def handle(self, *args, **options):
pass
23 changes: 23 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
import subprocess
import sys

from django import VERSION as DJANGO_VERSION
from django.conf import global_settings
from django.test import TestCase
from django.core.exceptions import ImproperlyConfigured

if sys.version_info >= (2, 7):
from unittest import skipIf
else:
from django.utils.unittest import skipIf

from mock import patch

from configurations.importer import ConfigurationImporter
Expand Down Expand Up @@ -108,3 +114,20 @@ def test_configuration_argument_in_cli(self):
proc = subprocess.Popen(['django-cadmin', 'runserver', '--help'],
stdout=subprocess.PIPE)
self.assertIn('--configuration', proc.communicate()[0].decode('utf-8'))

@skipIf(DJANGO_VERSION >= (1, 10), 'only applies to Django < 1.10')
def test_deprecated_option_list_command(self):
"""
Verify that the configuration option is correctly added to any
management commands which are still relying on option_list to
add their own custom arguments
Specific test for a pattern which was deprecated in Django 1.8
and which will become completely unsupported in Django 1.10.
https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/#custom-commands-options
"""
proc = subprocess.Popen(['django-cadmin', 'old_optparse_command', '--help'],
stdout=subprocess.PIPE)
output = proc.communicate()[0].decode('utf-8')
self.assertIn('--configuration', output)
self.assertIn('--arg1', output)

0 comments on commit d827664

Please sign in to comment.