Skip to content

Commit 62e1ab4

Browse files
committed
.
1 parent 8c84c74 commit 62e1ab4

File tree

1 file changed

+12
-92
lines changed

1 file changed

+12
-92
lines changed

tests/unit/codegen/extensions/test_tools.py

Lines changed: 12 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def large_codebase(tmpdir):
6868
for i in range(1, 401): # This will create a 400+ line file
6969
if i % 20 == 0:
7070
# Add some class methods periodically
71-
large_file_lines.extend([" @classmethod", f" def class_method_{i}(cls) -> None:", f" print('Class method {i}')", " return None", ""])
71+
large_file_lines.extend(
72+
[" @classmethod", f" def class_method_{i}(cls) -> None:", f" print('Class method {i}')", " return None", ""])
7273
else:
7374
# Add regular methods
7475
large_file_lines.extend(
@@ -123,7 +124,8 @@ def test_view_file_pagination(large_codebase):
123124
assert "def method_251" not in result.content # Method after page 1
124125

125126
# Test custom pagination range
126-
result = view_file(large_codebase, "src/large_file.py", start_line=200, end_line=250)
127+
result = view_file(large_codebase, "src/large_file.py",
128+
start_line=200, end_line=250)
127129
assert result.status == "success"
128130
assert result.start_line == 200
129131
assert result.end_line == 250
@@ -156,7 +158,8 @@ def test_view_file_pagination(large_codebase):
156158
assert len(result.content.splitlines()) <= 100
157159

158160
# Test line numbers display
159-
result = view_file(large_codebase, "src/large_file.py", start_line=198, end_line=202, line_numbers=True)
161+
result = view_file(large_codebase, "src/large_file.py",
162+
start_line=198, end_line=202, line_numbers=True)
160163
assert result.status == "success"
161164
assert "198|" in result.content
162165
assert "199|" in result.content
@@ -165,7 +168,8 @@ def test_view_file_pagination(large_codebase):
165168
assert "202|" in result.content
166169

167170
# Test without line numbers
168-
result = view_file(large_codebase, "src/large_file.py", start_line=198, end_line=202, line_numbers=False)
171+
result = view_file(large_codebase, "src/large_file.py",
172+
start_line=198, end_line=202, line_numbers=False)
169173
assert result.status == "success"
170174
assert "198|" not in result.content
171175
assert "199|" not in result.content
@@ -174,7 +178,8 @@ def test_view_file_pagination(large_codebase):
174178
def test_view_file_pagination_edge_cases(large_codebase):
175179
"""Test edge cases for file pagination."""
176180
# Test start_line > end_line (should respect provided end_line)
177-
result = view_file(large_codebase, "src/large_file.py", start_line=200, end_line=100)
181+
result = view_file(large_codebase, "src/large_file.py",
182+
start_line=200, end_line=100)
178183
assert result.status == "success"
179184
assert result.start_line == 200
180185
assert result.end_line == 100 # Should respect provided end_line
@@ -188,7 +193,8 @@ def test_view_file_pagination_edge_cases(large_codebase):
188193
assert result.has_more is False
189194

190195
# Test end_line > file length (should truncate to file length)
191-
result = view_file(large_codebase, "src/large_file.py", start_line=200, end_line=2000)
196+
result = view_file(large_codebase, "src/large_file.py",
197+
start_line=200, end_line=2000)
192198
assert result.status == "success"
193199
assert result.start_line == 200
194200
# Should respect max_lines and file length
@@ -236,92 +242,6 @@ def test_list_directory(codebase):
236242
assert expected_tree in rendered.strip()
237243

238244

239-
def test_search(codebase):
240-
"""Test searching the codebase."""
241-
result = search(codebase, "hello")
242-
assert result.status == "success"
243-
assert len(result.results) > 0
244-
245-
# Check that we found the right content
246-
assert any("hello" in match.match.lower() for file_result in result.results for match in file_result.matches)
247-
248-
# Check pagination info
249-
assert result.page == 1
250-
assert result.total_pages >= 1
251-
assert result.files_per_page == 10
252-
253-
254-
def test_search_regex(codebase):
255-
"""Test searching with regex."""
256-
# Search for function definitions
257-
result = search(codebase, r"def\s+\w+", use_regex=True)
258-
assert result.status == "success"
259-
assert len(result.results) > 0
260-
261-
# Should find both 'def hello' and 'def greet'
262-
matches = [match.line for file_result in result.results for match in file_result.matches]
263-
assert any("def hello" in match for match in matches)
264-
assert any("def greet" in match for match in matches)
265-
266-
267-
def test_search_pagination(codebase, tmpdir):
268-
"""Test search pagination."""
269-
# Create multiple files to test pagination
270-
files_dict = {}
271-
for i in range(15): # Create enough files to span multiple pages
272-
content = f"def function_{i}():\n print('Hello from function {i}!')"
273-
files_dict[f"src/file_{i}.py"] = content
274-
275-
# Create a new codebase with all the files
276-
with get_codebase_session(tmpdir=tmpdir, files=files_dict) as pagination_codebase:
277-
# Search with default pagination (page 1)
278-
result_page1 = search(pagination_codebase, "Hello", files_per_page=5)
279-
assert result_page1.status == "success"
280-
assert result_page1.page == 1
281-
assert len(result_page1.results) <= 5
282-
283-
# If we have enough results for multiple pages
284-
if result_page1.total_pages > 1:
285-
# Get page 2
286-
result_page2 = search(pagination_codebase, "Hello", page=2, files_per_page=5)
287-
assert result_page2.status == "success"
288-
assert result_page2.page == 2
289-
assert len(result_page2.results) <= 5
290-
291-
# Ensure different files on different pages
292-
page1_files = {r.filepath for r in result_page1.results}
293-
page2_files = {r.filepath for r in result_page2.results}
294-
assert not page1_files.intersection(page2_files)
295-
296-
297-
def test_search_uses_ripgrep(codebase, monkeypatch):
298-
"""Test that ripgrep is used when available."""
299-
# Track if ripgrep was called
300-
ripgrep_called = False
301-
302-
# Store original subprocess.run
303-
original_run = subprocess.run
304-
305-
# Mock subprocess.run to track calls and then call the original
306-
def mock_subprocess_run(*args, **kwargs):
307-
nonlocal ripgrep_called
308-
# Check if this is a ripgrep call
309-
if args and args[0] and isinstance(args[0], list) and args[0][0] == "rg":
310-
ripgrep_called = True
311-
# Call the original implementation
312-
return original_run(*args, **kwargs)
313-
314-
# Apply the mock
315-
monkeypatch.setattr(subprocess, "run", mock_subprocess_run)
316-
317-
# Perform a search
318-
result = search(codebase, "hello")
319-
assert result.status == "success"
320-
321-
# Verify ripgrep was called
322-
assert ripgrep_called, "Ripgrep was not used for the search"
323-
324-
325245
def test_edit_file(codebase):
326246
"""Test editing a file."""
327247
result = edit_file(codebase, "src/main.py", "print('edited')")

0 commit comments

Comments
 (0)