Test the run_command_safely function.
| 256 | |
| 257 | |
| 258 | class TestRunCommandSafely: |
| 259 | """Test the run_command_safely function.""" |
| 260 | |
| 261 | @pytest.fixture |
| 262 | def mock_console(self): |
| 263 | """Fixture providing a mock console.""" |
| 264 | console = Mock() |
| 265 | console.write = Mock() |
| 266 | console.read = Mock() |
| 267 | return console |
| 268 | |
| 269 | @pytest.mark.asyncio |
| 270 | async def test_run_command_safely_basic(self, mock_console): |
| 271 | """Test basic command execution.""" |
| 272 | # Mock console read to return prompt immediately |
| 273 | mock_console.read.return_value = { |
| 274 | 'data': 'command output\n', |
| 275 | 'prompt': '\x01\x02msf6\x01\x02 \x01\x02> \x01\x02', |
| 276 | 'busy': False |
| 277 | } |
| 278 | |
| 279 | result = await run_command_safely(mock_console, 'help') |
| 280 | |
| 281 | mock_console.write.assert_called_once_with('help\n') |
| 282 | assert 'command output' in result |
| 283 | |
| 284 | @pytest.mark.asyncio |
| 285 | async def test_run_command_safely_invalid_console(self, mock_console): |
| 286 | """Test command execution with invalid console.""" |
| 287 | # Remove required methods |
| 288 | delattr(mock_console, 'write') |
| 289 | |
| 290 | with pytest.raises(TypeError, match="Unsupported console object"): |
| 291 | await run_command_safely(mock_console, 'help') |
| 292 | |
| 293 | @pytest.mark.asyncio |
| 294 | async def test_run_command_safely_read_error(self, mock_console): |
| 295 | """Test command execution with read error - should timeout gracefully.""" |
| 296 | mock_console.read.side_effect = Exception("Read failed") |
| 297 | |
| 298 | # Should not raise exception, but timeout and return empty result |
| 299 | result = await run_command_safely(mock_console, 'help') |
| 300 | |
| 301 | # Should return empty string after timeout |
| 302 | assert isinstance(result, str) |
| 303 | assert result == "" # Empty result after timeout |
| 304 | |
| 305 | |
| 306 | class TestFindAvailablePort: |
nothing calls this directly
no outgoing calls
no test coverage detected