summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2.py9
-rw-r--r--tests/test_completion.py33
2 files changed, 23 insertions, 19 deletions
diff --git a/cmd2.py b/cmd2.py
index 64ba5747..feb6c69b 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -382,10 +382,13 @@ def path_complete(text, line, begidx, endidx, dir_exe_only=False, dir_only=False
# Used if we need to prepend a directory to the search string
dirname = ''
- # Handle ~ in search text
- if token_being_completed.startswith('~'):
+ # If the user only entered a '~', then complete it with a slash
+ if token_being_completed == '~':
+ return [os.path.sep]
- # Tilde without separator is invalid
+ elif token_being_completed.startswith('~'):
+
+ # Tilde without separator between path is invalid
if not token_being_completed.startswith('~' + os.path.sep):
return []
diff --git a/tests/test_completion.py b/tests/test_completion.py
index 57424032..8b7a2d14 100644
--- a/tests/test_completion.py
+++ b/tests/test_completion.py
@@ -353,7 +353,7 @@ def test_default_to_shell_completion(cmd2_app, request):
def test_path_completion_cwd():
# Run path complete with no path and no search text
text = ''
- line = 'shell ls '
+ line = 'shell ls {}'.format(text)
endidx = len(line)
begidx = endidx - len(text)
completions_empty = path_complete(text, line, begidx, endidx)
@@ -369,15 +369,6 @@ def test_path_completion_cwd():
assert completions_empty == completions_cwd
assert completions_cwd
-def test_path_completion_invalid_syntax():
- text = ''
- line = 'shell ls ~'
- endidx = len(line)
- begidx = endidx
-
- # Can't have a ~ without a separating slash
- assert path_complete(text, line, begidx, endidx) == []
-
def test_path_completion_doesnt_match_wildcards(request):
test_dir = os.path.dirname(request.module.__file__)
@@ -391,9 +382,20 @@ 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_just_tilde():
# Run path with just a tilde
text = ''
+ line = 'shell fake ~'
+ endidx = len(line)
+ begidx = endidx - len(text)
+ completions_tilde = path_complete(text, line, begidx, endidx)
+
+ # 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'):
cmd = 'dir'
else:
@@ -401,19 +403,18 @@ def test_path_completion_user_expansion():
line = 'shell {} ~{}'.format(cmd, os.path.sep)
endidx = len(line)
- begidx = endidx - len(text)
- completions_tilde = path_complete(text, line, begidx, endidx)
+ begidx = endidx
+ 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)
+ begidx = endidx
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__)