File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -166,6 +166,11 @@ def execute(
166166 if tool_name == "grep" :
167167 cmd_args .append ("--exclude-dir=.git" )
168168
169+ # Pattern may start with a dash, this will make rg interpret it as
170+ # an option, "-e" works around this.
171+ if tool_name == "rg" :
172+ cmd_args .extend (["-e" ])
173+
169174 # Add pattern and directory path
170175 cmd_args .extend (["--" , pattern , str (search_dir_path )])
171176
Original file line number Diff line number Diff line change 1+ import shutil
2+ from types import SimpleNamespace
3+ from unittest .mock import Mock
4+
5+ import pytest
6+
7+ from aider .tools import grep
8+
9+
10+ @pytest .mark .skipif (shutil .which ("rg" ) is None , reason = "rg is required" )
11+ @pytest .mark .parametrize ("search_term" , [
12+ "--pattern" ,
13+ "--pat tern" ,
14+ "-pattern" ,
15+ "--" ,
16+ "-- -test" ,
17+ ])
18+ def test_dash_prefixed_pattern_is_searched_literally (search_term , tmp_path , monkeypatch ):
19+ sample = tmp_path / "example.txt"
20+ sample .write_text (f"flag { search_term } should be found\n " )
21+
22+ coder = SimpleNamespace (
23+ repo = SimpleNamespace (root = str (tmp_path )),
24+ io = SimpleNamespace (
25+ tool_error = Mock (),
26+ tool_output = Mock (),
27+ tool_warning = Mock (),
28+ ),
29+ verbose = False ,
30+ root = str (tmp_path ),
31+ )
32+
33+ monkeypatch .setattr (
34+ grep .Tool , "_find_search_tool" , lambda : ("rg" , shutil .which ("rg" ))
35+ )
36+
37+ result = grep .Tool .execute (
38+ coder ,
39+ pattern = search_term ,
40+ file_pattern = "*.txt" ,
41+ directory = "." ,
42+ use_regex = False ,
43+ case_insensitive = False ,
44+ context_before = 0 ,
45+ context_after = 0 ,
46+ )
47+
48+ assert "Found matches" in result
49+ assert search_term in result
50+ coder .io .tool_error .assert_not_called ()
You can’t perform that action at this time.
0 commit comments