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: 2 additions & 0 deletions openhands-tools/openhands/tools/grep/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def _execute_with_ripgrep(
"rg",
"-l",
"-i",
# -e keeps a pattern that starts with "-" from being read as a flag.
"-e",
action.pattern,
str(search_path),
"--sortr=modified",
Expand Down
23 changes: 22 additions & 1 deletion tests/tools/grep/test_grep_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import openhands.tools.grep.impl as grep_impl
from openhands.tools.grep import GrepAction
from openhands.tools.grep.impl import GrepExecutor
from openhands.tools.utils import _check_grep_available
from openhands.tools.utils import _check_grep_available, _check_ripgrep_available


def test_grep_executor_initialization():
Expand Down Expand Up @@ -249,6 +249,27 @@ def test_grep_executor_invalid_regex():
assert "Invalid regex pattern" in observation.text


@pytest.mark.skipif(not _check_ripgrep_available(), reason="ripgrep not available")
def test_pattern_starting_with_dash(tmp_path):
(tmp_path / "flags.txt").write_text("run with -foo enabled\n")
(tmp_path / "other.txt").write_text("nothing here\n")

observation = GrepExecutor(working_dir=str(tmp_path))(GrepAction(pattern="-foo"))

assert observation.is_error is False
assert [Path(match).name for match in observation.matches] == ["flags.txt"]


@pytest.mark.skipif(not _check_ripgrep_available(), reason="ripgrep not available")
def test_ripgrep_pattern_cannot_inject_flags(tmp_path):
(tmp_path / "a.txt").write_text("alpha\n")
(tmp_path / "b.txt").write_text("beta\n")

observation = GrepExecutor(working_dir=str(tmp_path))(GrepAction(pattern="--files"))

assert observation.matches == []


def test_grep_executor_concurrent():
"""Test that concurrent grep calls return correct results.

Expand Down
Loading