# Path: delocate/tools.py
# @ensure_writable
# def set_install_name(
#     filename: str, oldname: str, newname: str, ad_hoc_sign: bool = True
# ) -> None:
#     """Set install name `oldname` to `newname` in library filename
# 
#     Parameters
#     ----------
#     filename : str
#         filename of library
#     oldname : str
#         current install name in library
#     newname : str
#         replacement name for `oldname`
#     ad_hoc_sign : {True, False}, optional
#         If True, sign library with ad-hoc signature
#     """
#     names = get_install_names(filename)
#     if oldname not in names:
#         raise InstallNameError(
#             "{0} not in install names for {1}".format(oldname, filename)
#         )
#     subprocess.run(
#         ["install_name_tool", "-change", oldname, newname, filename],
#         stdout=subprocess.PIPE,
#         stderr=subprocess.PIPE,
#         check=True,
#     )
#     if ad_hoc_sign:
#         # ad hoc signature is represented by a dash
#         # https://developer.apple.com/documentation/security/seccodesignatureflags/kseccodesignatureadhoc
#         replace_signature(filename, "-")
# 
# Path: delocate/wheeltools.py
# class InWheelCtx(InWheel):
#     """Context manager for doing things inside wheels
# 
#     On entering, you'll find yourself in the root tree of the wheel.  If you've
#     asked for an output wheel, then on exit we'll rewrite the wheel record and
#     pack stuff up for you.
# 
#     The context manager returns itself from the __enter__ method, so you can
#     set things like ``out_wheel``.  This is useful when processing in the wheel
#     will dicate what the output wheel name is, or whether you want to save at
#     all.
# 
#     The current path of the wheel contents is set in the attribute
#     ``wheel_path``.
#     """
# 
#     def __init__(self, in_wheel, out_wheel=None):
#         """Init in-wheel context manager returning self from enter
# 
#         Parameters
#         ----------
#         in_wheel : str
#             filename of wheel to unpack and work inside
#         out_wheel : None or str:
#             filename of wheel to write after exiting.  If None, don't write and
#             discard
#         """
#         super(InWheelCtx, self).__init__(in_wheel, out_wheel)
#         self.wheel_path = None
# 
#     def __enter__(self):
#         self.wheel_path = super(InWheelCtx, self).__enter__()
#         return self
# 
# Path: delocate/tests/test_wheelies.py
# PLAT_WHEEL = _collect_wheel("fakepkg1-1.0-cp*.whl")
# 
# STRAY_LIB_DEP = realpath(STRAY_LIB)
# 
# class PlatWheel(NamedTuple):
#     """Information about a temporary platform wheel."""
# 
#     whl: str  # Path to the wheel.
#     stray_lib: str  # Path to the external library.


import os
import pytest
from pathlib import Path
from typing import Iterator
from delocate.tools import set_install_name
from delocate.wheeltools import InWheelCtx
from .test_wheelies import PLAT_WHEEL, STRAY_LIB_DEP, PlatWheel




@pytest.fixture
def plat_wheel(tmp_path: Path) -> Iterator[PlatWheel]:
    """Return a modified platform wheel for testing."""
    plat_wheel_tmp = str(tmp_path / "plat-wheel.whl")
    stray_lib: str = STRAY_LIB_DEP

    with InWheelCtx(PLAT_WHEEL, plat_wheel_tmp):
        set_install_name(
            "fakepkg1/subpkg/module2.abi3.so",
            "libextfunc.dylib",
            stray_lib,
        )


# Path: delocate/tools.py
# @ensure_writable
# def set_install_name(
#     filename: str, oldname: str, newname: str, ad_hoc_sign: bool = True
# ) -> None:
#     """Set install name `oldname` to `newname` in library filename
# 
#     Parameters
#     ----------
#     filename : str
#         filename of library
#     oldname : str
#         current install name in library
#     newname : str
#         replacement name for `oldname`
#     ad_hoc_sign : {True, False}, optional
#         If True, sign library with ad-hoc signature
#     """
#     names = get_install_names(filename)
#     if oldname not in names:
#         raise InstallNameError(
#             "{0} not in install names for {1}".format(oldname, filename)
#         )
#     subprocess.run(
#         ["install_name_tool", "-change", oldname, newname, filename],
#         stdout=subprocess.PIPE,
#         stderr=subprocess.PIPE,
#         check=True,
#     )
#     if ad_hoc_sign:
#         # ad hoc signature is represented by a dash
#         # https://developer.apple.com/documentation/security/seccodesignatureflags/kseccodesignatureadhoc
#         replace_signature(filename, "-")
# 
# Path: delocate/wheeltools.py
# class InWheelCtx(InWheel):
#     """Context manager for doing things inside wheels
# 
#     On entering, you'll find yourself in the root tree of the wheel.  If you've
#     asked for an output wheel, then on exit we'll rewrite the wheel record and
#     pack stuff up for you.
# 
#     The context manager returns itself from the __enter__ method, so you can
#     set things like ``out_wheel``.  This is useful when processing in the wheel
#     will dicate what the output wheel name is, or whether you want to save at
#     all.
# 
#     The current path of the wheel contents is set in the attribute
#     ``wheel_path``.
#     """
# 
#     def __init__(self, in_wheel, out_wheel=None):
#         """Init in-wheel context manager returning self from enter
# 
#         Parameters
#         ----------
#         in_wheel : str
#             filename of wheel to unpack and work inside
#         out_wheel : None or str:
#             filename of wheel to write after exiting.  If None, don't write and
#             discard
#         """
#         super(InWheelCtx, self).__init__(in_wheel, out_wheel)
#         self.wheel_path = None
# 
#     def __enter__(self):
#         self.wheel_path = super(InWheelCtx, self).__enter__()
#         return self
# 
# Path: delocate/tests/test_wheelies.py
# PLAT_WHEEL = _collect_wheel("fakepkg1-1.0-cp*.whl")
# 
# STRAY_LIB_DEP = realpath(STRAY_LIB)
# 
# class PlatWheel(NamedTuple):
#     """Information about a temporary platform wheel."""
# 
#     whl: str  # Path to the wheel.
#     stray_lib: str  # Path to the external library.


# Path: delocate/tests/conftest.py
import os
import pytest
from pathlib import Path
from typing import Iterator
from delocate.tools import set_install_name
from delocate.wheeltools import InWheelCtx
from .test_wheelies import PLAT_WHEEL, STRAY_LIB_DEP, PlatWheel





@pytest.fixture
def plat_wheel(tmp_path: Path) -> Iterator[PlatWheel]:
    """Return a modified platform wheel for testing."""
    plat_wheel_tmp = str(tmp_path / "plat-wheel.whl")
    stray_lib: str = STRAY_LIB_DEP

    with InWheelCtx(PLAT_WHEEL, plat_wheel_tmp):
        set_install_name(
            "fakepkg1/subpkg/module2.abi3.so",
            "libextfunc.dylib",
            stray_lib,
        )


    yield PlatWheel(plat_wheel_tmp, os.path.realpath(stray_lib))