aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_kittygfx.py
blob: 74b6bb7a4a56afa12691103ee1a0adbd5ab7b7ec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
"""Cross-platform checks for the optional kitty-graphics startup splash.

Pure: stdlib only, no terminal, Textual, Code Mode, or IDA.
"""
from __future__ import annotations

import builtins
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

NEEDS_IDA = False

from idatui import kittygfx  # noqa: E402

PASS = FAIL = 0


def check(name, ok, detail=""):
    global PASS, FAIL
    if ok:
        PASS += 1
        print(f"  ok   {name}")
    else:
        FAIL += 1
        print(f"  FAIL {name}   {detail}")


def t_no_termios_falls_back():
    """Native Windows has no termios; the splash must simply use ANSI art."""
    original_import = builtins.__import__

    def without_termios(name, *args, **kwargs):
        if name == "termios":
            raise ModuleNotFoundError("No module named 'termios'")
        return original_import(name, *args, **kwargs)

    builtins.__import__ = without_termios
    try:
        check("missing termios disables graphics", kittygfx._query_tty(0) is False)
    except Exception as exc:  # the original Windows startup crash
        check("missing termios does not escape", False,
              f"{type(exc).__name__}: {exc}")
    finally:
        builtins.__import__ = original_import


def t_probe_failure_is_never_fatal():
    """Even an unexpected platform/probe error cannot prevent TUI startup."""
    original_query = kittygfx._query_tty
    original_stdout = sys.__stdout__
    original_supported = kittygfx._supported
    old_env = os.environ.pop("IDATUI_KITTY", None)

    class Tty:
        def isatty(self):
            return True

    def broken_query():
        raise RuntimeError("terminal API failed")

    try:
        sys.__stdout__ = Tty()
        kittygfx._query_tty = broken_query
        kittygfx._supported = None
        check("probe exception disables graphics", kittygfx.supported() is False)
        check("failed result is cached", kittygfx.supported() is False)
    except Exception as exc:
        check("probe exception does not escape", False,
              f"{type(exc).__name__}: {exc}")
    finally:
        kittygfx._query_tty = original_query
        kittygfx._supported = original_supported
        sys.__stdout__ = original_stdout
        if old_env is not None:
            os.environ["IDATUI_KITTY"] = old_env


def main() -> int:
    for fn in (t_no_termios_falls_back, t_probe_failure_is_never_fatal):
        print(f"\n{fn.__name__}")
        fn()
    print(f"\n{PASS} passed, {FAIL} failed")
    return 1 if FAIL else 0


if __name__ == "__main__":
    raise SystemExit(main())