Skip to content

Fix history search for a lone slash matching every item - #1858

Merged
tleonhardt merged 2 commits into
python-cmd2:mainfrom
mokshupadhyay:fix/history-single-slash-search
Dec 3, 2026
Merged

tleonhardt merged 2 commits into
python-cmd2:mainfrom
mokshupadhyay:fix/history-single-slash-search

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

The history command treats an argument that starts and ends with / as a regex, with the slashes as delimiters (history /foo/). A lone / both starts and ends with a slash, so it was treated as a regex with an empty pattern between the delimiters, which matches every item. Searching the history for a slash returned the whole history.

Real result with three history items, only one of which contains a slash (History.regex_search("/")), on main and with this change:

main:   [1, 2, 3]     every item
after:  [2]           only the item containing "/"

The fix requires more than one character before treating the argument as /regex/, in History.regex_search and in the call site that decides between regex, index and text search (Cmd._get_history). A lone / is now a plain text search, and /regex/ behaves as before.

Tests: test_history_regex_search_single_slash_is_literal and test_history_literal_search_for_single_slash in tests/test_history.py; both fail on main and pass now. pytest tests/test_history.py: 836 passed (with the two new ones). make check (ruff, prettier, typos, ty, mypy) passes. I did not run make docs-test, and I did not add a CHANGELOG.md entry because AGENTS.md asks agents not to commit markdown files without asking first; let me know if you want one.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (9963c78) to head (767c876).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1858   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6773     6773           
=======================================
  Hits         6757     6757           
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt

Copy link
Copy Markdown
Member

Thanks for the PR, @mokshupadhyay, and for covering both the history command and History.regex_search().

I reproduced this on 4.4.5. With help, help /tmp, and help history in history, history / returned all three. On that release, history /// returns only help /tmp. The outer slashes delimit the regex and the middle slash matches literally.

I agree this is a bug. A lone slash cannot enclose a pattern, but 4.4.5 counts it as both delimiters and strips it away. I checked that your patch makes history / select only help /tmp, while history // still selects all three entries as an explicitly empty regex. The full test suite passed locally (3,222 passed, 3 skipped).

Could you use a built-in command such as set prompt / in place of !ls /tmp in the CLI test and update the expected output? That keeps the test independent of the host shell and filesystem. The implementation looks good to me.

Keep the test independent of the host shell and filesystem, as asked in review.
@mokshupadhyay

Copy link
Copy Markdown
Contributor Author

Thanks for reproducing it and for the review. Done in a follow-up commit: the CLI test now runs set prompt / (a built-in command) instead of !ls /tmp, and the expected output is 2 set prompt /. It still fails on the previous source (the lone-slash search returns every item) and passes with the fix. Full suite: 3220 passed, 5 skipped; ruff check and ruff format --check are clean on the test file.

@tleonhardt
tleonhardt merged commit 8818df4 into python-cmd2:main Dec 3, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants