Commit e077c291 authored by Marco Buttu's avatar Marco Buttu Committed by Tim Graham
Browse files

Fixed #23551 -- Fixed bash autocompletion crash on Python 3.

parent 9f576dd5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -222,7 +222,7 @@ class ManagementUtility(object):
            curr = ''

        subcommands = list(get_commands()) + ['help']
        options = [('--help', None)]
        options = [('--help', False)]

        # subcommand
        if cword == 1:
+2 −0
Original line number Diff line number Diff line
@@ -70,3 +70,5 @@ Bugfixes

* Made the :setting:`SERIALIZE <TEST_SERIALIZE>` entry in the
  :setting:`TEST <DATABASE-TEST>` dictionary usable (:ticket:`23421`).

* Fixed a crash in bash autocompletion on Python 3 (:ticket:`23551`).
+24 −2
Original line number Diff line number Diff line
@@ -32,9 +32,24 @@ class BashCompletionTests(unittest.TestCase):
            del os.environ['DJANGO_AUTO_COMPLETE']

    def _user_input(self, input_str):
        """
        Set the environment and the list of command line arguments.

        This sets the bash variables $COMP_WORDS and $COMP_CWORD. The former is
        an array consisting of the individual words in the current command
        line, the latter is the index of the current cursor position, so in
        case a word is completed and the cursor is placed after a whitespace,
        $COMP_CWORD must be incremented by 1:

          * 'django-admin start' -> COMP_CWORD=1
          * 'django-admin startproject' -> COMP_CWORD=1
          * 'django-admin startproject ' -> COMP_CWORD=2
        """
        os.environ['COMP_WORDS'] = input_str
        os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1)
        sys.argv = input_str.split(' ')
        idx = len(input_str.split(' ')) - 1  # Index of the last word
        comp_cword = idx + 1 if input_str.endswith(' ') else idx
        os.environ['COMP_CWORD'] = str(comp_cword)
        sys.argv = input_str.split()

    def _run_autocomplete(self):
        util = ManagementUtility(argv=sys.argv)
@@ -68,6 +83,13 @@ class BashCompletionTests(unittest.TestCase):
        output = self._run_autocomplete()
        self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset'])

    def test_completed_subcommand(self):
        "Show option flags in case a subcommand is completed"
        self._user_input('django-admin startproject ')  # Trailing whitespace
        output = self._run_autocomplete()
        for item in output:
            self.assertTrue(item.startswith('--'))

    def test_help(self):
        "No errors, just an empty list if there are no autocomplete options"
        self._user_input('django-admin help --')