summaryrefslogtreecommitdiff
path: root/tests/bash_completion
diff options
context:
space:
mode:
authorMarco Buttu <marco.buttu@gmail.com>2014-09-20 16:38:37 +0200
committerTim Graham <timograham@gmail.com>2014-09-25 07:44:22 -0400
commite077c29155e5ecb1ff24eb61d30c99505797e84d (patch)
treebbfdae9dedb5b9f4998b89a226d272bae7c97de4 /tests/bash_completion
parent9f576dd54fafdd76aa250f8947cc76c7ae19216c (diff)
Fixed #23551 -- Fixed bash autocompletion crash on Python 3.
Diffstat (limited to 'tests/bash_completion')
-rw-r--r--tests/bash_completion/tests.py26
1 files changed, 24 insertions, 2 deletions
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 --')