Ticket #23551: test_subcommand_completed.patch

File test_subcommand_completed.patch, 2.1 KB (added by Marco Buttu, 10 years ago)

It contains the test and also a patch for the test itself

  • .py

    old new  
    3232            del os.environ['DJANGO_AUTO_COMPLETE']
    3333
    3434    def _user_input(self, input_str):
     35        """Set the environment and the list of command line arguments.
     36
     37        This method sets the bash variables $COMP_WORDS and $COMP_CWORD.
     38        The former is an array consisting of the individual words in the
     39        current command line, the latter is the index of the current
     40        cursor position, so in case a word is completed and the cursor is
     41        placed after a whitespace, $COMP_CWORD must be incremented by 1:
     42
     43          * 'django-admin start' -> COMP_CWORD=1
     44          * 'django-admin startproject' -> COMP_CWORD=1
     45          * 'django-admin startproject ' -> COMP_CWORD=2
     46        """
    3547        os.environ['COMP_WORDS'] = input_str
    36         os.environ['COMP_CWORD'] = str(len(input_str.split()) - 1)
    37         sys.argv = input_str.split(' ')
     48        idx = len(input_str.split(' ')) - 1 # Index of the last word
     49        comp_cword = idx + 1 if input_str.endswith(' ') else idx
     50        os.environ['COMP_CWORD'] = str(comp_cword)
     51        sys.argv = input_str.split()
    3852
    3953    def _run_autocomplete(self):
    4054        util = ManagementUtility(argv=sys.argv)
     
    6882        output = self._run_autocomplete()
    6983        self.assertEqual(output, ['sql sqlall sqlclear sqlcustom sqldropindexes sqlflush sqlindexes sqlmigrate sqlsequencereset'])
    7084
     85    def test_completed_subcommand(self):
     86        """Show option flags in case a subcommand is completed"""
     87        self._user_input('django-admin startproject ') # Trailing whitespace
     88        output = self._run_autocomplete()
     89        for item in output:
     90            self.assertTrue(item.startswith('--'))
     91
    7192    def test_help(self):
    7293        "No errors, just an empty list if there are no autocomplete options"
    7394        self._user_input('django-admin help --')
     
    88109            for app_config in apps.get_app_configs()
    89110            if app_config.label.startswith('a'))
    90111        self.assertEqual(output, a_labels)
     112
Back to Top