# Path: pre_commit/parse_shebang.py
# class ExecutableNotFoundError(OSError):
#     def to_output(self) -> tuple[int, bytes, None]:
# def parse_filename(filename: str) -> tuple[str, ...]:
# def find_executable(
#         exe: str, _environ: Mapping[str, str] | None = None,
# ) -> str | None:
# def normexe(orig: str) -> str:
#     def _error(msg: str) -> NoReturn:
# def normalize_cmd(cmd: tuple[str, ...]) -> tuple[str, ...]:
# 
# Path: pre_commit/xargs.py
# def xargs(
#         cmd: tuple[str, ...],
#         varargs: Sequence[str],
#         *,
#         color: bool = False,
#         target_concurrency: int = 1,
#         _max_length: int = _get_platform_max_length(),
#         **kwargs: Any,
# ) -> tuple[int, bytes]:
#     """A simplified implementation of xargs.
# 
#     color: Make a pty if on a platform that supports it
#     target_concurrency: Target number of partitions to run concurrently
#     """
#     cmd_fn = cmd_output_p if color else cmd_output_b
#     retcode = 0
#     stdout = b''
# 
#     try:
#         cmd = parse_shebang.normalize_cmd(cmd)
#     except parse_shebang.ExecutableNotFoundError as e:
#         return e.to_output()[:2]
# 
#     # on windows, batch files have a separate length limit than windows itself
#     if (
#             sys.platform == 'win32' and
#             cmd[0].lower().endswith(('.bat', '.cmd'))
#     ):  # pragma: win32 cover
#         # this is implementation details but the command gets translated into
#         # full/path/to/cmd.exe /c *cmd
#         cmd_exe = parse_shebang.find_executable('cmd.exe')
#         # 1024 is additionally subtracted to give headroom for further
#         # expansion inside the batch file
#         _max_length = 8192 - len(cmd_exe) - len(' /c ') - 1024
# 
#     partitions = partition(cmd, varargs, target_concurrency, _max_length)
# 
#     def run_cmd_partition(
#             run_cmd: tuple[str, ...],
#     ) -> tuple[int, bytes, bytes | None]:
#         return cmd_fn(
#             *run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
#         )
# 
#     threads = min(len(partitions), target_concurrency)
#     with _thread_mapper(threads) as thread_map:
#         results = thread_map(run_cmd_partition, partitions)
# 
#         for proc_retcode, proc_out, _ in results:
#             retcode = max(retcode, proc_retcode)
#             stdout += proc_out
# 
#     return retcode, stdout


import concurrent.futures
import os
import sys
import time
import pytest
from unittest import mock
from pre_commit import parse_shebang
from pre_commit import xargs
        ('foo',) + ('A',) * 6,
        ('foo',) + ('A',) * 6,
        ('foo',) + ('A',) * 4,
    )


def test_partition_target_concurrency_wont_make_tiny_partitions():
    ret = xargs.partition(
        ('foo',), ('A',) * 10,
        4,
        _max_length=50,
    )
    assert ret == (
        ('foo',) + ('A',) * 4,
        ('foo',) + ('A',) * 4,
        ('foo',) + ('A',) * 2,
    )


def test_argument_too_long():
    with pytest.raises(xargs.ArgumentTooLongError):
        xargs.partition(('a' * 5,), ('a' * 5,), 1, _max_length=10)


def test_xargs_smoke():
    ret, out = xargs.xargs(('echo',), ('hello', 'world'))
    assert ret == 0
    assert out.replace(b'\r\n', b'\n') == b'hello world\n'



# Path: pre_commit/parse_shebang.py
# class ExecutableNotFoundError(OSError):
#     def to_output(self) -> tuple[int, bytes, None]:
# def parse_filename(filename: str) -> tuple[str, ...]:
# def find_executable(
#         exe: str, _environ: Mapping[str, str] | None = None,
# ) -> str | None:
# def normexe(orig: str) -> str:
#     def _error(msg: str) -> NoReturn:
# def normalize_cmd(cmd: tuple[str, ...]) -> tuple[str, ...]:
# 
# Path: pre_commit/xargs.py
# def xargs(
#         cmd: tuple[str, ...],
#         varargs: Sequence[str],
#         *,
#         color: bool = False,
#         target_concurrency: int = 1,
#         _max_length: int = _get_platform_max_length(),
#         **kwargs: Any,
# ) -> tuple[int, bytes]:
#     """A simplified implementation of xargs.
# 
#     color: Make a pty if on a platform that supports it
#     target_concurrency: Target number of partitions to run concurrently
#     """
#     cmd_fn = cmd_output_p if color else cmd_output_b
#     retcode = 0
#     stdout = b''
# 
#     try:
#         cmd = parse_shebang.normalize_cmd(cmd)
#     except parse_shebang.ExecutableNotFoundError as e:
#         return e.to_output()[:2]
# 
#     # on windows, batch files have a separate length limit than windows itself
#     if (
#             sys.platform == 'win32' and
#             cmd[0].lower().endswith(('.bat', '.cmd'))
#     ):  # pragma: win32 cover
#         # this is implementation details but the command gets translated into
#         # full/path/to/cmd.exe /c *cmd
#         cmd_exe = parse_shebang.find_executable('cmd.exe')
#         # 1024 is additionally subtracted to give headroom for further
#         # expansion inside the batch file
#         _max_length = 8192 - len(cmd_exe) - len(' /c ') - 1024
# 
#     partitions = partition(cmd, varargs, target_concurrency, _max_length)
# 
#     def run_cmd_partition(
#             run_cmd: tuple[str, ...],
#     ) -> tuple[int, bytes, bytes | None]:
#         return cmd_fn(
#             *run_cmd, retcode=None, stderr=subprocess.STDOUT, **kwargs,
#         )
# 
#     threads = min(len(partitions), target_concurrency)
#     with _thread_mapper(threads) as thread_map:
#         results = thread_map(run_cmd_partition, partitions)
# 
#         for proc_retcode, proc_out, _ in results:
#             retcode = max(retcode, proc_retcode)
#             stdout += proc_out
# 
#     return retcode, stdout


# Path: tests/xargs_test.py
import concurrent.futures
import os
import sys
import time
import pytest
from unittest import mock
from pre_commit import parse_shebang
from pre_commit import xargs

        ('foo',) + ('A',) * 6,
        ('foo',) + ('A',) * 6,
        ('foo',) + ('A',) * 4,
    )


def test_partition_target_concurrency_wont_make_tiny_partitions():
    ret = xargs.partition(
        ('foo',), ('A',) * 10,
        4,
        _max_length=50,
    )
    assert ret == (
        ('foo',) + ('A',) * 4,
        ('foo',) + ('A',) * 4,
        ('foo',) + ('A',) * 2,
    )


def test_argument_too_long():
    with pytest.raises(xargs.ArgumentTooLongError):
        xargs.partition(('a' * 5,), ('a' * 5,), 1, _max_length=10)


def test_xargs_smoke():
    ret, out = xargs.xargs(('echo',), ('hello', 'world'))
    assert ret == 0
    assert out.replace(b'\r\n', b'\n') == b'hello world\n'



exit_cmd = parse_shebang.normalize_cmd(('bash', '-c', 'exit $1', '--'))