Skip to content

Commit fd3ad3d

Browse files
authored
Merge pull request #662 from python-cmd2/path_fix
Path fix
2 parents f3b9a35 + e566196 commit fd3ad3d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

cmd2/cmd2.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1141,7 +1141,11 @@ def complete_users() -> List[str]:
11411141

11421142
# Remove cwd if it was added to match the text readline expects
11431143
if cwd_added:
1144-
matches = [cur_path.replace(cwd + os.path.sep, '', 1) for cur_path in matches]
1144+
if cwd == os.path.sep:
1145+
to_replace = cwd
1146+
else:
1147+
to_replace = cwd + os.path.sep
1148+
matches = [cur_path.replace(to_replace, '', 1) for cur_path in matches]
11451149

11461150
# Restore the tilde string if we expanded one to match the text readline expects
11471151
if expanded_tilde_path:

tests/test_completion.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,25 @@ def test_path_completion_no_path(cmd2_app):
400400
assert completions_no_text == completions_cwd
401401
assert completions_cwd
402402

403+
404+
@pytest.mark.skipif(sys.platform == 'win32', reason="this only applies on systems where the root directory is a slash")
405+
def test_path_completion_cwd_is_root_dir(cmd2_app):
406+
# Change our CWD to root dir
407+
cwd = os.getcwd()
408+
os.chdir(os.path.sep)
409+
410+
text = ''
411+
line = 'shell ls {}'.format(text)
412+
endidx = len(line)
413+
begidx = endidx - len(text)
414+
completions = cmd2_app.path_complete(text, line, begidx, endidx)
415+
416+
# No match should start with a slash
417+
assert not any(match.startswith(os.path.sep) for match in completions)
418+
419+
# Restore CWD
420+
os.chdir(cwd)
421+
403422
def test_path_completion_doesnt_match_wildcards(cmd2_app, request):
404423
test_dir = os.path.dirname(request.module.__file__)
405424

0 commit comments

Comments
 (0)