PageRenderTime 113ms CodeModel.GetById 91ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/tests/regressiontests/bash_completion/tests.py

https://code.google.com/p/mango-py/
Python | 87 lines | 68 code | 11 blank | 8 comment | 1 complexity | 39039ce2e4881fae51947eb8547fc20d MD5 | raw file
 1"""
 2A series of tests to establish that the command-line bash completion works.
 3"""
 4import os
 5import sys
 6import StringIO
 7
 8from django.conf import settings
 9from django.core.management import ManagementUtility
10from django.utils import unittest
11
12class BashCompletionTests(unittest.TestCase):
13    """
14    Testing the Python level bash completion code.
15    This requires setting up the environment as if we got passed data
16    from bash.
17    """
18
19    def setUp(self):
20        self.old_DJANGO_AUTO_COMPLETE = os.environ.get('DJANGO_AUTO_COMPLETE')
21        os.environ['DJANGO_AUTO_COMPLETE'] = '1'
22        self.output = StringIO.StringIO()
23        self.old_stdout = sys.stdout
24        sys.stdout = self.output
25
26    def tearDown(self):
27        sys.stdout = self.old_stdout
28        if self.old_DJANGO_AUTO_COMPLETE:
29            os.environ['DJANGO_AUTO_COMPLETE'] = self.old_DJANGO_AUTO_COMPLETE
30        else:
31            del os.environ['DJANGO_AUTO_COMPLETE']
32
33    def _user_input(self, input_str):
34        os.environ['COMP_WORDS'] = input_str
35        os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1)
36        sys.argv = input_str.split(' ')
37
38    def _run_autocomplete(self):
39        util = ManagementUtility(argv=sys.argv)
40        try:
41            util.autocomplete()
42        except SystemExit:
43            pass
44        return self.output.getvalue().strip().split('\n')
45
46    def test_django_admin_py(self):
47        "django_admin.py will autocomplete option flags"
48        self._user_input('django-admin.py sqlall --v')
49        output = self._run_autocomplete()
50        self.assertEqual(output, ['--verbosity='])
51
52    def test_manage_py(self):
53        "manage.py will autocomplete option flags"
54        self._user_input('manage.py sqlall --v')
55        output = self._run_autocomplete()
56        self.assertEqual(output, ['--verbosity='])
57
58    def test_custom_command(self):
59        "A custom command can autocomplete option flags"
60        self._user_input('django-admin.py test_command --l')
61        output = self._run_autocomplete()
62        self.assertEqual(output, ['--list'])
63
64    def test_subcommands(self):
65        "Subcommands can be autocompleted"
66        self._user_input('django-admin.py sql')
67        output = self._run_autocomplete()
68        self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqlflush sqlindexes sqlinitialdata sqlreset sqlsequencereset'])
69
70    def test_help(self):
71        "No errors, just an empty list if there are no autocomplete options"
72        self._user_input('django-admin.py help --')
73        output = self._run_autocomplete()
74        self.assertEqual(output, [''])
75
76    def test_runfcgi(self):
77        "Command arguments will be autocompleted"
78        self._user_input('django-admin.py runfcgi h')
79        output = self._run_autocomplete()
80        self.assertEqual(output, ['host='])
81
82    def test_app_completion(self):
83        "Application names will be autocompleted for an AppCommand"
84        self._user_input('django-admin.py sqlall a')
85        output = self._run_autocomplete()
86        app_labels = [name.split('.')[-1] for name in settings.INSTALLED_APPS]
87        self.assertEqual(output, sorted(label for label in app_labels if label.startswith('a')))