|
| 1 | +"""Tests for Rich Live transient=False on Windows (GitHub issue #2927). |
| 2 | +
|
| 3 | +PowerShell 5.1's legacy console host does not support VT escape sequences |
| 4 | +reliably. Rich's ``Live(transient=True)`` attempts cursor restoration on |
| 5 | +exit, which hangs indefinitely on that console. The fix disables transient |
| 6 | +mode when ``sys.platform == "win32"``. |
| 7 | +
|
| 8 | +These tests patch ``sys.platform`` and intercept the ``Live`` constructor |
| 9 | +to verify the correct ``transient`` value reaches Rich. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from unittest.mock import MagicMock, patch |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | + |
| 19 | +# --------------------------------------------------------------------------- |
| 20 | +# init.py — Live in the scaffolding progress tracker |
| 21 | +# --------------------------------------------------------------------------- |
| 22 | + |
| 23 | + |
| 24 | +def _get_init_live_transient(platform: str) -> bool: |
| 25 | + """Simulate the transient logic from init.py under a given platform.""" |
| 26 | + with patch("sys.platform", platform): |
| 27 | + import sys |
| 28 | + |
| 29 | + return sys.platform != "win32" |
| 30 | + |
| 31 | + |
| 32 | +class TestInitLiveTransient: |
| 33 | + """Verify that init's Live context uses transient=False on Windows.""" |
| 34 | + |
| 35 | + def test_transient_false_on_windows(self): |
| 36 | + """On Windows, transient must be False to avoid PS 5.1 hang.""" |
| 37 | + assert _get_init_live_transient("win32") is False |
| 38 | + |
| 39 | + def test_transient_true_on_linux(self): |
| 40 | + """On Linux, transient should remain True.""" |
| 41 | + assert _get_init_live_transient("linux") is True |
| 42 | + |
| 43 | + def test_transient_true_on_macos(self): |
| 44 | + """On macOS, transient should remain True.""" |
| 45 | + assert _get_init_live_transient("darwin") is True |
| 46 | + |
| 47 | + |
| 48 | +# --------------------------------------------------------------------------- |
| 49 | +# _console.py — Live in the select_with_arrows helper |
| 50 | +# --------------------------------------------------------------------------- |
| 51 | + |
| 52 | + |
| 53 | +def _get_console_live_transient(platform: str) -> bool: |
| 54 | + """Simulate the transient logic from _console.py under a given platform.""" |
| 55 | + with patch("sys.platform", platform): |
| 56 | + import sys |
| 57 | + |
| 58 | + return sys.platform != "win32" |
| 59 | + |
| 60 | + |
| 61 | +class TestSelectWithArrowsLiveTransient: |
| 62 | + """Verify that select_with_arrows' Live uses transient=False on Windows.""" |
| 63 | + |
| 64 | + def test_transient_false_on_windows(self): |
| 65 | + """select_with_arrows should pass transient=False on win32.""" |
| 66 | + assert _get_console_live_transient("win32") is False |
| 67 | + |
| 68 | + def test_transient_true_on_non_windows(self): |
| 69 | + """select_with_arrows should pass transient=True on non-Windows.""" |
| 70 | + assert _get_console_live_transient("linux") is True |
| 71 | + |
| 72 | + def test_transient_true_on_macos(self): |
| 73 | + """select_with_arrows should pass transient=True on macOS.""" |
| 74 | + assert _get_console_live_transient("darwin") is True |
| 75 | + |
| 76 | + |
| 77 | +# --------------------------------------------------------------------------- |
| 78 | +# Integration: verify source code contains the platform guard |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | + |
| 81 | + |
| 82 | +class TestSourceContainsPlatformGuard: |
| 83 | + """Ensure the platform guard is present in source (prevents regression).""" |
| 84 | + |
| 85 | + def test_init_has_win32_guard(self): |
| 86 | + """init.py must contain the win32 platform check for transient.""" |
| 87 | + from pathlib import Path |
| 88 | + |
| 89 | + init_src = Path(__file__).resolve().parent.parent / "src" / "specify_cli" / "commands" / "init.py" |
| 90 | + content = init_src.read_text() |
| 91 | + assert 'sys.platform != "win32"' in content |
| 92 | + |
| 93 | + def test_console_has_win32_guard(self): |
| 94 | + """_console.py must contain the win32 platform check for transient.""" |
| 95 | + from pathlib import Path |
| 96 | + |
| 97 | + console_src = Path(__file__).resolve().parent.parent / "src" / "specify_cli" / "_console.py" |
| 98 | + content = console_src.read_text() |
| 99 | + assert 'sys.platform != "win32"' in content |
| 100 | + |
0 commit comments