# Path: dev_tools/shell_tools.py
# BOLD = 1
# DIM = 2
# RED = 31
# GREEN = 32
# YELLOW = 33
# def highlight(text: str, color_code: int, bold: bool=False) -> str:
#     def __init__(self, out_pipe: Optional[IO[str]] = None) -> None:
# async def _async_forward(async_chunks: collections.AsyncIterable,
#                          out: Optional[Union[TeeCapture, IO[str]]]
#                          ) -> Optional[str]:
# async def _async_wait_for_process(
#         future_process: Any,
#         out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#         err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr
# ) -> CommandOutput:
# def abbreviate_command_arguments_after_switches(
#         cmd: Tuple[str, ...]) -> Tuple[str, ...]:
# def run_cmd(*cmd: Optional[str],
#             out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#             err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr,
#             raise_on_fail: bool = True,
#             log_run_to_stderr: bool = True,
#             abbreviate_non_option_arguments: bool = False,
#             **kwargs
#             ) -> CommandOutput:
# def run_shell(cmd: str,
#               out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#               err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr,
#               raise_on_fail: bool = True,
#               log_run_to_stderr: bool = True,
#               **kwargs
#               ) -> CommandOutput:
# def output_of(*cmd: Optional[str], **kwargs) -> str:
# class TeeCapture:


import subprocess
import os
import sys
import pytest
from dev_tools import shell_tools

@only_in_python3_on_posix
def test_run_shell_capture():
    assert run_shell(
        'echo test 1>&2',
        err=None) == (None, None, 0)
    assert run_shell(
        'echo test 1>&2',
        err=shell_tools.TeeCapture()) == (None, 'test\n', 0)
    assert run_shell(
        'echo test 1>&2',
        err=None,
        out=shell_tools.TeeCapture()) == ('', None, 0)


@only_in_python3_on_posix
def test_run_shell_does_not_deadlock_on_large_outputs():
    assert run_shell(
        r"""python3 -c "import sys;"""
        r"""print((('o' * 99) + '\n') * 10000);"""
        r"""print((('e' * 99) + '\n') * 10000, file=sys.stderr)"""
        '"',
        out=None,
        err=None) == (None, None, 0)


@only_in_python3_on_posix
def test_output_of():
    assert shell_tools.output_of('true') == ''
    with pytest.raises(subprocess.CalledProcessError):

# Path: dev_tools/shell_tools.py
# BOLD = 1
# DIM = 2
# RED = 31
# GREEN = 32
# YELLOW = 33
# def highlight(text: str, color_code: int, bold: bool=False) -> str:
#     def __init__(self, out_pipe: Optional[IO[str]] = None) -> None:
# async def _async_forward(async_chunks: collections.AsyncIterable,
#                          out: Optional[Union[TeeCapture, IO[str]]]
#                          ) -> Optional[str]:
# async def _async_wait_for_process(
#         future_process: Any,
#         out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#         err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr
# ) -> CommandOutput:
# def abbreviate_command_arguments_after_switches(
#         cmd: Tuple[str, ...]) -> Tuple[str, ...]:
# def run_cmd(*cmd: Optional[str],
#             out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#             err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr,
#             raise_on_fail: bool = True,
#             log_run_to_stderr: bool = True,
#             abbreviate_non_option_arguments: bool = False,
#             **kwargs
#             ) -> CommandOutput:
# def run_shell(cmd: str,
#               out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
#               err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr,
#               raise_on_fail: bool = True,
#               log_run_to_stderr: bool = True,
#               **kwargs
#               ) -> CommandOutput:
# def output_of(*cmd: Optional[str], **kwargs) -> str:
# class TeeCapture:


# Path: dev_tools/shell_tools_test.py
import subprocess
import os
import sys
import pytest
from dev_tools import shell_tools


@only_in_python3_on_posix
def test_run_shell_capture():
    assert run_shell(
        'echo test 1>&2',
        err=None) == (None, None, 0)
    assert run_shell(
        'echo test 1>&2',
        err=shell_tools.TeeCapture()) == (None, 'test\n', 0)
    assert run_shell(
        'echo test 1>&2',
        err=None,
        out=shell_tools.TeeCapture()) == ('', None, 0)


@only_in_python3_on_posix
def test_run_shell_does_not_deadlock_on_large_outputs():
    assert run_shell(
        r"""python3 -c "import sys;"""
        r"""print((('o' * 99) + '\n') * 10000);"""
        r"""print((('e' * 99) + '\n') * 10000, file=sys.stderr)"""
        '"',
        out=None,
        err=None) == (None, None, 0)


@only_in_python3_on_posix
def test_output_of():
    assert shell_tools.output_of('true') == ''
    with pytest.raises(subprocess.CalledProcessError):

        _ = shell_tools.output_of('false')