Ticket #12221: full_completion.diff

File full_completion.diff, 6.8 KB (added by ericholscher, 6 years ago)

All of the work I've done thus far for this ticket.

  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 60dcf72..5432ef4 100644
    a b class ManagementUtility(object): 
    327327            prev_opts = [x.split('=')[0] for x in cwords[1:cword-1]]
    328328            options = filter(lambda (x, v): x not in prev_opts, options)
    329329
     330            if hasattr(subcommand_cls, 'complete'):
     331                complete = subcommand_cls.complete()
     332                if isinstance(complete, list):
     333                    options += [(k, 0) for k in complete]
     334                elif isinstance(complete, dict):
     335                    to_add = complete[str(cword-2)]
     336                    if to_add == 'COMMAND_NAME':
     337                        print ' '.join(filter(lambda x: x.startswith(curr), subcommands))
     338                    elif to_add == 'APP_NAME':
     339                        from django.conf import settings
     340                        options += [(a.split('.')[-1], 0) for a in settings.INSTALLED_APPS]
     341                    else:
     342                        options += to_add
     343
     344
    330345            # filter options by current input
    331346            options = [(k, v) for k, v in options if k.startswith(curr)]
    332347            for option in options:
  • new file tests/regressiontests/bash_completion/management/commands/dict_command.py

    diff --git a/tests/regressiontests/bash_completion/__init__.py b/tests/regressiontests/bash_completion/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bash_completion/management/__init__.py b/tests/regressiontests/bash_completion/management/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bash_completion/management/commands/__init__.py b/tests/regressiontests/bash_completion/management/commands/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bash_completion/management/commands/dict_command.py b/tests/regressiontests/bash_completion/management/commands/dict_command.py
    new file mode 100644
    index 0000000..d7f0979
    - +  
     1import sys, os
     2from optparse import OptionParser, make_option
     3
     4from django.core.management.base import BaseCommand
     5
     6class Command(BaseCommand):
     7    option_list = BaseCommand.option_list + (
     8        make_option("--list", action="store_true", dest="list",
     9                    help="Print all options"),
     10    )
     11
     12    def complete(self):
     13        return {
     14            '0': [('awesome', None), ('sweet', None)],
     15            '1': 'COMMAND_NAME',
     16            '2': 'APP_NAME',
     17        }
     18
     19    def handle(self, *args, **options):
     20        pass
  • new file tests/regressiontests/bash_completion/management/commands/test_command.py

    diff --git a/tests/regressiontests/bash_completion/management/commands/test_command.py b/tests/regressiontests/bash_completion/management/commands/test_command.py
    new file mode 100644
    index 0000000..2b9ec0f
    - +  
     1import sys, os
     2from optparse import OptionParser, make_option
     3
     4from django.core.management.base import BaseCommand
     5
     6class Command(BaseCommand):
     7    option_list = BaseCommand.option_list + (
     8        make_option("--list", action="store_true", dest="list",
     9                    help="Print all options"),
     10    )
     11
     12    def complete(self):
     13        return ['awesome', 'sweet']
     14
     15    def handle(self, *args, **options):
     16        pass
  • new file tests/regressiontests/bash_completion/tests.py

    diff --git a/tests/regressiontests/bash_completion/models.py b/tests/regressiontests/bash_completion/models.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/regressiontests/bash_completion/tests.py b/tests/regressiontests/bash_completion/tests.py
    new file mode 100644
    index 0000000..e751bee
    - +  
     1"""
     2A series of tests to establish that the command-line bash completion works.
     3"""
     4import os
     5import unittest
     6import sys
     7import StringIO
     8
     9from django.core.management import ManagementUtility
     10
     11class BashCompletionTests(unittest.TestCase):
     12    """
     13    Testing the Python level bash completion code.
     14    This requires settings up the environment like if we got passed data
     15    from bash.
     16    """
     17
     18    def setUp(self):
     19        os.environ['DJANGO_AUTO_COMPLETE'] = '1'
     20        self.output = StringIO.StringIO()
     21        self.old_stdout = sys.stdout
     22        sys.stdout = self.output
     23
     24    def tearDown(self):
     25        sys.stdout = self.old_stdout
     26
     27    def _user_input(self, input_str):
     28        os.environ['COMP_WORDS'] = input_str
     29        os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1)
     30        sys.argv = input_str.split(' ')
     31
     32    def _run_autocomplete(self):
     33        util = ManagementUtility(argv=sys.argv)
     34        try:
     35            util.autocomplete()
     36        except SystemExit:
     37            pass
     38        return self.output.getvalue().strip().split('\n')
     39
     40    def test_django_admin_py(self):
     41        self._user_input('django-admin.py sqlall --v')
     42        output = self._run_autocomplete()
     43        self.assertEqual(output, ['--verbosity='])
     44
     45    def test_manage_py(self):
     46        self._user_input('manage.py sqlall --v')
     47        output = self._run_autocomplete()
     48        self.assertEqual(output, ['--verbosity='])
     49
     50    def test_custom_command(self):
     51        self._user_input('django-admin.py test_command --l')
     52        output = self._run_autocomplete()
     53        self.assertEqual(output, ['--list'])
     54
     55    def test_subcommands(self):
     56        self._user_input('django-admin.py sql')
     57        output = self._run_autocomplete()
     58        self.assertEqual(output, ['sqlinitialdata sqlclear sqlreset sqlsequencereset sql sqlall sqlflush sqlcustom sqlindexes'])
     59
     60    def test_help(self):
     61        #Help should return nothing since it takes no args.
     62        self._user_input('django-admin.py help --')
     63        output = self._run_autocomplete()
     64        self.assertEqual(output, [''])
     65
     66    def test_runfcgi(self):
     67        #runfcgi has its own special options
     68        self._user_input('django-admin.py runfcgi h')
     69        output = self._run_autocomplete()
     70        self.assertEqual(output, ['host='])
     71
     72    def test_app_completion(self):
     73        self._user_input('django-admin.py sqlall a')
     74        output = self._run_autocomplete()
     75        self.assertEqual(output, ['auth', 'admin'])
     76
     77    def test_dict_return_list(self):
     78        self._user_input('django-admin.py dict_command a')
     79        output = self._run_autocomplete()
     80        self.assertEqual(output, ['awesome'])
     81
     82    def test_dict_return_commands(self):
     83        self._user_input('django-admin.py dict_command awesome sqlal')
     84        output = self._run_autocomplete()
     85        self.assertEqual(output, ['sqlall'])
     86
     87    def test_dict_return_apps(self):
     88        self._user_input('django-admin.py dict_command awesome sqlall a')
     89        output = self._run_autocomplete()
     90        self.assertEqual(output, ['auth', 'admin'])
Back to Top