|
| 1 | +import subprocess |
1 | 2 | from unittest.mock import Mock, patch
|
2 | 3 |
|
3 |
| -from pydistcheck._shared_lib_utils import _MACHO_STRIP_SYMBOL, _get_symbols |
| 4 | +from pydistcheck._shared_lib_utils import ( |
| 5 | + _MACHO_STRIP_SYMBOL, |
| 6 | + _get_symbols, |
| 7 | + _run_command, |
| 8 | +) |
| 9 | + |
| 10 | + |
| 11 | +def test_run_command_handles_binary_output(): |
| 12 | + """Test that _run_command can handle binary output containing invalid UTF-8 bytes.""" |
| 13 | + mock_output = bytes( |
| 14 | + [0x68, 0x65, 0x6C, 0x6C, 0x6F, 0xD7, 0x77, 0x6F, 0x72, 0x6C, 0x64] |
| 15 | + ) # i.e., "hello" + invalid UTF-8 + "world" |
| 16 | + |
| 17 | + with patch("subprocess.run") as mock_run: |
| 18 | + mock_run.return_value = Mock(stdout=mock_output) |
| 19 | + |
| 20 | + # Should not raise UnicodeDecodeError, |
| 21 | + result = _run_command(["some", "command"]) |
| 22 | + # latin1 encoding should preserve all bytes. |
| 23 | + assert len(result) == len(mock_output) |
| 24 | + # Check valid parts of the string. |
| 25 | + assert "hello" in result |
| 26 | + assert "world" in result |
| 27 | + |
| 28 | + |
| 29 | +def test_run_command_handles_command_failure(): |
| 30 | + with patch("subprocess.run") as mock_run: |
| 31 | + mock_run.side_effect = subprocess.CalledProcessError(1, ["cmd"]) |
| 32 | + result = _run_command(["failing", "command"]) |
| 33 | + assert result == "__command_failed__" |
| 34 | + |
| 35 | + |
| 36 | +def test_run_command_handles_missing_command(): |
| 37 | + with patch("subprocess.run") as mock_run: |
| 38 | + mock_run.side_effect = FileNotFoundError() |
| 39 | + result = _run_command(["nonexistent"]) |
| 40 | + assert result == "__tool_not_available__" |
4 | 41 |
|
5 | 42 |
|
6 | 43 | def test_get_symbols_filters_radr_symbol():
|
|
0 commit comments