Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2539,7 +2539,7 @@ def shell_cmd_complete(
return Completions()

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

Expand Down
20 changes: 20 additions & 0 deletions tests/test_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,26 @@ def test_shell_command_completion_literal_brackets_in_given_path(cmd_app, tmp_pa
assert completions.to_strings() == (str(bin_dir / "[ab]-tool"),)


@pytest.mark.parametrize("absolute", [False, True], ids=["relative", "absolute"])
def test_shell_command_completion_windows_forward_slashes(cmd_app, tmp_path, monkeypatch, absolute) -> None:
bin_dir = tmp_path / "bin"
bin_dir.mkdir()
make_exes(bin_dir, ("my-other.exe",))
make_exes(tmp_path, ("my-local.exe",))
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("PATH", str(bin_dir))

text = (tmp_path / "my").as_posix() if absolute else "./my"
expected = os.path.join(os.path.dirname(text), "my-local.exe")
line = f"shell {text}"
monkeypatch.setattr(os.path, "sep", "\\")
monkeypatch.setattr(os.path, "altsep", "/")

completions = cmd_app.shell_cmd_complete(text, line, len(line) - len(text), len(line))

assert completions.to_strings() == (expected,)


@pytest.mark.skipif(sys.platform == "win32", reason="'*' and '?' are not legal in Windows file names")
@pytest.mark.parametrize("prefix", ["my*", "my?"])
def test_shell_command_completion_literal_wildcards_in_name(cmd_app, tmp_path, monkeypatch, prefix) -> None:
Expand Down
Loading