@@ -2264,3 +2264,83 @@ def mock_read_text(fname, silent=False):
2264
2264
2265
2265
# Verify no files were added
2266
2266
self .assertEqual (len (coder .abs_fnames ), 0 )
2267
+
2268
+ def test_cmd_regex (self ):
2269
+ from unittest import mock
2270
+
2271
+ # Create test environment
2272
+ with GitTemporaryDirectory () as repo_dir :
2273
+ io = InputOutput (pretty = False , fancy_input = False , yes = True )
2274
+ coder = Coder .create (self .GPT35 , None , io )
2275
+
2276
+ # Setup test files
2277
+ file1_path = Path (repo_dir ) / "file1.py"
2278
+ file2_path = Path (repo_dir ) / "file2.py"
2279
+ file3_path = Path (repo_dir ) / "file3.py"
2280
+ file1_path .write_text ("def test_function(): pass" )
2281
+ file2_path .write_text ("# This file contains search_string in a comment" )
2282
+ file3_path .write_text ("def another_function(value=123): return value" )
2283
+
2284
+ # Mock get_all_abs_files to return our test files
2285
+ coder .get_all_abs_files = mock .MagicMock (return_value = [
2286
+ str (file1_path ),
2287
+ str (file2_path ),
2288
+ str (file3_path )
2289
+ ])
2290
+
2291
+ # Mock read_text to return the actual file contents
2292
+ def mock_read_text (fname , silent = False ):
2293
+ if str (file1_path ) in str (fname ):
2294
+ return "def test_function(): pass"
2295
+ elif str (file2_path ) in str (fname ):
2296
+ return "# This file contains search_string in a comment"
2297
+ elif str (file3_path ) in str (fname ):
2298
+ return "def another_function(value=123): return value"
2299
+ return None
2300
+
2301
+ io .read_text = mock .MagicMock (side_effect = mock_read_text )
2302
+
2303
+ # Run the regex command
2304
+ commands = Commands (io , coder )
2305
+
2306
+ # Test searching with a regex pattern that matches multiple files
2307
+ commands .cmd_regex (r"def\s+\w+\(" )
2308
+
2309
+ # Check that the expected files were added to the chat
2310
+ self .assertEqual (len (coder .abs_fnames ), 2 )
2311
+
2312
+ # Convert abs_fnames to a list of rel paths for easier comparison
2313
+ added_files = [str (Path (path ).name ) for path in coder .abs_fnames ]
2314
+ self .assertIn ("file1.py" , added_files )
2315
+ self .assertIn ("file3.py" , added_files )
2316
+ self .assertNotIn ("file2.py" , added_files )
2317
+
2318
+ # Clear added files
2319
+ coder .abs_fnames .clear ()
2320
+
2321
+ # Test searching with a regex that only matches one file
2322
+ commands .cmd_regex (r"value=\d+" )
2323
+
2324
+ # Check that only the file with the pattern was added
2325
+ self .assertEqual (len (coder .abs_fnames ), 1 )
2326
+ added_files = [str (Path (path ).name ) for path in coder .abs_fnames ]
2327
+ self .assertIn ("file3.py" , added_files )
2328
+ self .assertNotIn ("file1.py" , added_files )
2329
+ self .assertNotIn ("file2.py" , added_files )
2330
+
2331
+ # Clear added files
2332
+ coder .abs_fnames .clear ()
2333
+
2334
+ # Test with an invalid regex pattern
2335
+ with mock .patch .object (io , "tool_error" ) as mock_tool_error :
2336
+ commands .cmd_regex (r"[invalid regex" )
2337
+ mock_tool_error .assert_called_with (mock .ANY ) # Should call tool_error with an error message
2338
+
2339
+ # Test searching for a non-matching pattern
2340
+ coder .abs_fnames .clear ()
2341
+ with mock .patch .object (io , "tool_error" ) as mock_tool_error :
2342
+ commands .cmd_regex (r"nonexistent_pattern\d+" )
2343
+ mock_tool_error .assert_called_with ("No files found matching regex pattern 'nonexistent_pattern\\ d+'." )
2344
+
2345
+ # Verify no files were added
2346
+ self .assertEqual (len (coder .abs_fnames ), 0 )
0 commit comments