|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +"""Tests for ReadFileTool.""" |
| 16 | + |
15 | 17 | from pathlib import Path |
16 | 18 | from typing import Optional |
17 | 19 |
|
18 | 20 | from google.adk.environment._base_environment import BaseEnvironment |
19 | 21 | from google.adk.environment._base_environment import ExecutionResult |
20 | | -from google.adk.tools.environment._tools import ReadFileTool |
| 22 | +from google.adk.environment._local_environment import LocalEnvironment |
| 23 | +from google.adk.tools.environment._read_file_tool import ReadFileTool |
21 | 24 | import pytest |
| 25 | +import pytest_asyncio |
22 | 26 |
|
23 | 27 |
|
24 | 28 | class _StubEnvironment(BaseEnvironment): |
@@ -94,3 +98,99 @@ async def test_read_file_with_line_range_treats_shell_payload_as_literal_path(): |
94 | 98 | 'error': f'File not found: {payload}', |
95 | 99 | } |
96 | 100 | assert environment.execute_calls == [] |
| 101 | + |
| 102 | + |
| 103 | +@pytest_asyncio.fixture(name='env') |
| 104 | +async def _env(tmp_path: Path): |
| 105 | + """Create and initialize a LocalEnvironment backed by a temp directory.""" |
| 106 | + environment = LocalEnvironment(working_dir=tmp_path) |
| 107 | + await environment.initialize() |
| 108 | + yield environment |
| 109 | + await environment.close() |
| 110 | + |
| 111 | + |
| 112 | +class TestReadFileTool: |
| 113 | + """Tests for ReadFileTool behavior.""" |
| 114 | + |
| 115 | + @pytest.mark.asyncio |
| 116 | + async def test_read_file_with_line_range_returns_selected_lines( |
| 117 | + self, env: LocalEnvironment |
| 118 | + ): |
| 119 | + """Reads the requested line range and preserves line numbers.""" |
| 120 | + await env.write_file('sample.txt', 'line1\nline2\nline3\n') |
| 121 | + |
| 122 | + tool = ReadFileTool(env) |
| 123 | + result = await tool.run_async( |
| 124 | + args={'path': 'sample.txt', 'start_line': 2, 'end_line': 3}, |
| 125 | + tool_context=None, |
| 126 | + ) |
| 127 | + |
| 128 | + assert result == { |
| 129 | + 'status': 'ok', |
| 130 | + 'content': ' 2\tline2\n 3\tline3\n', |
| 131 | + 'total_lines': 3, |
| 132 | + } |
| 133 | + |
| 134 | + @pytest.mark.asyncio |
| 135 | + async def test_read_file_with_line_range_missing_file_returns_error( |
| 136 | + self, env: LocalEnvironment |
| 137 | + ): |
| 138 | + """Returns a missing-file error for ranged reads.""" |
| 139 | + tool = ReadFileTool(env) |
| 140 | + |
| 141 | + result = await tool.run_async( |
| 142 | + args={'path': 'missing.txt', 'start_line': 2}, |
| 143 | + tool_context=None, |
| 144 | + ) |
| 145 | + |
| 146 | + assert result == { |
| 147 | + 'status': 'error', |
| 148 | + 'error': 'File not found: missing.txt', |
| 149 | + } |
| 150 | + |
| 151 | + @pytest.mark.asyncio |
| 152 | + async def test_read_file_rejects_non_integer_end_line( |
| 153 | + self, env: LocalEnvironment |
| 154 | + ): |
| 155 | + """Rejects non-integer line numbers without executing shell syntax.""" |
| 156 | + await env.write_file('sample.txt', 'line1\nline2\n') |
| 157 | + marker = env.working_dir / 'marker.txt' |
| 158 | + injected_end_line = f"1'; touch {marker}; echo '" |
| 159 | + |
| 160 | + tool = ReadFileTool(env) |
| 161 | + result = await tool.run_async( |
| 162 | + args={'path': 'sample.txt', 'end_line': injected_end_line}, |
| 163 | + tool_context=None, |
| 164 | + ) |
| 165 | + |
| 166 | + assert result == { |
| 167 | + 'status': 'error', |
| 168 | + 'error': '`end_line` must be an integer if provided.', |
| 169 | + } |
| 170 | + assert not marker.exists() |
| 171 | + |
| 172 | + @pytest.mark.asyncio |
| 173 | + async def test_read_file_rejects_boolean_line_numbers( |
| 174 | + self, env: LocalEnvironment |
| 175 | + ): |
| 176 | + """Rejects boolean values for start_line and end_line.""" |
| 177 | + await env.write_file('sample.txt', 'line1\nline2\n') |
| 178 | + |
| 179 | + tool = ReadFileTool(env) |
| 180 | + res_start = await tool.run_async( |
| 181 | + args={'path': 'sample.txt', 'start_line': True}, |
| 182 | + tool_context=None, |
| 183 | + ) |
| 184 | + res_end = await tool.run_async( |
| 185 | + args={'path': 'sample.txt', 'end_line': False}, |
| 186 | + tool_context=None, |
| 187 | + ) |
| 188 | + |
| 189 | + assert res_start == { |
| 190 | + 'status': 'error', |
| 191 | + 'error': '`start_line` must be an integer if provided.', |
| 192 | + } |
| 193 | + assert res_end == { |
| 194 | + 'status': 'error', |
| 195 | + 'error': '`end_line` must be an integer if provided.', |
| 196 | + } |
0 commit comments