summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkmvanbrunt <kmvanbrunt@gmail.com>2018-04-15 02:11:51 -0400
committerGitHub <noreply@github.com>2018-04-15 02:11:51 -0400
commit13aac11fb54c74d00360a32df624edc3e7be82d2 (patch)
treefe92db7485afd7e7c56964412577853c1655dc0a
parent9d9e843845b57419e4be1abc65119b1d04dfa0f0 (diff)
parentef99976350b423315cd5a49a16b4000551302d09 (diff)
downloadcmd2-git-13aac11fb54c74d00360a32df624edc3e7be82d2.tar.gz
Merge pull request #353 from python-cmd2/unhackify
Unhackify
-rw-r--r--.gitignore14
-rw-r--r--CHANGELOG.md20
-rwxr-xr-xcmd2.py258
-rw-r--r--docs/conf.py2
-rwxr-xr-xexamples/tab_completion.py4
-rwxr-xr-xsetup.py2
-rw-r--r--tests/test_cmd2.py2
-rw-r--r--tests/test_completion.py168
8 files changed, 272 insertions, 198 deletions
diff --git a/.gitignore b/.gitignore
index bb9e88c6..ad7f428b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,11 +1,19 @@
+# Python development, test, and build
__pycache__
build
dist
cmd2.egg-info
-.idea
.cache
*.pyc
-.coverage
.tox
-htmlcov
.pytest_cache
+
+# Code Coverage
+.coverage
+htmlcov
+
+# PyCharm
+.idea
+
+# Visual Studio Code
+.vscode
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 253ceaa2..8a480123 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,23 @@
+## 0.8.5 (April 15, 2018)
+* Bug Fixes
+ * Fixed a bug with all argument decorators where the wrapped function wasn't returning a value and thus couldn't cause the cmd2 app to quit
+
+* Enhancements
+ * Added support for verbose help with -v where it lists a brief summary of what each command does
+ * Added support for categorizing commands into groups within the help menu
+ * See the [Grouping Commands](http://cmd2.readthedocs.io/en/latest/argument_processing.html?highlight=verbose#grouping-commands) section of the docs for more info
+ * See [help_categories.py](https://github.com/python-cmd2/cmd2/blob/master/examples/help_categories.py) for an example
+ * Tab completion of paths now supports ~user user path expansion
+ * Simplified implementation of various tab completion functions so they no longer require ``ctypes``
+ * Expanded documentation of ``display_matches`` list to clarify its purpose. See cmd2.py for this documentation.
+ * Adding opening quote to tab completion if any of the matches have a space.
+
+* **Python 2 EOL notice**
+ * This is the last release where new features will be added to ``cmd2`` for Python 2.7
+ * The 0.9.0 release of ``cmd2`` will support Python 3.4+ only
+ * Additional 0.8.x releases may be created to supply bug fixes for Python 2.7 up until August 31, 2018
+ * After August 31, 2018 not even bug fixes will be provided for Python 2.7
+
## 0.8.4 (April 10, 2018)
* Bug Fixes
* Fixed conditional dependency issue in setup.py that was in 0.8.3.
diff --git a/cmd2.py b/cmd2.py
index 167e71c7..1689d37c 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -188,7 +188,7 @@ if six.PY2 and sys.platform.startswith('lin'):
pass
-__version__ = '0.8.4'
+__version__ = '0.8.5'
# Pyparsing enablePackrat() can greatly speed up parsing, but problems have been seen in Python 3 in the past
pyparsing.ParserElement.enablePackrat()
@@ -1107,8 +1107,11 @@ class Cmd(cmd.Cmd):
# will be added if there is an unmatched opening quote
self.allow_closing_quote = True
- # If the tab-completion matches should be displayed in a way that is different than the actual match values,
- # then place those results in this list. path_complete uses this to show only the basename of completions.
+ # Use this list if you are completing strings that contain a common delimiter and you only want to
+ # display the final portion of the matches as the tab-completion suggestions. The full matches
+ # still must be returned from your completer function. For an example, look at path_complete()
+ # which uses this to show only the basename of paths as the suggestions. delimiter_complete() also
+ # populates this list.
self.display_matches = []
# ----- Methods related to presenting output to the user -----
@@ -1321,7 +1324,7 @@ class Cmd(cmd.Cmd):
On Success
tokens: list of unquoted tokens
this is generally the list needed for tab completion functions
- raw_tokens: list of tokens as they appear on the command line, meaning their quotes are preserved
+ raw_tokens: list of tokens with any quotes preserved
this can be used to know if a token was quoted or is missing a closing quote
Both lists are guaranteed to have at least 1 item
@@ -1349,7 +1352,7 @@ class Cmd(cmd.Cmd):
break
except ValueError:
# ValueError can be caused by missing closing quote
- if len(quotes_to_try) == 0:
+ if not quotes_to_try:
# Since we have no more quotes to try, something else
# is causing the parsing error. Return None since
# this means the line is malformed.
@@ -1481,7 +1484,7 @@ class Cmd(cmd.Cmd):
matches = self.basic_complete(text, line, begidx, endidx, match_against)
# Display only the portion of the match that's being completed based on delimiter
- if len(matches) > 0:
+ if matches:
# Get the common beginning for the matches
common_prefix = os.path.commonprefix(matches)
@@ -1489,7 +1492,7 @@ class Cmd(cmd.Cmd):
# Calculate what portion of the match we are completing
display_token_index = 0
- if len(prefix_tokens) > 0:
+ if prefix_tokens:
display_token_index = len(prefix_tokens) - 1
# Get this portion for each match and store them in self.display_matches
@@ -1497,7 +1500,7 @@ class Cmd(cmd.Cmd):
match_tokens = cur_match.split(delimiter)
display_token = match_tokens[display_token_index]
- if len(display_token) == 0:
+ if not display_token:
display_token = delimiter
self.display_matches.append(display_token)
@@ -1759,7 +1762,7 @@ class Cmd(cmd.Cmd):
:return: List[str] - a list of possible tab completions
"""
# Don't tab complete anything if no shell command has been started
- if not complete_blank and len(text) == 0:
+ if not complete_blank and not text:
return []
# If there are no path characters in the search text, then do shell command completion in the user's path
@@ -1862,7 +1865,7 @@ class Cmd(cmd.Cmd):
if rl_type == RlType.GNU:
# Check if we should show display_matches
- if len(self.display_matches) > 0:
+ if self.display_matches:
matches_to_display = self.display_matches
# Recalculate longest_match_length for display_matches
@@ -1920,7 +1923,7 @@ class Cmd(cmd.Cmd):
if rl_type == RlType.PYREADLINE:
# Check if we should show display_matches
- if len(self.display_matches) > 0:
+ if self.display_matches:
matches_to_display = self.display_matches
else:
matches_to_display = matches
@@ -1931,117 +1934,6 @@ class Cmd(cmd.Cmd):
# Display the matches
orig_pyreadline_display(matches_to_display)
- def _handle_completion_token_quote(self, raw_completion_token):
- """
- This is called by complete() to add an opening quote to the token being completed if it is needed
- The readline input buffer is then updated with the new string
- :param raw_completion_token: str - the token being completed as it appears on the command line
- :return: True if a quote was added, False otherwise
- """
- if len(self.completion_matches) == 0:
- return False
-
- quote_added = False
-
- # Check if token on screen is already quoted
- if len(raw_completion_token) == 0 or raw_completion_token[0] not in QUOTES:
-
- # Get the common prefix of all matches. This is what be written to the screen.
- common_prefix = os.path.commonprefix(self.completion_matches)
-
- # If common_prefix contains a space, then we must add an opening quote to it
- if ' ' in common_prefix:
-
- # Figure out what kind of quote to add
- if '"' in common_prefix:
- quote = "'"
- else:
- quote = '"'
-
- new_completion_token = quote + common_prefix
-
- # Handle a single result
- if len(self.completion_matches) == 1:
- str_to_append = ''
-
- # Add a closing quote if allowed
- if self.allow_closing_quote:
- str_to_append += quote
-
- orig_line = readline.get_line_buffer()
- endidx = readline.get_endidx()
-
- # If we are at the end of the line, then add a space if allowed
- if self.allow_appended_space and endidx == len(orig_line):
- str_to_append += ' '
-
- new_completion_token += str_to_append
-
- # Update the line
- quote_added = True
- self._replace_completion_token(raw_completion_token, new_completion_token)
-
- return quote_added
-
- def _replace_completion_token(self, raw_completion_token, new_completion_token):
- """
- Replaces the token being completed in the readline line buffer which updates the screen
- This is used for things like adding an opening quote for completions with spaces
- :param raw_completion_token: str - the original token being completed as it appears on the command line
- :param new_completion_token: str- the replacement token
- :return: None
- """
- orig_line = readline.get_line_buffer()
- endidx = readline.get_endidx()
-
- starting_index = orig_line[:endidx].rfind(raw_completion_token)
-
- if starting_index != -1:
- # Build the new line
- new_line = orig_line[:starting_index]
- new_line += new_completion_token
- new_line += orig_line[endidx:]
-
- # Calculate the new cursor offset
- len_diff = len(new_completion_token) - len(raw_completion_token)
- new_point = endidx + len_diff
-
- # Replace the line and update the cursor offset
- self._set_readline_line(new_line)
- self._set_readline_point(new_point)
-
- @staticmethod
- def _set_readline_line(new_line):
- """
- Sets the readline line buffer
- :param new_line: str - the new line contents
- """
- if rl_type == RlType.GNU:
- # Byte encode the new line
- if six.PY3:
- encoded_line = bytes(new_line, encoding='utf-8')
- else:
- encoded_line = bytes(new_line)
-
- # Replace the line
- readline_lib.rl_replace_line(encoded_line, 0)
-
- elif rl_type == RlType.PYREADLINE:
- readline.rl.mode.l_buffer.set_line(new_line)
-
- @staticmethod
- def _set_readline_point(new_point):
- """
- Sets the cursor offset in the readline line buffer
- :param new_point: int - the new cursor offset
- """
- if rl_type == RlType.GNU:
- rl_point = ctypes.c_int.in_dll(readline_lib, "rl_point")
- rl_point.value = new_point
-
- elif rl_type == RlType.PYREADLINE:
- readline.rl.mode.l_buffer.point = new_point
-
# ----- Methods which override stuff in cmd -----
def complete(self, text, state):
@@ -2072,10 +1964,9 @@ class Cmd(cmd.Cmd):
begidx = max(readline.get_begidx() - stripped, 0)
endidx = max(readline.get_endidx() - stripped, 0)
- # We only break words on whitespace and quotes when tab completing.
- # Therefore shortcuts become part of the text variable if there isn't a space after it.
- # We need to remove it from text and update the indexes. This only applies if we are at
- # the beginning of the line.
+ # Shortcuts are not word break characters when tab completing. Therefore shortcuts become part
+ # of the text variable if there isn't a word break, like a space, after it. We need to remove it
+ # from text and update the indexes. This only applies if we are at the the beginning of the line.
shortcut_to_restore = ''
if begidx == 0:
for (shortcut, expansion) in self.shortcuts:
@@ -2119,21 +2010,32 @@ class Cmd(cmd.Cmd):
self.completion_matches = []
return None
- # readline still performs word breaks after a quote. Therefore something like quoted search
- # text with a space would have resulted in begidx pointing to the middle of the token we
- # we want to complete. Figure out where that token actually begins and save the beginning
- # portion of it that was not part of the text readline gave us. We will remove it from the
- # completions later since readline expects them to start with the original text.
- actual_begidx = line[:endidx].rfind(tokens[-1])
+ # Text we need to remove from completions later
text_to_remove = ''
- if actual_begidx != begidx:
- text_to_remove = line[actual_begidx:begidx]
+ # Get the token being completed with any opening quote preserved
+ raw_completion_token = raw_tokens[-1]
+
+ # Check if the token being completed has an opening quote
+ if raw_completion_token and raw_completion_token[0] in QUOTES:
+
+ # Since the token is still being completed, we know the opening quote is unclosed
+ unclosed_quote = raw_completion_token[0]
- # Adjust text and where it begins so the completer routines
- # get unbroken search text to complete on.
- text = text_to_remove + text
- begidx = actual_begidx
+ # readline still performs word breaks after a quote. Therefore something like quoted search
+ # text with a space would have resulted in begidx pointing to the middle of the token we
+ # we want to complete. Figure out where that token actually begins and save the beginning
+ # portion of it that was not part of the text readline gave us. We will remove it from the
+ # completions later since readline expects them to start with the original text.
+ actual_begidx = line[:endidx].rfind(tokens[-1])
+
+ if actual_begidx != begidx:
+ text_to_remove = line[actual_begidx:begidx]
+
+ # Adjust text and where it begins so the completer routines
+ # get unbroken search text to complete on.
+ text = text_to_remove + text
+ begidx = actual_begidx
# Check if a valid command was entered
if command in self.get_all_commands():
@@ -2164,7 +2066,7 @@ class Cmd(cmd.Cmd):
# call the completer function for the current command
self.completion_matches = self._redirect_complete(text, line, begidx, endidx, compfunc)
- if len(self.completion_matches) > 0:
+ if self.completion_matches:
# Eliminate duplicates
matches_set = set(self.completion_matches)
@@ -2173,36 +2075,58 @@ class Cmd(cmd.Cmd):
display_matches_set = set(self.display_matches)
self.display_matches = list(display_matches_set)
- # Get the token being completed as it appears on the command line
- raw_completion_token = raw_tokens[-1]
-
- # Add an opening quote if needed
- if self._handle_completion_token_quote(raw_completion_token):
- # An opening quote was added and the screen was updated. Return no results.
- self.completion_matches = []
- return None
+ # Check if display_matches has been used. If so, then matches
+ # on delimited strings like paths was done.
+ if self.display_matches:
+ matches_delimited = True
+ else:
+ matches_delimited = False
- if text_to_remove or shortcut_to_restore:
- # If self.display_matches is empty, then set it to self.completion_matches
+ # Since self.display_matches is empty, set it to self.completion_matches
# before we alter them. That way the suggestions will reflect how we parsed
# the token being completed and not how readline did.
- if len(self.display_matches) == 0:
- self.display_matches = copy.copy(self.completion_matches)
+ self.display_matches = copy.copy(self.completion_matches)
+
+ # Check if we need to add an opening quote
+ if not unclosed_quote:
+
+ add_quote = False
+
+ # This is the tab completion text that will appear on the command line.
+ common_prefix = os.path.commonprefix(self.completion_matches)
- # Check if we need to remove text from the beginning of tab completions
- if text_to_remove:
- self.completion_matches = \
- [m.replace(text_to_remove, '', 1) for m in self.completion_matches]
+ if matches_delimited:
+ # Check if any portion of the display matches appears in the tab completion
+ display_prefix = os.path.commonprefix(self.display_matches)
- # Check if we need to restore a shortcut in the tab completions
- # so it doesn't get erased from the command line
- if shortcut_to_restore:
- self.completion_matches = \
- [shortcut_to_restore + match for match in self.completion_matches]
+ # For delimited matches, we check what appears before the display
+ # matches (common_prefix) as well as the display matches themselves.
+ if (' ' in common_prefix) or (display_prefix and ' ' in ''.join(self.display_matches)):
+ add_quote = True
- # If the token being completed starts with a quote then we know it has an unclosed quote
- if len(raw_completion_token) > 0 and raw_completion_token[0] in QUOTES:
- unclosed_quote = raw_completion_token[0]
+ # If there is a tab completion and any match has a space, then add an opening quote
+ elif common_prefix and ' ' in ''.join(self.completion_matches):
+ add_quote = True
+
+ if add_quote:
+ # Figure out what kind of quote to add and save it as the unclosed_quote
+ if '"' in ''.join(self.completion_matches):
+ unclosed_quote = "'"
+ else:
+ unclosed_quote = '"'
+
+ self.completion_matches = [unclosed_quote + match for match in self.completion_matches]
+
+ # Check if we need to remove text from the beginning of tab completions
+ elif text_to_remove:
+ self.completion_matches = \
+ [m.replace(text_to_remove, '', 1) for m in self.completion_matches]
+
+ # Check if we need to restore a shortcut in the tab completions
+ # so it doesn't get erased from the command line
+ if shortcut_to_restore:
+ self.completion_matches = \
+ [shortcut_to_restore + match for match in self.completion_matches]
else:
# Complete token against aliases and command names
@@ -2226,7 +2150,7 @@ class Cmd(cmd.Cmd):
self.completion_matches[0] += str_to_append
# Otherwise sort matches
- elif len(self.completion_matches) > 0:
+ elif self.completion_matches:
self.completion_matches.sort()
self.display_matches.sort()
@@ -2871,7 +2795,7 @@ Usage: Usage: alias [name] | [<name> <value>]
alias save_results "print_results > out.txt"
"""
# If no args were given, then print a list of current aliases
- if len(arglist) == 0:
+ if not arglist:
for cur_alias in self.aliases:
self.poutput("alias {} {}".format(cur_alias, self.aliases[cur_alias]))
@@ -2919,7 +2843,7 @@ Usage: Usage: unalias [-a] name [name ...]
Options:
-a remove all alias definitions
"""
- if len(arglist) == 0:
+ if not arglist:
self.do_help('unalias')
if '-a' in arglist:
@@ -3233,7 +3157,7 @@ Usage: Usage: unalias [-a] name [name ...]
# Support expanding ~ in quoted paths
for index, _ in enumerate(tokens):
- if len(tokens[index]) > 0:
+ if tokens[index]:
# Check if the token is quoted. Since shlex.split() passed, there isn't
# an unclosed quote, so we only need to check the first character.
first_char = tokens[index][0]
diff --git a/docs/conf.py b/docs/conf.py
index 93f1c4c9..c654c7bd 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -62,7 +62,7 @@ author = 'Catherine Devlin and Todd Leonhardt'
# The short X.Y version.
version = '0.8'
# The full version, including alpha/beta/rc tags.
-release = '0.8.4'
+release = '0.8.5'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
diff --git a/examples/tab_completion.py b/examples/tab_completion.py
index 93d6c0ef..1419b294 100755
--- a/examples/tab_completion.py
+++ b/examples/tab_completion.py
@@ -8,8 +8,8 @@ import cmd2
from cmd2 import with_argparser, with_argument_list
# List of strings used with flag and index based completion functions
-food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
-sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football']
+food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
+sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
class TabCompleteExample(cmd2.Cmd):
diff --git a/setup.py b/setup.py
index 2221eae0..3e13d5b4 100755
--- a/setup.py
+++ b/setup.py
@@ -8,7 +8,7 @@ import sys
import setuptools
from setuptools import setup
-VERSION = '0.8.4'
+VERSION = '0.8.5'
DESCRIPTION = "cmd2 - a tool for building interactive command line applications in Python"
LONG_DESCRIPTION = """cmd2 is a tool for building interactive command line applications in Python. Its goal is to make
it quick and easy for developers to build feature-rich and user-friendly interactive command line applications. It
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 75d27869..58f6dbae 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -26,7 +26,7 @@ from conftest import run_cmd, normalize, BASE_HELP, BASE_HELP_VERBOSE, \
def test_ver():
- assert cmd2.__version__ == '0.8.4'
+ assert cmd2.__version__ == '0.8.5'
def test_empty_statement(base_app):
diff --git a/tests/test_completion.py b/tests/test_completion.py
index f7caf03a..b102bc0a 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -29,16 +29,17 @@ except ImportError:
pass
-@pytest.fixture
-def cmd2_app():
- c = cmd2.Cmd()
- return c
-
-
# List of strings used with completion functions
-food_item_strs = ['Pizza', 'Hamburger', 'Ham', 'Potato']
+food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
-delimited_strs = ['/home/user/file.txt', '/home/user/prog.c', '/home/otheruser/maps']
+delimited_strs = \
+ [
+ '/home/user/file.txt',
+ '/home/user/file space.txt',
+ '/home/user/prog.c',
+ '/home/other user/maps',
+ '/home/other user/tests'
+ ]
# Dictionary used with flag based completion functions
flag_dict = \
@@ -59,6 +60,33 @@ index_dict = \
2: sport_item_strs, # Tab-complete sport items at index 2 in command line
}
+
+class CompletionsExample(cmd2.Cmd):
+ """
+ Example cmd2 application used to exercise tab-completion tests
+ """
+ def __init__(self):
+ cmd2.Cmd.__init__(self)
+
+ def do_test_basic(self, args):
+ pass
+
+ def complete_test_basic(self, text, line, begidx, endidx):
+ return self.basic_complete(text, line, begidx, endidx, food_item_strs)
+
+ def do_test_delimited(self, args):
+ pass
+
+ def complete_test_delimited(self, text, line, begidx, endidx):
+ return self.delimiter_complete(text, line, begidx, endidx, delimited_strs, '/')
+
+
+@pytest.fixture
+def cmd2_app():
+ c = CompletionsExample()
+ return c
+
+
def complete_tester(text, line, begidx, endidx, app):
"""
This is a convenience function to test cmd2.complete() since
@@ -425,12 +453,12 @@ def test_delimiter_completion(cmd2_app):
cmd2_app.delimiter_complete(text, line, begidx, endidx, delimited_strs, '/')
- # Remove duplicates from display_matches and sort it. This is typically done in the display function.
+ # Remove duplicates from display_matches and sort it. This is typically done in complete().
display_set = set(cmd2_app.display_matches)
display_list = list(display_set)
display_list.sort()
- assert display_list == ['otheruser', 'user']
+ assert display_list == ['other user', 'user']
def test_flag_based_completion_single(cmd2_app):
text = 'Pi'
@@ -638,6 +666,113 @@ def test_parseline_expands_shortcuts(cmd2_app):
assert args == 'cat foobar.txt'
assert line.replace('!', 'shell ') == out_line
+def test_add_opening_quote_basic_no_text(cmd2_app):
+ text = ''
+ line = 'test_basic {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # The whole list will be returned with no opening quotes added
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and cmd2_app.completion_matches == sorted(food_item_strs)
+
+def test_add_opening_quote_basic_nothing_added(cmd2_app):
+ text = 'P'
+ line = 'test_basic {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and cmd2_app.completion_matches == ['Pizza', 'Potato']
+
+def test_add_opening_quote_basic_quote_added(cmd2_app):
+ text = 'Ha'
+ line = 'test_basic {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected = sorted(['"Ham', '"Ham Sandwich'])
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and cmd2_app.completion_matches == expected
+
+def test_add_opening_quote_basic_text_is_common_prefix(cmd2_app):
+ # This tests when the text entered is the same as the common prefix of the matches
+ text = 'Ham'
+ line = 'test_basic {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected = sorted(['"Ham', '"Ham Sandwich'])
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and cmd2_app.completion_matches == expected
+
+def test_add_opening_quote_delimited_no_text(cmd2_app):
+ text = ''
+ line = 'test_delimited {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ # The whole list will be returned with no opening quotes added
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and cmd2_app.completion_matches == sorted(delimited_strs)
+
+def test_add_opening_quote_delimited_nothing_added(cmd2_app):
+ text = '/ho'
+ line = 'test_delimited {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected_matches = sorted(delimited_strs)
+ expected_display = sorted(['other user', 'user'])
+
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and \
+ cmd2_app.completion_matches == expected_matches and \
+ cmd2_app.display_matches == expected_display
+
+def test_add_opening_quote_delimited_quote_added(cmd2_app):
+ text = '/home/user/fi'
+ line = 'test_delimited {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected_common_prefix = '"/home/user/file'
+ expected_display = sorted(['file.txt', 'file space.txt'])
+
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and \
+ os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \
+ cmd2_app.display_matches == expected_display
+
+def test_add_opening_quote_delimited_text_is_common_prefix(cmd2_app):
+ # This tests when the text entered is the same as the common prefix of the matches
+ text = '/home/user/file'
+ line = 'test_delimited {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected_common_prefix = '"/home/user/file'
+ expected_display = sorted(['file.txt', 'file space.txt'])
+
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and \
+ os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \
+ cmd2_app.display_matches == expected_display
+
+def test_add_opening_quote_delimited_space_in_prefix(cmd2_app):
+ # This test when a space appears before the part of the string that is the display match
+ text = '/home/oth'
+ line = 'test_delimited {}'.format(text)
+ endidx = len(line)
+ begidx = endidx - len(text)
+
+ expected_common_prefix = '"/home/other user/'
+ expected_display = ['maps', 'tests']
+
+ first_match = complete_tester(text, line, begidx, endidx, cmd2_app)
+ assert first_match is not None and \
+ os.path.commonprefix(cmd2_app.completion_matches) == expected_common_prefix and \
+ cmd2_app.display_matches == expected_display
class SubcommandsExample(cmd2.Cmd):
"""
@@ -787,19 +922,6 @@ def test_subcommand_tab_completion_with_no_completer(sc_app):
first_match = complete_tester(text, line, begidx, endidx, sc_app)
assert first_match is None
-def test_subcommand_tab_completion_add_quote(sc_app):
- # This makes sure an opening quote is added to the readline line buffer
- text = 'Space'
- line = 'base sport {}'.format(text)
- endidx = len(line)
- begidx = endidx - len(text)
-
- first_match = complete_tester(text, line, begidx, endidx, sc_app)
-
- # No matches are returned when an opening quote is added to the screen
- assert first_match is None
- assert readline.get_line_buffer() == 'base sport "Space Ball" '
-
def test_subcommand_tab_completion_space_in_text(sc_app):
text = 'B'
line = 'base sport "Space {}'.format(text)