# 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/languages/ruby.py
# ENVIRONMENT_DIR = 'rbenv'
# def get_default_version() -> str:
# def get_env_patch(
#         venv: str,
#         language_version: str,
# ) -> PatchesT:
# def in_env(
#         prefix: Prefix,
#         language_version: str,
# ) -> Generator[None, None, None]:
# def _extract_resource(filename: str, dest: str) -> None:
# def _install_rbenv(
#         prefix: Prefix,
#         version: str,
# ) -> None:  # pragma: win32 no cover
# def _install_ruby(
#         prefix: Prefix,
#         version: str,
# ) -> None:  # pragma: win32 no cover
# def install_environment(
#         prefix: Prefix, version: str, additional_dependencies: Sequence[str],
# ) -> None:
# def run_hook(
#         hook: Hook,
#         file_args: Sequence[str],
#         color: bool,
# ) -> tuple[int, bytes]:
# 
# Path: pre_commit/prefix.py
# class Prefix(NamedTuple):
#     prefix_dir: str
# 
#     def path(self, *parts: str) -> str:
#         return os.path.normpath(os.path.join(self.prefix_dir, *parts))
# 
#     def exists(self, *parts: str) -> bool:
#         return os.path.exists(self.path(*parts))
# 
#     def star(self, end: str) -> tuple[str, ...]:
#         paths = os.listdir(self.prefix_dir)
#         return tuple(path for path in paths if path.endswith(end))
# 
# Path: pre_commit/util.py
# def cmd_output(*cmd: str, **kwargs: Any) -> tuple[int, str, str | None]:
#     returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
#     stdout = stdout_b.decode() if stdout_b is not None else None
#     stderr = stderr_b.decode() if stderr_b is not None else None
#     return returncode, stdout, stderr
# 
# Path: pre_commit/util.py
# def resource_bytesio(filename: str) -> IO[bytes]:
#     return importlib.resources.open_binary('pre_commit.resources', filename)
# 
# Path: testing/util.py
# TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
# def docker_is_running() -> bool:  # pragma: win32 no cover
# def get_resource_path(path):
# def cmd_output_mocked_pre_commit_home(
#         *args, tempdir_factory, pre_commit_home=None, env=None, **kwargs,
# ):
# def run_opts(
#         all_files=False,
#         files=(),
#         color=False,
#         verbose=False,
#         hook=None,
#         remote_branch='',
#         local_branch='',
#         from_ref='',
#         to_ref='',
#         remote_name='',
#         remote_url='',
#         hook_stage='commit',
#         show_diff_on_failure=False,
#         commit_msg_filename='',
#         checkout_type='',
#         is_squash_merge='',
#         rewrite_command='',
# ):
# def cwd(path):
# def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):


import os.path
import tarfile
import pytest
import pre_commit.constants as C
from unittest import mock
from pre_commit import parse_shebang
from pre_commit.languages import ruby
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output
from pre_commit.util import resource_bytesio
from testing.util import xfailif_windows
@pytest.fixture
def find_exe_mck():
    with mock.patch.object(parse_shebang, 'find_executable') as mck:
        yield mck


def test_uses_default_version_when_not_available(find_exe_mck):
    find_exe_mck.return_value = None
    assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT


def test_uses_system_if_both_gem_and_ruby_are_available(find_exe_mck):
    find_exe_mck.return_value = '/path/to/exe'
    assert ACTUAL_GET_DEFAULT_VERSION() == 'system'


@pytest.fixture
def fake_gem_prefix(tmpdir):
    gemspec = '''\
Gem::Specification.new do |s|
    s.name = 'pre_commit_placeholder_package'
    s.version = '0.0.0'
    s.summary = 'placeholder gem for pre-commit hooks'
    s.authors = ['Anthony Sottile']
end
'''
    tmpdir.join('placeholder_gem.gemspec').write(gemspec)
    yield Prefix(tmpdir)



# 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/languages/ruby.py
# ENVIRONMENT_DIR = 'rbenv'
# def get_default_version() -> str:
# def get_env_patch(
#         venv: str,
#         language_version: str,
# ) -> PatchesT:
# def in_env(
#         prefix: Prefix,
#         language_version: str,
# ) -> Generator[None, None, None]:
# def _extract_resource(filename: str, dest: str) -> None:
# def _install_rbenv(
#         prefix: Prefix,
#         version: str,
# ) -> None:  # pragma: win32 no cover
# def _install_ruby(
#         prefix: Prefix,
#         version: str,
# ) -> None:  # pragma: win32 no cover
# def install_environment(
#         prefix: Prefix, version: str, additional_dependencies: Sequence[str],
# ) -> None:
# def run_hook(
#         hook: Hook,
#         file_args: Sequence[str],
#         color: bool,
# ) -> tuple[int, bytes]:
# 
# Path: pre_commit/prefix.py
# class Prefix(NamedTuple):
#     prefix_dir: str
# 
#     def path(self, *parts: str) -> str:
#         return os.path.normpath(os.path.join(self.prefix_dir, *parts))
# 
#     def exists(self, *parts: str) -> bool:
#         return os.path.exists(self.path(*parts))
# 
#     def star(self, end: str) -> tuple[str, ...]:
#         paths = os.listdir(self.prefix_dir)
#         return tuple(path for path in paths if path.endswith(end))
# 
# Path: pre_commit/util.py
# def cmd_output(*cmd: str, **kwargs: Any) -> tuple[int, str, str | None]:
#     returncode, stdout_b, stderr_b = cmd_output_b(*cmd, **kwargs)
#     stdout = stdout_b.decode() if stdout_b is not None else None
#     stderr = stderr_b.decode() if stderr_b is not None else None
#     return returncode, stdout, stderr
# 
# Path: pre_commit/util.py
# def resource_bytesio(filename: str) -> IO[bytes]:
#     return importlib.resources.open_binary('pre_commit.resources', filename)
# 
# Path: testing/util.py
# TESTING_DIR = os.path.abspath(os.path.dirname(__file__))
# def docker_is_running() -> bool:  # pragma: win32 no cover
# def get_resource_path(path):
# def cmd_output_mocked_pre_commit_home(
#         *args, tempdir_factory, pre_commit_home=None, env=None, **kwargs,
# ):
# def run_opts(
#         all_files=False,
#         files=(),
#         color=False,
#         verbose=False,
#         hook=None,
#         remote_branch='',
#         local_branch='',
#         from_ref='',
#         to_ref='',
#         remote_name='',
#         remote_url='',
#         hook_stage='commit',
#         show_diff_on_failure=False,
#         commit_msg_filename='',
#         checkout_type='',
#         is_squash_merge='',
#         rewrite_command='',
# ):
# def cwd(path):
# def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):


# Path: tests/languages/ruby_test.py
import os.path
import tarfile
import pytest
import pre_commit.constants as C
from unittest import mock
from pre_commit import parse_shebang
from pre_commit.languages import ruby
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output
from pre_commit.util import resource_bytesio
from testing.util import xfailif_windows

@pytest.fixture
def find_exe_mck():
    with mock.patch.object(parse_shebang, 'find_executable') as mck:
        yield mck


def test_uses_default_version_when_not_available(find_exe_mck):
    find_exe_mck.return_value = None
    assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT


def test_uses_system_if_both_gem_and_ruby_are_available(find_exe_mck):
    find_exe_mck.return_value = '/path/to/exe'
    assert ACTUAL_GET_DEFAULT_VERSION() == 'system'


@pytest.fixture
def fake_gem_prefix(tmpdir):
    gemspec = '''\
Gem::Specification.new do |s|
    s.name = 'pre_commit_placeholder_package'
    s.version = '0.0.0'
    s.summary = 'placeholder gem for pre-commit hooks'
    s.authors = ['Anthony Sottile']
end
'''
    tmpdir.join('placeholder_gem.gemspec').write(gemspec)
    yield Prefix(tmpdir)



@xfailif_windows  # pragma: win32 no cover