diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-03-18 16:04:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-18 16:04:55 -0400 |
commit | 627360cfc71995af939569b2aee9dbc8141544d0 (patch) | |
tree | 8943d62abcc30d325061041788541fba577e0692 | |
parent | 4f63fdfdc9bf52e99220cd4b1a7a09ea5d78b239 (diff) | |
parent | 083080b5ae00a2a01b009bea85cd03a34cacf133 (diff) | |
download | cmd2-git-627360cfc71995af939569b2aee9dbc8141544d0.tar.gz |
Merge pull request #322 from python-cmd2/tilde
Fixed how we complete ~
-rwxr-xr-x | cmd2.py | 17 | ||||
-rw-r--r-- | tests/test_completion.py | 41 |
2 files changed, 38 insertions, 20 deletions
@@ -323,8 +323,6 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False if endidx == len(line) or (endidx < len(line) and line[endidx] != os.path.sep): add_trailing_sep_if_dir = True - add_sep_after_tilde = False - # Readline places begidx after ~ and path separators (/) so we need to extract any directory # path that appears before the search text dirname = line[prev_space_index + 1:begidx] @@ -342,14 +340,18 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False if not dirname: dirname = os.getcwd() elif dirname == '~': - # If tilde was used without separator, add a separator after the tilde in the completions - add_sep_after_tilde = True + # If a ~ was used without a separator between text, then this is invalid + if text: + return [] + # If only a ~ was entered, then complete it with a slash + else: + return [os.path.sep] # Build the search string search_str = os.path.join(dirname, text + '*') - # Expand "~" to the real user directory - search_str = os.path.expanduser(search_str) + # Expand "~" to the real user directory + search_str = os.path.expanduser(search_str) # Find all matching path completions path_completions = glob.glob(search_str) @@ -376,9 +378,6 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False # If it is a file and we are at the end of the line, then add a space if os.path.isfile(path_completions[0]) and endidx == len(line): completions[0] += ' ' - # If tilde was expanded without a separator, prepend one - elif os.path.isdir(path_completions[0]) and add_sep_after_tilde: - completions[0] = os.path.sep + completions[0] completions.sort() return completions diff --git a/tests/test_completion.py b/tests/test_completion.py index a51a4e1f..f749f084 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -359,7 +359,7 @@ def test_path_completion_cwd(): completions_empty = path_complete(text, line, begidx, endidx) # Run path complete with path set to the CWD - cwd = os.getcwd() + cwd = os.getcwd() + os.path.sep line = 'shell ls {}'.format(cwd) endidx = len(line) begidx = endidx - len(text) @@ -382,29 +382,48 @@ def test_path_completion_doesnt_match_wildcards(request): # Currently path completion doesn't accept wildcards, so will always return empty results assert path_complete(text, line, begidx, endidx) == [] -def test_path_completion_user_expansion(): +def test_path_completion_invalid_syntax(): + # Test a missing separator between a ~ and path + text = '' + line = 'shell fake ~Desktop' + endidx = len(line) + begidx = endidx - len(text) + + assert path_complete(text, line, begidx, endidx) == [] + +def test_path_completion_just_tilde(): # Run path with just a tilde text = '' - if sys.platform.startswith('win'): - line = 'shell dir ~{}'.format(text) - else: - line = 'shell ls ~{}'.format(text) + line = 'shell fake ~' endidx = len(line) begidx = endidx - len(text) completions_tilde = path_complete(text, line, begidx, endidx) - # Run path complete on the user's home directory - user_dir = os.path.expanduser('~') + # Path complete should return a slash + assert completions_tilde == [os.path.sep] + +def test_path_completion_user_expansion(): + # Run path with a tilde and a slash + text = '' if sys.platform.startswith('win'): - line = 'shell dir {}'.format(user_dir) + cmd = 'dir' else: - line = 'shell ls {}'.format(user_dir) + cmd = 'ls' + + line = 'shell {} ~{}'.format(cmd, os.path.sep) + endidx = len(line) + begidx = endidx - len(text) + completions_tilde_slash = path_complete(text, line, begidx, endidx) + + # Run path complete on the user's home directory + user_dir = os.path.expanduser('~') + os.path.sep + line = 'shell {} {}'.format(cmd, user_dir) endidx = len(line) begidx = endidx - len(text) completions_home = path_complete(text, line, begidx, endidx) # Verify that the results are the same in both cases - assert completions_tilde == completions_home + assert completions_tilde_slash == completions_home def test_path_completion_directories_only(request): test_dir = os.path.dirname(request.module.__file__) |