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
4 changes: 4 additions & 0 deletions peekdocs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,10 @@ def _index_progress(done, total_count, filename):
return 0 if total_matches > 0 else 1

want_rank = "--rank" in args
if want_rank:
# Strip it so it isn't swallowed as a positional search term
# (which would corrupt the query, e.g. AND-mode → zero matches).
args.remove("--rank")
no_index = "--no-index" in args
if no_index:
args.remove("--no-index")
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ def test_search_finds_matches(tmp_path, monkeypatch, capsys):
assert highlighted_runs[1].text == "Hello"


def test_rank_flag_not_swallowed_as_search_term(tmp_path, monkeypatch, capsys):
# Regression: --rank must be stripped from args, not consumed as a
# positional search term. It once leaked into the query, so an AND search
# "term --rank" matched nothing (real report: 351 matches -> 0 with rank on).
# Ranking reorders results; it must never change the match COUNT.
(tmp_path / "a.txt").write_text("bowling night was great\n")
(tmp_path / "b.txt").write_text("more bowling night here\n")
monkeypatch.chdir(tmp_path)
main(["--index"])
capsys.readouterr()

# OR mode: --rank must not change the match count (2 files, both match).
assert main(["bowling"]) == 0
plain = capsys.readouterr().out
assert main(["bowling", "--rank"]) == 0
ranked = capsys.readouterr().out
assert f"{HIGHLIGHT}2{RESET} match(es)" in plain
assert f"{HIGHLIGHT}2{RESET} match(es)" in ranked # NOT 0 — query wasn't polluted
# The actual query echo (not the command echo) must be just the term.
assert "on [bowling --rank]" not in ranked.replace(HIGHLIGHT, "").replace(RESET, "")

# AND mode — the exact case reported (351 -> 0). Return code 0 = matches
# found; before the fix "bowling AND night AND --rank" matched nothing (1).
assert main(["bowling", "night", "-a"]) == 0
capsys.readouterr()
assert main(["bowling", "night", "-a", "--rank"]) == 0


def test_search_no_matches(tmp_path, monkeypatch, capsys):
doc = Document()
doc.add_paragraph("Nothing here")
Expand Down
Loading