| 1 |
""" |
| 2 |
A series of tests to establish that the command-line bash completion works. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
import unittest |
| 8 |
|
| 9 |
from django.apps import apps |
| 10 |
from django.core.management import ManagementUtility |
| 11 |
from django.test.utils import captured_stdout |
| 12 |
|
| 13 |
|
| 14 |
class BashCompletionTests(unittest.TestCase): |
| 15 |
""" |
| 16 |
Testing the Python level bash completion code. |
| 17 |
This requires setting up the environment as if we got passed data |
| 18 |
from bash. |
| 19 |
""" |
| 20 |
|
| 21 |
def setUp(self): |
| 22 |
self.old_DJANGO_AUTO_COMPLETE = os.environ.get("DJANGO_AUTO_COMPLETE") |
| 23 |
os.environ["DJANGO_AUTO_COMPLETE"] = "1" |
| 24 |
|
| 25 |
def tearDown(self): |
| 26 |
if self.old_DJANGO_AUTO_COMPLETE: |
| 27 |
os.environ["DJANGO_AUTO_COMPLETE"] = self.old_DJANGO_AUTO_COMPLETE |
| 28 |
else: |
| 29 |
del os.environ["DJANGO_AUTO_COMPLETE"] |
| 30 |
|
| 31 |
def _user_input(self, input_str): |
| 32 |
""" |
| 33 |
Set the environment and the list of command line arguments. |
| 34 |
|
| 35 |
This sets the bash variables $COMP_WORDS and $COMP_CWORD. The former is |
| 36 |
an array consisting of the individual words in the current command |
| 37 |
line, the latter is the index of the current cursor position, so in |
| 38 |
case a word is completed and the cursor is placed after a whitespace, |
| 39 |
$COMP_CWORD must be incremented by 1: |
| 40 |
|
| 41 |
* 'django-admin start' -> COMP_CWORD=1 |
| 42 |
* 'django-admin startproject' -> COMP_CWORD=1 |
| 43 |
* 'django-admin startproject ' -> COMP_CWORD=2 |
| 44 |
""" |
| 45 |
os.environ["COMP_WORDS"] = input_str |
| 46 |
idx = len(input_str.split(" ")) - 1 # Index of the last word |
| 47 |
comp_cword = idx + 1 if input_str.endswith(" ") else idx |
| 48 |
os.environ["COMP_CWORD"] = str(comp_cword) |
| 49 |
sys.argv = input_str.split() |
| 50 |
|
| 51 |
def _run_autocomplete(self): |
| 52 |
util = ManagementUtility(argv=sys.argv) |
| 53 |
with captured_stdout() as stdout: |
| 54 |
try: |
| 55 |
util.autocomplete() |
| 56 |
except SystemExit: |
| 57 |
pass |
| 58 |
return stdout.getvalue().strip().split("\n") |
| 59 |
|
| 60 |
def test_django_admin_py(self): |
| 61 |
"django_admin.py will autocomplete option flags" |
| 62 |
self._user_input("django-admin sqlmigrate --verb") |
| 63 |
output = self._run_autocomplete() |
| 64 |
self.assertEqual(output, ["--verbosity="]) |
| 65 |
|
| 66 |
def test_manage_py(self): |
| 67 |
"manage.py will autocomplete option flags" |
| 68 |
self._user_input("manage.py sqlmigrate --verb") |
| 69 |
output = self._run_autocomplete() |
| 70 |
self.assertEqual(output, ["--verbosity="]) |
| 71 |
|
| 72 |
def test_custom_command(self): |
| 73 |
"A custom command can autocomplete option flags" |
| 74 |
self._user_input("django-admin test_command --l") |
| 75 |
output = self._run_autocomplete() |
| 76 |
self.assertEqual(output, ["--list"]) |
| 77 |
|
| 78 |
def test_subcommands(self): |
| 79 |
"Subcommands can be autocompleted" |
| 80 |
self._user_input("django-admin sql") |
| 81 |
output = self._run_autocomplete() |
| 82 |
self.assertEqual(output, ["sqlflush sqlmigrate sqlsequencereset"]) |
| 83 |
|
| 84 |
def test_completed_subcommand(self): |
| 85 |
"Show option flags in case a subcommand is completed" |
| 86 |
self._user_input("django-admin startproject ") # Trailing whitespace |
| 87 |
output = self._run_autocomplete() |
| 88 |
for item in output: |
| 89 |
self.assertTrue(item.startswith("--")) |
| 90 |
|
| 91 |
def test_help(self): |
| 92 |
"No errors, just an empty list if there are no autocomplete options" |
| 93 |
self._user_input("django-admin help --") |
| 94 |
output = self._run_autocomplete() |
| 95 |
self.assertEqual(output, [""]) |
| 96 |
|
| 97 |
def test_app_completion(self): |
| 98 |
"Application names will be autocompleted for an AppCommand" |
| 99 |
self._user_input("django-admin sqlmigrate a") |
| 100 |
output = self._run_autocomplete() |
| 101 |
a_labels = sorted( |
| 102 |
app_config.label |
| 103 |
for app_config in apps.get_app_configs() |
| 104 |
if app_config.label.startswith("a") |
| 105 |
) |
| 106 |
self.assertEqual(output, a_labels) |