2009-11-18 12:10:50 +01:00
|
|
|
"""
|
|
|
|
A series of tests to establish that the command-line bash completion works.
|
|
|
|
"""
|
2024-01-26 12:45:07 +01:00
|
|
|
|
2009-11-18 12:10:50 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2013-07-01 14:22:27 +02:00
|
|
|
import unittest
|
2009-11-18 12:10:50 +01:00
|
|
|
|
2013-12-24 12:25:17 +01:00
|
|
|
from django.apps import apps
|
2009-11-18 12:10:50 +01:00
|
|
|
from django.core.management import ManagementUtility
|
2014-11-28 23:47:53 +01:00
|
|
|
from django.test.utils import captured_stdout
|
2009-11-18 12:10:50 +01:00
|
|
|
|
2011-10-13 20:51:33 +02:00
|
|
|
|
2009-11-18 12:10:50 +01:00
|
|
|
class BashCompletionTests(unittest.TestCase):
|
|
|
|
"""
|
|
|
|
Testing the Python level bash completion code.
|
|
|
|
This requires setting up the environment as if we got passed data
|
|
|
|
from bash.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.old_DJANGO_AUTO_COMPLETE = os.environ.get("DJANGO_AUTO_COMPLETE")
|
|
|
|
os.environ["DJANGO_AUTO_COMPLETE"] = "1"
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
if self.old_DJANGO_AUTO_COMPLETE:
|
|
|
|
os.environ["DJANGO_AUTO_COMPLETE"] = self.old_DJANGO_AUTO_COMPLETE
|
|
|
|
else:
|
|
|
|
del os.environ["DJANGO_AUTO_COMPLETE"]
|
|
|
|
|
|
|
|
def _user_input(self, input_str):
|
2014-09-20 16:38:37 +02:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2009-11-18 12:10:50 +01:00
|
|
|
os.environ["COMP_WORDS"] = input_str
|
2014-09-20 16:38:37 +02:00
|
|
|
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()
|
2009-11-18 12:10:50 +01:00
|
|
|
|
|
|
|
def _run_autocomplete(self):
|
|
|
|
util = ManagementUtility(argv=sys.argv)
|
2014-11-28 23:47:53 +01:00
|
|
|
with captured_stdout() as stdout:
|
2017-09-07 14:16:21 +02:00
|
|
|
try:
|
2014-11-28 23:47:53 +01:00
|
|
|
util.autocomplete()
|
2017-09-07 14:16:21 +02:00
|
|
|
except SystemExit:
|
|
|
|
pass
|
2014-11-28 23:47:53 +01:00
|
|
|
return stdout.getvalue().strip().split("\n")
|
2009-11-18 12:10:50 +01:00
|
|
|
|
|
|
|
def test_django_admin_py(self):
|
|
|
|
"django_admin.py will autocomplete option flags"
|
2014-12-27 00:23:00 +01:00
|
|
|
self._user_input("django-admin sqlmigrate --verb")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ["--verbosity="])
|
|
|
|
|
|
|
|
def test_manage_py(self):
|
|
|
|
"manage.py will autocomplete option flags"
|
2014-12-27 00:23:00 +01:00
|
|
|
self._user_input("manage.py sqlmigrate --verb")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ["--verbosity="])
|
|
|
|
|
|
|
|
def test_custom_command(self):
|
|
|
|
"A custom command can autocomplete option flags"
|
2014-07-26 13:21:52 +02:00
|
|
|
self._user_input("django-admin test_command --l")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ["--list"])
|
|
|
|
|
|
|
|
def test_subcommands(self):
|
|
|
|
"Subcommands can be autocompleted"
|
2014-07-26 13:21:52 +02:00
|
|
|
self._user_input("django-admin sql")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
2014-12-26 19:56:08 +01:00
|
|
|
self.assertEqual(output, ["sqlflush sqlmigrate sqlsequencereset"])
|
2009-11-18 12:10:50 +01:00
|
|
|
|
2014-09-20 16:38:37 +02:00
|
|
|
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("--"))
|
|
|
|
|
2009-11-18 12:10:50 +01:00
|
|
|
def test_help(self):
|
|
|
|
"No errors, just an empty list if there are no autocomplete options"
|
2014-07-26 13:21:52 +02:00
|
|
|
self._user_input("django-admin help --")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, [""])
|
|
|
|
|
|
|
|
def test_app_completion(self):
|
|
|
|
"Application names will be autocompleted for an AppCommand"
|
2014-12-27 00:23:00 +01:00
|
|
|
self._user_input("django-admin sqlmigrate a")
|
2009-11-18 12:10:50 +01:00
|
|
|
output = self._run_autocomplete()
|
2016-04-08 04:04:45 +02:00
|
|
|
a_labels = sorted(
|
|
|
|
app_config.label
|
|
|
|
for app_config in apps.get_app_configs()
|
|
|
|
if app_config.label.startswith("a")
|
|
|
|
)
|
2013-12-19 15:57:23 +01:00
|
|
|
self.assertEqual(output, a_labels)
|