# Path: pre_commit_hooks/check_shebang_scripts_are_executable.py
# def _check_git_filemode(paths: Sequence[str]) -> int:
#     seen: set[str] = set()
#     for ls_file in git_ls_files(paths):
#         is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
#         if not is_executable and has_shebang(ls_file.filename):
#             _message(ls_file.filename)
#             seen.add(ls_file.filename)
# 
#     return int(bool(seen))
# 
# Path: pre_commit_hooks/check_shebang_scripts_are_executable.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_shebangs(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 pytest
from pre_commit_hooks.check_shebang_scripts_are_executable import \
    _check_git_filemode
from pre_commit_hooks.check_shebang_scripts_are_executable import main
from pre_commit_hooks.util import cmd_output
from __future__ import annotations





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)

        files = [f_path, g_path]

# Path: pre_commit_hooks/check_shebang_scripts_are_executable.py
# def _check_git_filemode(paths: Sequence[str]) -> int:
#     seen: set[str] = set()
#     for ls_file in git_ls_files(paths):
#         is_executable = any(b in EXECUTABLE_VALUES for b in ls_file.mode[-3:])
#         if not is_executable and has_shebang(ls_file.filename):
#             _message(ls_file.filename)
#             seen.add(ls_file.filename)
# 
#     return int(bool(seen))
# 
# Path: pre_commit_hooks/check_shebang_scripts_are_executable.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_shebangs(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_shebang_scripts_are_executable_test.py
import os
import pytest
from pre_commit_hooks.check_shebang_scripts_are_executable import \
    _check_git_filemode
from pre_commit_hooks.check_shebang_scripts_are_executable import main
from pre_commit_hooks.util import cmd_output

from __future__ import annotations





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)

        files = [f_path, g_path]

        assert _check_git_filemode(files) == 0