Skip to content

Commit 2c34f12

Browse files
Recognize forward slashes in Windows shell command completion (#1830)
1 parent 56a9e56 commit 2c34f12

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎cmd2/cmd2.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ def shell_cmd_complete(
25392539
return Completions()
25402540

25412541
# If the search text is a bare command name (no '~' or path separator), complete executables from PATH
2542-
if not text.startswith("~") and os.path.sep not in text:
2542+
if not text.startswith("~") and os.path.sep not in text and (os.path.altsep is None or os.path.altsep not in text):
25432543
items = [CompletionItem(exe) for exe in utils.get_exes_in_path(text)]
25442544
return Completions(items=items)
25452545

‎tests/test_completion.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,26 @@ def test_shell_command_completion_literal_brackets_in_given_path(cmd_app, tmp_pa
431431
assert completions.to_strings() == (str(bin_dir / "[ab]-tool"),)
432432

433433

434+
@pytest.mark.parametrize("absolute", [False, True], ids=["relative", "absolute"])
435+
def test_shell_command_completion_windows_forward_slashes(cmd_app, tmp_path, monkeypatch, absolute) -> None:
436+
bin_dir = tmp_path / "bin"
437+
bin_dir.mkdir()
438+
make_exes(bin_dir, ("my-other.exe",))
439+
make_exes(tmp_path, ("my-local.exe",))
440+
monkeypatch.chdir(tmp_path)
441+
monkeypatch.setenv("PATH", str(bin_dir))
442+
443+
text = (tmp_path / "my").as_posix() if absolute else "./my"
444+
expected = os.path.join(os.path.dirname(text), "my-local.exe")
445+
line = f"shell {text}"
446+
monkeypatch.setattr(os.path, "sep", "\\")
447+
monkeypatch.setattr(os.path, "altsep", "/")
448+
449+
completions = cmd_app.shell_cmd_complete(text, line, len(line) - len(text), len(line))
450+
451+
assert completions.to_strings() == (expected,)
452+
453+
434454
@pytest.mark.skipif(sys.platform == "win32", reason="'*' and '?' are not legal in Windows file names")
435455
@pytest.mark.parametrize("prefix", ["my*", "my?"])
436456
def test_shell_command_completion_literal_wildcards_in_name(cmd_app, tmp_path, monkeypatch, prefix) -> None:

0 commit comments

Comments
 (0)