* tests: fully type the test suite * chore: require more typing Signed-off-by: Henry Schreiner <henryfs@princeton.edu> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Henry Schreiner <henryfs@princeton.edu> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
import os
|
|
import re
|
|
import subprocess
|
|
import sysconfig
|
|
from typing import Any
|
|
|
|
import rich.console
|
|
import rich.text
|
|
|
|
|
|
def define_env(env: Any) -> None: # noqa: ANN401
|
|
"Hook function for mkdocs-macros"
|
|
|
|
@env.macro # type: ignore[untyped-decorator]
|
|
def subprocess_run(*args: str) -> str:
|
|
"Run a subprocess and return the stdout"
|
|
env = os.environ.copy()
|
|
scripts = sysconfig.get_path("scripts")
|
|
env.pop("NO_COLOR", None)
|
|
env["PATH"] = f"{scripts}{os.pathsep}{env.get('PATH', '')}"
|
|
env["PYTHON_COLORS"] = "1"
|
|
output = subprocess.run(args, check=True, capture_output=True, text=True, env=env).stdout
|
|
rich_text = rich.text.Text.from_ansi(output)
|
|
console = rich.console.Console(record=True, force_terminal=True)
|
|
console.print(rich_text)
|
|
page = console.export_html(inline_styles=True)
|
|
result = re.search(r"<body.*?>(.*?)</body>", page, re.DOTALL | re.IGNORECASE)
|
|
assert result
|
|
txt = result.group(1).strip()
|
|
return txt.replace("code ", 'code class="nohighlight" ')
|