From d84fc8fccca777f7324879679af0e43047d067a6 Mon Sep 17 00:00:00 2001 From: anticipate218 <3481402509@qq.com> Date: Sat, 14 Nov 2026 16:39:19 +0800 Subject: [PATCH] fix: complete literal bracketed file paths --- CHANGELOG.md | 6 +++++ cmd2/cmd2.py | 6 ++--- tests/test_completion.py | 53 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2046f23fc..84cf989e2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## Unreleased + +- Bug Fixes + - Fixed file path completion for names containing square brackets, including bracketed working + directories and home directories. + ## 4.4.2 (November 11, 2026) - Security diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 2ae2937ce..4107fbfc6 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2374,7 +2374,7 @@ def path_complete( # If the search text is blank, then search in the CWD for * if not text: - search_str = os.path.join(os.getcwd(), "*") + search_str = os.path.join(os.getcwd(), "") cwd_added = True else: # Purposely don't match any path containing wildcards @@ -2384,7 +2384,7 @@ def path_complete( return Completions() # Start the search string - search_str = text + "*" + search_str = text # Handle tilde expansion and completion if text.startswith("~"): @@ -2407,7 +2407,7 @@ def path_complete( cwd_added = True # Find all matching path completions - matches = glob.glob(search_str) + matches = glob.glob(glob.escape(search_str) + "*") # Filter out results that don't belong if path_filter is not None: diff --git a/tests/test_completion.py b/tests/test_completion.py index c66639773..39b204602 100644 --- a/tests/test_completion.py +++ b/tests/test_completion.py @@ -615,6 +615,59 @@ def test_path_completion_user_path_expansion(cmd_app) -> None: assert completions_tilde_slash == completions_home +@pytest.mark.parametrize("prefix", ["[ab]", "[!ab]"]) +def test_path_completion_literal_brackets_in_filename(cmd_app, tmp_path, monkeypatch, prefix) -> None: + for name in ("[ab].txt", "[!ab].txt", "a.txt", "c.txt"): + (tmp_path / name).touch() + monkeypatch.chdir(tmp_path) + line = f"shell cat {prefix}" + + completions = cmd_app.path_complete(prefix, line, len(line) - len(prefix), len(line)) + + assert completions.to_strings() == (prefix + ".txt",) + + +@pytest.mark.parametrize("mode", ["absolute", "relative", "empty_cwd", "cwd_prefix", "tilde"]) +def test_path_completion_literal_brackets_in_directory(cmd_app, tmp_path, monkeypatch, mode) -> None: + directory = tmp_path / "project[2026]" + directory.mkdir() + (directory / "target.txt").touch() + monkeypatch.chdir(tmp_path) + if mode == "absolute": + text = str(directory / "tar") + expected = str(directory / "target.txt") + elif mode == "relative": + text = os.path.join(directory.name, "tar") + expected = os.path.join(directory.name, "target.txt") + elif mode == "tilde": + monkeypatch.setenv("USERPROFILE" if sys.platform == "win32" else "HOME", str(directory)) + text = os.path.join("~", "tar") + expected = os.path.join("~", "target.txt") + else: + monkeypatch.chdir(directory) + text = "" if mode == "empty_cwd" else "tar" + expected = "target.txt" + line = f"shell cat {text}" + + completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line)) + + assert completions.to_strings() == (expected,) + + +def test_path_completion_literal_brackets_directory_filter(cmd_app, tmp_path, monkeypatch) -> None: + (tmp_path / "[ab]-dir").mkdir() + (tmp_path / "a-dir").mkdir() + (tmp_path / "[ab]-file.txt").touch() + monkeypatch.chdir(tmp_path) + text = "[ab]" + line = f"shell cat {text}" + + completions = cmd_app.path_complete(text, line, len(line) - len(text), len(line), path_filter=os.path.isdir) + + assert completions.to_strings() == ("[ab]-dir" + os.path.sep,) + assert not completions.allow_finalization + + def test_basic_completion(cmd_app) -> None: text = "P" line = f"list_food -f {text}"