diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py index b25477fc82..0502489d4e 100644 --- a/django/core/management/__init__.py +++ b/django/core/management/__init__.py @@ -222,7 +222,7 @@ class ManagementUtility(object): curr = '' subcommands = list(get_commands()) + ['help'] - options = [('--help', None)] + options = [('--help', False)] # subcommand if cword == 1: diff --git a/docs/releases/1.7.1.txt b/docs/releases/1.7.1.txt index cd534f1346..e16b7811e5 100644 --- a/docs/releases/1.7.1.txt +++ b/docs/releases/1.7.1.txt @@ -70,3 +70,5 @@ Bugfixes * Made the :setting:`SERIALIZE ` entry in the :setting:`TEST ` dictionary usable (:ticket:`23421`). + +* Fixed a crash in bash autocompletion on Python 3 (:ticket:`23551`). diff --git a/tests/bash_completion/tests.py b/tests/bash_completion/tests.py index 329ca8c70c..25a62148c0 100644 --- a/tests/bash_completion/tests.py +++ b/tests/bash_completion/tests.py @@ -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 --')