Skip to content

Commit

Permalink
fix test part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Czaki committed Feb 6, 2024
1 parent 2078633 commit 07c3976
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 58 deletions.
11 changes: 7 additions & 4 deletions delocate/delocating.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

import functools
import itertools
import logging
import os
import re
Expand Down Expand Up @@ -46,6 +45,7 @@
)
from .tmpdirs import TemporaryDirectory
from .tools import (
_is_macho_file,
_remove_absolute_rpaths,
dir2zip,
find_package_dirs,
Expand Down Expand Up @@ -604,6 +604,8 @@ def _get_macos_min_version(dylib_path: Path) -> Iterable[tuple[str, Version]]:
Iterable[tuple[str, Version]]
A list of tuples containing the CPU type and the minimum macOS version.
"""
if dylib_path.is_dir() or not _is_macho_file(dylib_path):

Check failure on line 607 in delocate/delocating.py

View workflow job for this annotation

GitHub Actions / mypy

Argument 1 to "_is_macho_file" has incompatible type "Path"; expected "str"
return []
m = MachO(dylib_path)
res = []
for header in m.headers:
Expand Down Expand Up @@ -710,14 +712,15 @@ def _calculate_minimum_wheel_name(
provided one.
"""
# get platform tag from wheel name using packaging
if wheel_name.endswith("any.whl"):
# universal wheel, no need to update the platform tag
return wheel_name, set()
arch_version = _get_archs_and_version_from_wheel_name(wheel_name)
# get the architecture and minimum macOS version from the libraries
# in the wheel
version_info_dict: Dict[str, Dict[Version, List[Path]]] = {}

for lib in itertools.chain(
wheel_dir.glob("**/*.dylib"), wheel_dir.glob("**/*.so")
):
for lib in wheel_dir.glob("**/*"):
for arch, version in _get_macos_min_version(lib):
version_info_dict.setdefault(arch.lower(), {}).setdefault(
version, []
Expand Down
4 changes: 3 additions & 1 deletion delocate/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
@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")
plat_wheel_tmp = str(
tmp_path / "plat-1.0-cp311-cp311-macosx_10_9_x86_64.whl"
)
stray_lib: str = STRAY_LIB_DEP

with InWheelCtx(PLAT_WHEEL, plat_wheel_tmp):
Expand Down
83 changes: 47 additions & 36 deletions delocate/tests/test_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,17 @@ def test_wheel(script_runner: ScriptRunner) -> None:
)
_check_wheel(Path("fixed", basename(fixed_wheel)), ".dylibs")
# More than one wheel
shutil.copy2(fixed_wheel, "wheel_copy.ext")
copy_name = "fakepkg1_copy-1.0-cp36-abi3-macosx_10_9_universal2.whl"
shutil.copy2(fixed_wheel, copy_name)
result = script_runner.run(
["delocate-wheel", "-w", "fixed2", fixed_wheel, "wheel_copy.ext"],
["delocate-wheel", "-w", "fixed2", fixed_wheel, copy_name],
check=True,
)
assert _proc_lines(result.stdout) == [
"Fixing: " + name for name in (fixed_wheel, "wheel_copy.ext")
"Fixing: " + name for name in (fixed_wheel, copy_name)
]
_check_wheel(Path("fixed2", basename(fixed_wheel)), ".dylibs")
_check_wheel(Path("fixed2", "wheel_copy.ext"), ".dylibs")
_check_wheel(Path("fixed2", copy_name), ".dylibs")

# Verbose - single wheel
result = script_runner.run(
Expand All @@ -283,12 +284,12 @@ def test_wheel(script_runner: ScriptRunner) -> None:
"--wheel-dir",
"fixed4",
fixed_wheel,
"wheel_copy.ext",
copy_name,
],
check=True,
)
wheel_lines2 = [
"Fixing: wheel_copy.ext",
f"Fixing: {copy_name}",
"Copied to package .dylibs directory:",
stray_lib,
]
Expand All @@ -298,20 +299,24 @@ def test_wheel(script_runner: ScriptRunner) -> None:
@pytest.mark.xfail( # type: ignore[misc]
sys.platform != "darwin", reason="Needs macOS linkage."
)
def test_fix_wheel_dylibs(script_runner: ScriptRunner) -> None:
def test_fix_wheel_dylibs(script_runner: ScriptRunner, tmp_path: Path) -> None:
# Check default and non-default search for dynamic libraries
with InTemporaryDirectory() as tmpdir:
# Default in-place fix
fixed_wheel, stray_lib = _fixed_wheel(tmpdir)
_rename_module(fixed_wheel, "module.other", "test.whl")
shutil.copyfile("test.whl", "test2.whl")
# Default is to look in all files and therefore fix
script_runner.run(["delocate-wheel", "test.whl"], check=True)
_check_wheel("test.whl", ".dylibs")
# Can turn this off to only look in dynamic lib exts
script_runner.run(["delocate-wheel", "test2.whl", "-d"], check=True)
with InWheel("test2.whl"): # No fix
assert not Path("fakepkg1", ".dylibs").exists()
fixed_wheel, stray_lib = _fixed_wheel(tmp_path)
test1_name = (
tmp_path / "fakepkg1_test-1.0-cp36-abi3-macosx_10_9_universal2.whl"
)
test2_name = (
tmp_path / "fakepkg1_test2-1.0-cp36-abi3-macosx_10_9_universal2.whl"
)
_rename_module(fixed_wheel, "module.other", test1_name)
shutil.copyfile(test1_name, test2_name)
# Default is to look in all files and therefore fix
script_runner.run(["delocate-wheel", test1_name], check=True)
_check_wheel(test1_name, ".dylibs")
# Can turn this off to only look in dynamic lib exts
script_runner.run(["delocate-wheel", test2_name, "-d"], check=True)
with InWheel(test2_name): # No fix
assert not Path("fakepkg1", ".dylibs").exists()


@pytest.mark.xfail( # type: ignore[misc]
Expand Down Expand Up @@ -577,23 +582,29 @@ def test_add_platforms(script_runner: ScriptRunner) -> None:


@pytest.mark.xfail(sys.platform != "darwin", reason="Needs macOS linkage.")
def test_fix_wheel_with_excluded_dylibs(script_runner: ScriptRunner):
with InTemporaryDirectory() as tmpdir:
fixed_wheel, stray_lib = _fixed_wheel(tmpdir)
_rename_module(fixed_wheel, "module.other", "test.whl")
shutil.copyfile("test.whl", "test2.whl")
# We exclude the stray library so it shouldn't be present in the wheel
result = script_runner.run(
["delocate-wheel", "-vv", "-e", "extfunc", "test.whl"], check=True
)
assert "libextfunc.dylib excluded" in result.stderr
with InWheel("test.whl"):
assert not Path("plat_pkg/fakepkg1/.dylibs").exists()
# We exclude a library that does not exist so we should behave normally
script_runner.run(
["delocate-wheel", "-e", "doesnotexist", "test2.whl"], check=True
)
_check_wheel("test2.whl", ".dylibs")
def test_fix_wheel_with_excluded_dylibs(script_runner: ScriptRunner, tmp_path):
fixed_wheel, stray_lib = _fixed_wheel(tmp_path)
test1_name = (
tmp_path / "fakepkg1_test-1.0-cp36-abi3-macosx_10_9_universal2.whl"
)
test2_name = (
tmp_path / "fakepkg1_test2-1.0-cp36-abi3-macosx_10_9_universal2.whl"
)

_rename_module(fixed_wheel, "module.other", test1_name)
shutil.copyfile(test1_name, test2_name)
# We exclude the stray library so it shouldn't be present in the wheel
result = script_runner.run(
["delocate-wheel", "-vv", "-e", "extfunc", test1_name], check=True
)
assert "libextfunc.dylib excluded" in result.stderr
with InWheel(test1_name):
assert not Path("plat_pkg/fakepkg1/.dylibs").exists()
# We exclude a library that does not exist so we should behave normally
script_runner.run(
["delocate-wheel", "-e", "doesnotexist", test2_name], check=True
)
_check_wheel(test1_name, ".dylibs")


@pytest.mark.xfail( # type: ignore[misc]
Expand Down
71 changes: 54 additions & 17 deletions delocate/tests/test_wheelies.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,23 @@ def test_fix_plat() -> None:
assert os.listdir(dylibs) == ["libextfunc.dylib"]
# New output name
fixed_wheel, stray_lib = _fixed_wheel(tmpdir)
assert delocate_wheel(fixed_wheel, "fixed_wheel.ext") == {
_rp(stray_lib): {dep_mod: stray_lib}
}
zip2dir("fixed_wheel.ext", "plat_pkg1")
assert delocate_wheel(
fixed_wheel, "fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl"
) == {_rp(stray_lib): {dep_mod: stray_lib}}
zip2dir("fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl", "plat_pkg1")
assert exists(pjoin("plat_pkg1", "fakepkg1"))
dylibs = pjoin("plat_pkg1", "fakepkg1", ".dylibs")
assert exists(dylibs)
assert os.listdir(dylibs) == ["libextfunc.dylib"]
# Test another lib output directory
assert delocate_wheel(
fixed_wheel, "fixed_wheel2.ext", "dylibs_dir"
fixed_wheel,
"fixed_wheel2-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
"dylibs_dir",
) == {_rp(stray_lib): {dep_mod: stray_lib}}
zip2dir("fixed_wheel2.ext", "plat_pkg2")
zip2dir(
"fixed_wheel2-1.0-cp39-cp39-macosx_10_9_x86_64.whl", "plat_pkg2"
)
assert exists(pjoin("plat_pkg2", "fakepkg1"))
dylibs = pjoin("plat_pkg2", "fakepkg1", "dylibs_dir")
assert exists(dylibs)
Expand Down Expand Up @@ -202,21 +206,35 @@ def test_fix_plat_dylibs():
# Check default and non-default searches for dylibs
with InTemporaryDirectory() as tmpdir:
fixed_wheel, stray_lib = _fixed_wheel(tmpdir)
_rename_module(fixed_wheel, "module.other", "test.whl")
_rename_module(
fixed_wheel,
"module.other",
"fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
)
# With dylibs-only - only analyze files with exts '.dylib', '.so'
assert_equal(
delocate_wheel("test.whl", lib_filt_func="dylibs-only"), {}
delocate_wheel(
"fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
lib_filt_func="dylibs-only",
),
{},
)
# With func that doesn't find the module

def func(fn):
return fn.endswith(".so")

assert_equal(delocate_wheel("test.whl", lib_filt_func=func), {})
assert_equal(
delocate_wheel(
"fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
lib_filt_func=func,
),
{},
)
# Default - looks in every file
dep_mod = pjoin("fakepkg1", "subpkg", "module.other")
assert_equal(
delocate_wheel("test.whl"),
delocate_wheel("fixed_wheel-1.0-cp39-cp39-macosx_10_9_x86_64.whl"),
{realpath(stray_lib): {dep_mod: stray_lib}},
)

Expand Down Expand Up @@ -366,9 +384,14 @@ def test_fix_rpath():
},
}

assert delocate_wheel(RPATH_WHEEL, "tmp.whl") == stray_libs
assert (
delocate_wheel(
RPATH_WHEEL, "out-1.0-cp39-cp39-macosx_10_9_x86_64.whl"
)
== stray_libs
)

with InWheel("tmp.whl"):
with InWheel("out-1.0-cp39-cp39-macosx_10_9_x86_64.whl"):
check_call(
[
"codesign",
Expand All @@ -386,7 +409,9 @@ def ignore_libextfunc(path: str) -> bool:

assert (
delocate_wheel(
RPATH_WHEEL, "tmp.whl", lib_filt_func=ignore_libextfunc
RPATH_WHEEL,
"tmp-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
lib_filt_func=ignore_libextfunc,
)
== {}
)
Expand All @@ -402,7 +427,9 @@ def ignore_libextfunc2(path: str) -> bool:

assert (
delocate_wheel(
RPATH_WHEEL, "tmp.whl", lib_filt_func=ignore_libextfunc2
RPATH_WHEEL,
"tmp-1.0-cp39-cp39-macosx_10_9_x86_64.whl",
lib_filt_func=ignore_libextfunc2,
)
== stray_libs_only_direct
)
Expand All @@ -424,8 +451,13 @@ def test_fix_toplevel() -> None:
realpath("libs/libextfunc2_rpath.dylib"): {dep_mod: dep_path},
}

assert delocate_wheel(TOPLEVEL_WHEEL, "out.whl") == stray_libs
with InWheel("out.whl") as wheel_path:
assert (
delocate_wheel(
TOPLEVEL_WHEEL, "out-1.0-cp39-cp39-macosx_10_9_x86_64.whl"
)
== stray_libs
)
with InWheel("out-1.0-cp39-cp39-macosx_10_9_x86_64.whl") as wheel_path:
assert "fakepkg_toplevel.dylibs" in os.listdir(wheel_path)


Expand All @@ -443,7 +475,12 @@ def test_fix_namespace() -> None:
realpath("libs/libextfunc2_rpath.dylib"): {dep_mod: dep_path},
}

assert delocate_wheel(NAMESPACE_WHEEL, "out.whl") == stray_libs
assert (
delocate_wheel(
NAMESPACE_WHEEL, "out-1.0-cp39-cp39-macosx_10_9_x86_64.whl"
)
== stray_libs
)


def test_source_date_epoch() -> None:
Expand Down

0 comments on commit 07c3976

Please sign in to comment.