This repository was archived by the owner on Apr 23, 2025. It is now read-only.
forked from raizamartin/gemini-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tree_tool.py
More file actions
396 lines (329 loc) · 14.5 KB
/
test_tree_tool.py
File metadata and controls
396 lines (329 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""
Tests for the tree tool module.
"""
import os
import subprocess
import tempfile
from pathlib import Path
import pytest
from unittest.mock import patch, MagicMock, mock_open
# Direct import for coverage tracking
import src.cli_code.tools.tree_tool
from src.cli_code.tools.tree_tool import TreeTool, DEFAULT_TREE_DEPTH, MAX_TREE_DEPTH
class TestTreeTool:
"""Tests for the TreeTool class."""
def test_init(self):
"""Test initialization of TreeTool."""
tool = TreeTool()
assert tool.name == "tree"
assert "Displays the directory structure as a tree" in tool.description
assert "depth" in tool.args_schema
assert "path" in tool.args_schema
assert len(tool.required_args) == 0 # All args are optional
@patch("subprocess.run")
def test_tree_command_success(self, mock_run):
"""Test successful execution of tree command."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1\n └── file2.txt"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert "file1.txt" in result
assert "dir1" in result
assert "file2.txt" in result
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", str(DEFAULT_TREE_DEPTH)]
assert kwargs.get("capture_output") is True
assert kwargs.get("text") is True
@patch("subprocess.run")
def test_tree_with_custom_path(self, mock_run):
"""Test tree command with custom path."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = "test_dir\n├── file1.txt\n└── file2.txt"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(path="test_dir")
# Verify results
assert "test_dir" in result
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", str(DEFAULT_TREE_DEPTH), "test_dir"]
@patch("subprocess.run")
def test_tree_with_custom_depth(self, mock_run):
"""Test tree command with custom depth."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(depth=2)
# Verify results
assert "file1.txt" in result
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", "2"] # Depth should be converted to string
@patch("subprocess.run")
def test_tree_with_string_depth(self, mock_run):
"""Test tree command with depth as string."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(depth="2") # String instead of int
# Verify results
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", "2"] # Should be converted properly
@patch("subprocess.run")
def test_tree_with_invalid_depth_string(self, mock_run):
"""Test tree command with invalid depth string."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(depth="invalid") # Invalid depth string
# Verify results
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", str(DEFAULT_TREE_DEPTH)] # Should use default
@patch("subprocess.run")
def test_tree_with_too_large_depth(self, mock_run):
"""Test tree command with depth larger than maximum."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(depth=MAX_TREE_DEPTH + 5) # Too large
# Verify results
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", str(MAX_TREE_DEPTH)] # Should be clamped to max
@patch("subprocess.run")
def test_tree_with_too_small_depth(self, mock_run):
"""Test tree command with depth smaller than minimum."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
mock_process.stdout = ".\n├── file1.txt\n└── dir1"
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute(depth=0) # Too small
# Verify results
mock_run.assert_called_once()
args, kwargs = mock_run.call_args
assert args[0] == ["tree", "-L", "1"] # Should be clamped to min (1)
@patch("subprocess.run")
def test_tree_truncate_long_output(self, mock_run):
"""Test tree command with very long output that gets truncated."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 0
# Create an output with 201 lines (more than the 200 line limit)
mock_process.stdout = "\n".join([f"line{i}" for i in range(201)])
mock_run.return_value = mock_process
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert "... (output truncated)" in result
# Result should have only 200 lines + truncation message
assert len(result.splitlines()) == 201
# The 200th line should be "line199"
assert "line199" in result
# The 201st line (which would be "line200") should NOT be in the result
assert "line200" not in result
@patch("subprocess.run")
def test_tree_command_not_found_fallback(self, mock_run):
"""Test fallback when tree command is not found."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 127 # Command not found
mock_process.stderr = "tree: command not found"
mock_run.return_value = mock_process
# Mock the fallback implementation
with patch.object(TreeTool, '_fallback_tree_implementation') as mock_fallback:
mock_fallback.return_value = "Fallback tree output"
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert result == "Fallback tree output"
mock_fallback.assert_called_once_with(".", DEFAULT_TREE_DEPTH)
@patch("subprocess.run")
def test_tree_command_error_fallback(self, mock_run):
"""Test fallback when tree command returns an error."""
# Setup mock
mock_process = MagicMock()
mock_process.returncode = 1 # Error
mock_process.stderr = "Some error"
mock_run.return_value = mock_process
# Mock the fallback implementation
with patch.object(TreeTool, '_fallback_tree_implementation') as mock_fallback:
mock_fallback.return_value = "Fallback tree output"
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert result == "Fallback tree output"
mock_fallback.assert_called_once_with(".", DEFAULT_TREE_DEPTH)
@patch("subprocess.run")
def test_tree_command_file_not_found(self, mock_run):
"""Test when the 'tree' command itself isn't found."""
# Setup mock
mock_run.side_effect = FileNotFoundError("No such file or directory: 'tree'")
# Mock the fallback implementation
with patch.object(TreeTool, '_fallback_tree_implementation') as mock_fallback:
mock_fallback.return_value = "Fallback tree output"
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert result == "Fallback tree output"
mock_fallback.assert_called_once_with(".", DEFAULT_TREE_DEPTH)
@patch("subprocess.run")
def test_tree_command_timeout(self, mock_run):
"""Test tree command timeout."""
# Setup mock
mock_run.side_effect = subprocess.TimeoutExpired(cmd="tree", timeout=15)
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert "Error: Tree command timed out" in result
assert "too large or complex" in result
@patch("subprocess.run")
def test_tree_command_unexpected_error_with_fallback_success(self, mock_run):
"""Test unexpected error with successful fallback."""
# Setup mock
mock_run.side_effect = Exception("Unexpected error")
# Mock the fallback implementation
with patch.object(TreeTool, '_fallback_tree_implementation') as mock_fallback:
mock_fallback.return_value = "Fallback tree output"
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert result == "Fallback tree output"
mock_fallback.assert_called_once_with(".", DEFAULT_TREE_DEPTH)
@patch("subprocess.run")
def test_tree_command_unexpected_error_with_fallback_failure(self, mock_run):
"""Test unexpected error with failed fallback."""
# Setup mock
mock_run.side_effect = Exception("Unexpected error")
# Mock the fallback implementation
with patch.object(TreeTool, '_fallback_tree_implementation') as mock_fallback:
mock_fallback.side_effect = Exception("Fallback error")
# Execute tool
tool = TreeTool()
result = tool.execute()
# Verify results
assert "An unexpected error occurred" in result
assert "Unexpected error" in result
mock_fallback.assert_called_once_with(".", DEFAULT_TREE_DEPTH)
@patch("pathlib.Path.resolve")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_dir")
@patch("os.walk")
def test_fallback_tree_implementation(self, mock_walk, mock_is_dir, mock_exists, mock_resolve):
"""Test the fallback tree implementation."""
# Setup mocks
mock_resolve.return_value = Path("test_dir")
mock_exists.return_value = True
mock_is_dir.return_value = True
mock_walk.return_value = [
("test_dir", ["dir1", "dir2"], ["file1.txt"]),
("test_dir/dir1", [], ["file2.txt"]),
("test_dir/dir2", [], ["file3.txt"])
]
# Execute fallback
tool = TreeTool()
result = tool._fallback_tree_implementation("test_dir")
# Verify results
assert "." in result # Root directory
assert "dir1" in result # Subdirectories
assert "dir2" in result
assert "file1.txt" in result # Files
assert "file2.txt" in result
assert "file3.txt" in result
@patch("pathlib.Path.resolve")
@patch("pathlib.Path.exists")
def test_fallback_tree_nonexistent_path(self, mock_exists, mock_resolve):
"""Test fallback tree with nonexistent path."""
# Setup mocks
mock_resolve.return_value = Path("nonexistent")
mock_exists.return_value = False
# Execute fallback
tool = TreeTool()
result = tool._fallback_tree_implementation("nonexistent")
# Verify results
assert "Error: Path 'nonexistent' does not exist" in result
@patch("pathlib.Path.resolve")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_dir")
def test_fallback_tree_not_a_directory(self, mock_is_dir, mock_exists, mock_resolve):
"""Test fallback tree with a file path."""
# Setup mocks
mock_resolve.return_value = Path("file.txt")
mock_exists.return_value = True
mock_is_dir.return_value = False
# Execute fallback
tool = TreeTool()
result = tool._fallback_tree_implementation("file.txt")
# Verify results
assert "Error: Path 'file.txt' is not a directory" in result
@patch("pathlib.Path.resolve")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_dir")
@patch("os.walk")
def test_fallback_tree_truncate_long_output(self, mock_walk, mock_is_dir, mock_exists, mock_resolve):
"""Test fallback tree with very long output that gets truncated."""
# Setup mocks
mock_resolve.return_value = Path("test_dir")
mock_exists.return_value = True
mock_is_dir.return_value = True
# Create a directory structure with more than 200 files
dirs = [("test_dir", [], [f"file{i}.txt" for i in range(201)])]
mock_walk.return_value = dirs
# Execute fallback
tool = TreeTool()
result = tool._fallback_tree_implementation("test_dir")
# Verify results
assert "... (output truncated)" in result
assert len(result.splitlines()) <= 201 # 200 lines + truncation message
@patch("pathlib.Path.resolve")
@patch("pathlib.Path.exists")
@patch("pathlib.Path.is_dir")
@patch("os.walk")
def test_fallback_tree_error(self, mock_walk, mock_is_dir, mock_exists, mock_resolve):
"""Test error in fallback tree implementation."""
# Setup mocks
mock_resolve.return_value = Path("test_dir")
mock_exists.return_value = True
mock_is_dir.return_value = True
mock_walk.side_effect = Exception("Unexpected error")
# Execute fallback
tool = TreeTool()
result = tool._fallback_tree_implementation("test_dir")
# Verify results
assert "Error generating directory tree" in result
assert "Unexpected error" in result