|
| 1 | +"""Tests for Rich encoding fix to handle surrogate characters.""" |
| 2 | + |
| 3 | +from io import StringIO |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +from cycode.cli import consts |
| 9 | +from cycode.cli.models import Document |
| 10 | +from cycode.cli.printers.rich_printer import RichPrinter |
| 11 | +from cycode.cyclient.models import Detection |
| 12 | + |
| 13 | + |
| 14 | +def create_strict_encoding_console() -> tuple[Console, StringIO]: |
| 15 | + """Create a Console that enforces strict UTF-8 encoding, simulating Windows console behavior. |
| 16 | + |
| 17 | + When Rich writes to the console, the file object needs to encode strings to bytes. |
| 18 | + With errors='strict' (default for TextIOWrapper), this raises UnicodeEncodeError on surrogates. |
| 19 | + This function simulates that behavior to test the encoding fix. |
| 20 | + """ |
| 21 | + buffer = StringIO() |
| 22 | + |
| 23 | + class StrictEncodingWrapper: |
| 24 | + def __init__(self, file_obj: StringIO) -> None: |
| 25 | + self._file = file_obj |
| 26 | + |
| 27 | + def write(self, text: str) -> int: |
| 28 | + """Validate encoding before writing to simulate strict encoding behavior.""" |
| 29 | + text.encode('utf-8') |
| 30 | + return self._file.write(text) |
| 31 | + |
| 32 | + def flush(self) -> None: |
| 33 | + self._file.flush() |
| 34 | + |
| 35 | + def isatty(self) -> bool: |
| 36 | + return False |
| 37 | + |
| 38 | + def __getattr__(self, name: str): |
| 39 | + # Delegate all other attributes to the underlying file |
| 40 | + return getattr(self._file, name) |
| 41 | + |
| 42 | + strict_file = StrictEncodingWrapper(buffer) |
| 43 | + console = Console(file=strict_file, width=80, force_terminal=False) |
| 44 | + return console, buffer |
| 45 | + |
| 46 | + |
| 47 | +def test_rich_printer_handles_surrogate_characters_in_violation_card() -> None: |
| 48 | + """Test that RichPrinter._print_violation_card() handles surrogate characters without errors. |
| 49 | + |
| 50 | + The error occurs in Rich's console._write_buffer() -> write() when console.print() is called. |
| 51 | + On Windows with strict encoding, this raises UnicodeEncodeError on surrogates. |
| 52 | + """ |
| 53 | + surrogate_char = chr(0xDC96) |
| 54 | + document_content = 'A' * 1236 + surrogate_char + 'B' * 100 |
| 55 | + document = Document( |
| 56 | + path='test.py', |
| 57 | + content=document_content, |
| 58 | + is_git_diff_format=False, |
| 59 | + ) |
| 60 | + |
| 61 | + detection = Detection( |
| 62 | + detection_type_id='test-id', |
| 63 | + type='test-type', |
| 64 | + message='Test message', |
| 65 | + detection_details={ |
| 66 | + 'description': 'Summary with ' + surrogate_char + ' surrogate character', |
| 67 | + 'policy_display_name': 'Test Policy', |
| 68 | + 'start_position': 1236, |
| 69 | + 'length': 1, |
| 70 | + 'line': 0, |
| 71 | + }, |
| 72 | + detection_rule_id='test-rule-id', |
| 73 | + severity='Medium', |
| 74 | + ) |
| 75 | + |
| 76 | + mock_ctx = MagicMock() |
| 77 | + mock_ctx.obj = { |
| 78 | + 'scan_type': consts.SAST_SCAN_TYPE, |
| 79 | + 'show_secret': False, |
| 80 | + } |
| 81 | + mock_ctx.info_name = consts.SAST_SCAN_TYPE |
| 82 | + |
| 83 | + console, _ = create_strict_encoding_console() |
| 84 | + printer = RichPrinter(mock_ctx, console, console) |
| 85 | + printer._print_violation_card(document, detection, 1, 1) |
0 commit comments