2009-11-18 11:10:50 +00:00
|
|
|
"""
|
|
|
|
A series of tests to establish that the command-line bash completion works.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2009-11-18 11:10:50 +00:00
|
|
|
|
2013-12-24 11:25:17 +00:00
|
|
|
from django.apps import apps
|
2009-11-18 11:10:50 +00:00
|
|
|
from django.core.management import ManagementUtility
|
2012-08-07 13:41:54 +00:00
|
|
|
from django.utils.six import StringIO
|
2009-11-18 11:10:50 +00:00
|
|
|
|
2011-10-13 18:51:33 +00:00
|
|
|
|
2009-11-18 11:10:50 +00: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'
|
2012-08-07 13:41:54 +00:00
|
|
|
self.output = StringIO()
|
2009-11-18 11:10:50 +00:00
|
|
|
self.old_stdout = sys.stdout
|
|
|
|
sys.stdout = self.output
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
sys.stdout = self.old_stdout
|
|
|
|
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):
|
|
|
|
os.environ['COMP_WORDS'] = input_str
|
|
|
|
os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1)
|
|
|
|
sys.argv = input_str.split(' ')
|
|
|
|
|
|
|
|
def _run_autocomplete(self):
|
|
|
|
util = ManagementUtility(argv=sys.argv)
|
|
|
|
try:
|
|
|
|
util.autocomplete()
|
|
|
|
except SystemExit:
|
|
|
|
pass
|
|
|
|
return self.output.getvalue().strip().split('\n')
|
|
|
|
|
|
|
|
def test_django_admin_py(self):
|
|
|
|
"django_admin.py will autocomplete option flags"
|
2013-10-16 14:24:59 +00:00
|
|
|
self._user_input('django-admin.py sqlall --verb')
|
2009-11-18 11:10:50 +00:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ['--verbosity='])
|
|
|
|
|
|
|
|
def test_manage_py(self):
|
|
|
|
"manage.py will autocomplete option flags"
|
2013-10-16 14:24:59 +00:00
|
|
|
self._user_input('manage.py sqlall --verb')
|
2009-11-18 11:10:50 +00:00
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ['--verbosity='])
|
|
|
|
|
|
|
|
def test_custom_command(self):
|
|
|
|
"A custom command can autocomplete option flags"
|
|
|
|
self._user_input('django-admin.py test_command --l')
|
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ['--list'])
|
|
|
|
|
|
|
|
def test_subcommands(self):
|
|
|
|
"Subcommands can be autocompleted"
|
|
|
|
self._user_input('django-admin.py sql')
|
|
|
|
output = self._run_autocomplete()
|
2014-05-12 02:15:59 +00:00
|
|
|
self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset'])
|
2009-11-18 11:10:50 +00:00
|
|
|
|
|
|
|
def test_help(self):
|
|
|
|
"No errors, just an empty list if there are no autocomplete options"
|
|
|
|
self._user_input('django-admin.py help --')
|
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, [''])
|
|
|
|
|
|
|
|
def test_runfcgi(self):
|
|
|
|
"Command arguments will be autocompleted"
|
|
|
|
self._user_input('django-admin.py runfcgi h')
|
|
|
|
output = self._run_autocomplete()
|
|
|
|
self.assertEqual(output, ['host='])
|
|
|
|
|
|
|
|
def test_app_completion(self):
|
|
|
|
"Application names will be autocompleted for an AppCommand"
|
|
|
|
self._user_input('django-admin.py sqlall a')
|
|
|
|
output = self._run_autocomplete()
|
2013-12-19 14:57:23 +00:00
|
|
|
a_labels = sorted(app_config.label
|
2013-12-24 11:25:17 +00:00
|
|
|
for app_config in apps.get_app_configs()
|
2013-12-19 14:57:23 +00:00
|
|
|
if app_config.label.startswith('a'))
|
|
|
|
self.assertEqual(output, a_labels)
|