|
4 | 4 | Tests the Click CLI interface including the pipe command. |
5 | 5 | """ |
6 | 6 |
|
7 | | -from unittest.mock import patch |
| 7 | +from unittest.mock import AsyncMock, patch |
8 | 8 |
|
9 | 9 | import pytest |
| 10 | +from anyio import BrokenResourceError, EndOfStream |
10 | 11 | from click.testing import CliRunner |
11 | 12 |
|
12 | 13 | from .driver import PySerial |
13 | 14 | from jumpstarter.common.utils import serve |
14 | 15 |
|
15 | 16 |
|
| 17 | +@pytest.fixture |
| 18 | +def anyio_backend(): |
| 19 | + return "asyncio" |
| 20 | + |
| 21 | + |
16 | 22 | @pytest.fixture |
17 | 23 | def pyserial_client(): |
18 | 24 | """Fixture to create a PySerial client with loop:// URL for testing.""" |
@@ -241,3 +247,101 @@ def test_cli_base_command(pyserial_client): |
241 | 247 | assert result.exit_code == 0 |
242 | 248 | assert "Serial port client" in result.output or "Commands:" in result.output |
243 | 249 |
|
| 250 | + |
| 251 | +@pytest.mark.anyio |
| 252 | +async def test_serial_to_output_handles_end_of_stream(pyserial_client): |
| 253 | + """Test that _serial_to_output handles EndOfStream gracefully.""" |
| 254 | + # Create a mock stream that raises EndOfStream |
| 255 | + mock_stream = AsyncMock() |
| 256 | + mock_stream.receive.side_effect = EndOfStream |
| 257 | + |
| 258 | + # Capture the click output |
| 259 | + from click.testing import CliRunner |
| 260 | + |
| 261 | + runner = CliRunner() |
| 262 | + with runner.isolated_filesystem(): |
| 263 | + # Call _serial_to_output directly |
| 264 | + await pyserial_client._serial_to_output(mock_stream, None, False) |
| 265 | + |
| 266 | + # The method should have caught EndOfStream and not raised it |
| 267 | + # (we're testing that it doesn't propagate) |
| 268 | + |
| 269 | + |
| 270 | +@pytest.mark.anyio |
| 271 | +async def test_serial_to_output_handles_broken_resource(pyserial_client): |
| 272 | + """Test that _serial_to_output handles BrokenResourceError gracefully.""" |
| 273 | + # Create a mock stream that raises BrokenResourceError |
| 274 | + mock_stream = AsyncMock() |
| 275 | + mock_stream.receive.side_effect = BrokenResourceError |
| 276 | + |
| 277 | + # Capture the click output |
| 278 | + from click.testing import CliRunner |
| 279 | + |
| 280 | + runner = CliRunner() |
| 281 | + with runner.isolated_filesystem(): |
| 282 | + # Call _serial_to_output directly |
| 283 | + await pyserial_client._serial_to_output(mock_stream, None, False) |
| 284 | + |
| 285 | + # The method should have caught BrokenResourceError and not raised it |
| 286 | + # (we're testing that it doesn't propagate) |
| 287 | + |
| 288 | + |
| 289 | +@pytest.mark.anyio |
| 290 | +async def test_serial_to_output_end_of_stream_with_file(pyserial_client): |
| 291 | + """Test that _serial_to_output handles EndOfStream when writing to file.""" |
| 292 | + # Create a mock stream that raises EndOfStream |
| 293 | + mock_stream = AsyncMock() |
| 294 | + mock_stream.receive.side_effect = EndOfStream |
| 295 | + |
| 296 | + from click.testing import CliRunner |
| 297 | + |
| 298 | + runner = CliRunner() |
| 299 | + with runner.isolated_filesystem(): |
| 300 | + # Call _serial_to_output with a file output |
| 301 | + await pyserial_client._serial_to_output(mock_stream, "test.log", False) |
| 302 | + |
| 303 | + # The file should have been created (even if empty) |
| 304 | + import os |
| 305 | + |
| 306 | + assert os.path.exists("test.log") |
| 307 | + |
| 308 | + |
| 309 | +@pytest.mark.anyio |
| 310 | +async def test_serial_to_output_broken_resource_with_file(pyserial_client): |
| 311 | + """Test that _serial_to_output handles BrokenResourceError when writing to file.""" |
| 312 | + # Create a mock stream that raises BrokenResourceError |
| 313 | + mock_stream = AsyncMock() |
| 314 | + mock_stream.receive.side_effect = BrokenResourceError |
| 315 | + |
| 316 | + from click.testing import CliRunner |
| 317 | + |
| 318 | + runner = CliRunner() |
| 319 | + with runner.isolated_filesystem(): |
| 320 | + # Call _serial_to_output with a file output |
| 321 | + await pyserial_client._serial_to_output(mock_stream, "test.log", False) |
| 322 | + |
| 323 | + # The file should have been created (even if empty) |
| 324 | + import os |
| 325 | + |
| 326 | + assert os.path.exists("test.log") |
| 327 | + |
| 328 | + |
| 329 | +@pytest.mark.anyio |
| 330 | +async def test_serial_to_output_receives_data_then_end_of_stream(pyserial_client): |
| 331 | + """Test that _serial_to_output successfully receives data before EndOfStream.""" |
| 332 | + # Create a mock stream that returns data then raises EndOfStream |
| 333 | + mock_stream = AsyncMock() |
| 334 | + mock_stream.receive.side_effect = [b"Hello", b"World", EndOfStream] |
| 335 | + |
| 336 | + from click.testing import CliRunner |
| 337 | + |
| 338 | + runner = CliRunner() |
| 339 | + with runner.isolated_filesystem(): |
| 340 | + # Call _serial_to_output with a file output |
| 341 | + await pyserial_client._serial_to_output(mock_stream, "test.log", False) |
| 342 | + |
| 343 | + # Verify the file contains the data |
| 344 | + with open("test.log", "rb") as f: |
| 345 | + content = f.read() |
| 346 | + assert content == b"HelloWorld" |
| 347 | + |
0 commit comments