| 1 | """ |
| 2 | A series of tests to establish that the command-line bash completion works. |
| 3 | """ |
| 4 | import os |
| 5 | import unittest |
| 6 | import sys |
| 7 | import StringIO |
| 8 | |
| 9 | from django.core.management import ManagementUtility |
| 10 | |
| 11 | class BashCompletionTests(unittest.TestCase): |
| 12 | """ |
| 13 | Testing the Python level bash completion code. |
| 14 | This requires settings up the environment like if we got passed data |
| 15 | from bash. |
| 16 | """ |
| 17 | |
| 18 | def setUp(self): |
| 19 | os.environ['DJANGO_AUTO_COMPLETE'] = '1' |
| 20 | self.output = StringIO.StringIO() |
| 21 | self.old_stdout = sys.stdout |
| 22 | sys.stdout = self.output |
| 23 | |
| 24 | def tearDown(self): |
| 25 | sys.stdout = self.old_stdout |
| 26 | |
| 27 | def _user_input(self, input_str): |
| 28 | os.environ['COMP_WORDS'] = input_str |
| 29 | os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1) |
| 30 | sys.argv = input_str.split(' ') |
| 31 | |
| 32 | def _run_autocomplete(self): |
| 33 | util = ManagementUtility(argv=sys.argv) |
| 34 | try: |
| 35 | util.autocomplete() |
| 36 | except SystemExit: |
| 37 | pass |
| 38 | return self.output.getvalue().strip().split('\n') |
| 39 | |
| 40 | def test_django_admin_py(self): |
| 41 | self._user_input('django-admin.py sqlall --v') |
| 42 | output = self._run_autocomplete() |
| 43 | self.assertEqual(output, ['--verbosity=']) |
| 44 | |
| 45 | def test_manage_py(self): |
| 46 | self._user_input('manage.py sqlall --v') |
| 47 | output = self._run_autocomplete() |
| 48 | self.assertEqual(output, ['--verbosity=']) |
| 49 | |
| 50 | def test_custom_command(self): |
| 51 | self._user_input('django-admin.py test_command --l') |
| 52 | output = self._run_autocomplete() |
| 53 | self.assertEqual(output, ['--list']) |
| 54 | |
| 55 | def test_subcommands(self): |
| 56 | self._user_input('django-admin.py sql') |
| 57 | output = self._run_autocomplete() |
| 58 | self.assertEqual(output, ['sqlinitialdata sqlclear sqlreset sqlsequencereset sql sqlall sqlflush sqlcustom sqlindexes']) |
| 59 | |
| 60 | def test_help(self): |
| 61 | #Help should return nothing since it takes no args. |
| 62 | self._user_input('django-admin.py help --') |
| 63 | output = self._run_autocomplete() |
| 64 | self.assertEqual(output, ['']) |
| 65 | |
| 66 | def test_runfcgi(self): |
| 67 | #Help should return nothing since it takes no args. |
| 68 | self._user_input('django-admin.py runfcgi h') |
| 69 | output = self._run_autocomplete() |
| 70 | self.assertEqual(output, ['host=']) |
| 71 | |
| 72 | def test_app_completion(self): |
| 73 | #Help should return nothing since it takes no args. |
| 74 | self._user_input('django-admin.py sqlall a') |
| 75 | output = self._run_autocomplete() |
| 76 | self.assertEqual(output, ['auth', 'admin']) |
| 77 | No newline at end of file |