HEX
Server: Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.4.25
System: Windows NT DESKTOP-4TAV2RJ 10.0 build 19045 (Windows 10) AMD64
User: fred (0)
PHP: 8.4.25
Disabled: NONE
Upload Files
File: C:/Users/fred/anaconda3/Lib/site-packages/traitlets/tests/utils.py
from __future__ import annotations

import sys
from subprocess import PIPE, Popen
from typing import Any, Sequence


def get_output_error_code(cmd: str | Sequence[str]) -> tuple[str, str, Any]:
    """Get stdout, stderr, and exit code from running a command"""
    p = Popen(cmd, stdout=PIPE, stderr=PIPE)  # noqa: S603
    out, err = p.communicate()
    out_str = out.decode("utf8", "replace")
    err_str = err.decode("utf8", "replace")
    return out_str, err_str, p.returncode


def check_help_output(pkg: str, subcommand: Sequence[str] | None = None) -> tuple[str, str]:
    """test that `python -m PKG [subcommand] -h` works"""
    cmd = [sys.executable, "-m", pkg]
    if subcommand:
        cmd.extend(subcommand)
    cmd.append("-h")
    out, err, rc = get_output_error_code(cmd)
    assert rc == 0, err
    assert "Traceback" not in err
    assert "Options" in out
    assert "--help-all" in out
    return out, err


def check_help_all_output(pkg: str, subcommand: Sequence[str] | None = None) -> tuple[str, str]:
    """test that `python -m PKG --help-all` works"""
    cmd = [sys.executable, "-m", pkg]
    if subcommand:
        cmd.extend(subcommand)
    cmd.append("--help-all")
    out, err, rc = get_output_error_code(cmd)
    assert rc == 0, err
    assert "Traceback" not in err
    assert "Options" in out
    assert "Class options" in out
    return out, err