<commit_before>"""Tests for rich module."""
import sys

from subprocess_tee import run
from subprocess_tee.rich import ConsoleEx


def test_rich_console_ex() -> None:
    """Validate that ConsoleEx can capture output from print() calls."""
    console = ConsoleEx(record=True, redirect=True)
    console.print("alpha")
    print("beta")
    sys.stdout.write("gamma\n")
    sys.stderr.write("delta\n")
    proc = run("echo 123")
    assert proc.stdout == "123\n"
    text = console.export_text()
    assert text == "alpha\nbeta\ngamma\ndelta\n123\n"
<commit_msg>Add testing of rich html_export<commit_after>"""Tests for rich module."""
import sys

from subprocess_tee import run
from subprocess_tee.rich import ConsoleEx


def test_rich_console_ex() -> None:
    """Validate that ConsoleEx can capture output from print() calls."""
    console = ConsoleEx(record=True, redirect=True)
    console.print("alpha")
    print("beta")
    sys.stdout.write("gamma\n")
    sys.stderr.write("delta\n")
    proc = run("echo 123")
    assert proc.stdout == "123\n"
    text = console.export_text()
    assert text == "alpha\nbeta\ngamma\ndelta\n123\n"


def test_rich_console_ex_ansi() -> None:
    """Validate that ANSI sent to sys.stdout does not become garbage in record."""
    print()
    console = ConsoleEx(force_terminal=True, record=True, redirect=True)
    console.print("[green]this from Console.print()[/green]", style="red")
    proc = run(r'echo -e "\033[31mred\033[0m"')
    assert proc.returncode == 0
    assert proc.stdout == "\x1b[31mred\x1b[0m\n"

    # validate that what rich recorded is the same as what the subprocess produced
    text = console.export_text(clear=False)
    assert "\x1b[31mred\x1b[0m\n" in text

    # validate that html export also contains at least the "red" text
    html = console.export_html(clear=False)
    assert "red" in html

{'fraction_non_alphanumeric': 0.13083109919571045, 'fraction_numerical': 0.02037533512064343, 'mean_word_length': 3.8720626631853787, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4994651', 'n_tokens_mistral': 613, 'n_tokens_neox': 588, 'n_words': 162}