# Path: pre_commit_hooks/check_executables_have_shebangs.py
# EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
# def check_executables(paths: list[str]) -> int:
# def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile, None, None]:
# def _check_git_filemode(paths: Sequence[str]) -> int:
# def has_shebang(path: str) -> int:
# def _message(path: str) -> None:
# def main(argv: Sequence[str] | None = None) -> int:
# class GitLsFile(NamedTuple):
# 
# Path: pre_commit_hooks/check_executables_have_shebangs.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser(description=__doc__)
#     parser.add_argument('filenames', nargs='*')
#     args = parser.parse_args(argv)
# 
#     return check_executables(args.filenames)
# 
# Path: pre_commit_hooks/util.py
# def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
#     kwargs.setdefault('stdout', subprocess.PIPE)
#     kwargs.setdefault('stderr', subprocess.PIPE)
#     proc = subprocess.Popen(cmd, **kwargs)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode()
#     if retcode is not None and proc.returncode != retcode:
#         raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
#     return stdout


import os
import sys
import pytest
from pre_commit_hooks import check_executables_have_shebangs
from pre_commit_hooks.check_executables_have_shebangs import main
from pre_commit_hooks.util import cmd_output
    path.write(content, 'wb')
    assert main((str(path),)) == 1
    _, stderr = capsys.readouterr()
    assert stderr.startswith(f'{path}: marked executable but')


def test_check_git_filemode_passing(tmpdir):
    with tmpdir.as_cwd():
        cmd_output('git', 'init', '.')

        f = tmpdir.join('f')
        f.write('#!/usr/bin/env bash')
        f_path = str(f)
        cmd_output('chmod', '+x', f_path)
        cmd_output('git', 'add', f_path)
        cmd_output('git', 'update-index', '--chmod=+x', f_path)

        g = tmpdir.join('g').ensure()
        g_path = str(g)
        cmd_output('git', 'add', g_path)

        # this is potentially a problem, but not something the script intends
        # to check for -- we're only making sure that things that are
        # executable have shebangs
        h = tmpdir.join('h')
        h.write('#!/usr/bin/env bash')
        h_path = str(h)
        cmd_output('git', 'add', h_path)

        files = (f_path, g_path, h_path)

# Path: pre_commit_hooks/check_executables_have_shebangs.py
# EXECUTABLE_VALUES = frozenset(('1', '3', '5', '7'))
# def check_executables(paths: list[str]) -> int:
# def git_ls_files(paths: Sequence[str]) -> Generator[GitLsFile, None, None]:
# def _check_git_filemode(paths: Sequence[str]) -> int:
# def has_shebang(path: str) -> int:
# def _message(path: str) -> None:
# def main(argv: Sequence[str] | None = None) -> int:
# class GitLsFile(NamedTuple):
# 
# Path: pre_commit_hooks/check_executables_have_shebangs.py
# def main(argv: Sequence[str] | None = None) -> int:
#     parser = argparse.ArgumentParser(description=__doc__)
#     parser.add_argument('filenames', nargs='*')
#     args = parser.parse_args(argv)
# 
#     return check_executables(args.filenames)
# 
# Path: pre_commit_hooks/util.py
# def cmd_output(*cmd: str, retcode: int | None = 0, **kwargs: Any) -> str:
#     kwargs.setdefault('stdout', subprocess.PIPE)
#     kwargs.setdefault('stderr', subprocess.PIPE)
#     proc = subprocess.Popen(cmd, **kwargs)
#     stdout, stderr = proc.communicate()
#     stdout = stdout.decode()
#     if retcode is not None and proc.returncode != retcode:
#         raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
#     return stdout


# Path: tests/check_executables_have_shebangs_test.py
import os
import sys
import pytest
from pre_commit_hooks import check_executables_have_shebangs
from pre_commit_hooks.check_executables_have_shebangs import main
from pre_commit_hooks.util import cmd_output

    path.write(content, 'wb')
    assert main((str(path),)) == 1
    _, stderr = capsys.readouterr()
    assert stderr.startswith(f'{path}: marked executable but')


def test_check_git_filemode_passing(tmpdir):
    with tmpdir.as_cwd():
        cmd_output('git', 'init', '.')

        f = tmpdir.join('f')
        f.write('#!/usr/bin/env bash')
        f_path = str(f)
        cmd_output('chmod', '+x', f_path)
        cmd_output('git', 'add', f_path)
        cmd_output('git', 'update-index', '--chmod=+x', f_path)

        g = tmpdir.join('g').ensure()
        g_path = str(g)
        cmd_output('git', 'add', g_path)

        # this is potentially a problem, but not something the script intends
        # to check for -- we're only making sure that things that are
        # executable have shebangs
        h = tmpdir.join('h')
        h.write('#!/usr/bin/env bash')
        h_path = str(h)
        cmd_output('git', 'add', h_path)

        files = (f_path, g_path, h_path)

        assert check_executables_have_shebangs._check_git_filemode(files) == 0