| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from unittest.mock import call, patch |
| 5 |
|
| 6 |
from auditwheel.patcher import Patchelf |
| 7 |
from auditwheel.repair import append_rpath_within_wheel |
| 8 |
|
| 9 |
|
| 10 |
@patch("auditwheel.patcher._verify_patchelf") |
| 11 |
@patch("auditwheel.patcher.check_output") |
| 12 |
@patch("auditwheel.patcher.check_call") |
| 13 |
class TestRepair: |
| 14 |
def test_append_rpath(self, check_call, check_output, _): # noqa: PT019 |
| 15 |
patcher = Patchelf() |
| 16 |
# When a library has an existing RPATH entry within wheel_dir |
| 17 |
existing_rpath = b"$ORIGIN/.existinglibdir" |
| 18 |
check_output.return_value = existing_rpath |
| 19 |
wheel_dir = Path.cwd() |
| 20 |
lib_name = Path("test.so") |
| 21 |
full_lib_name = lib_name.absolute() |
| 22 |
append_rpath_within_wheel(lib_name, "$ORIGIN/.lib", wheel_dir, patcher) |
| 23 |
check_output_expected_args = [ |
| 24 |
call(["patchelf", "--print-rpath", full_lib_name]), |
| 25 |
] |
| 26 |
# Then that entry is preserved when updating the RPATH |
| 27 |
check_call_expected_args = [ |
| 28 |
call(["patchelf", "--remove-rpath", full_lib_name]), |
| 29 |
call( |
| 30 |
[ |
| 31 |
"patchelf", |
| 32 |
"--force-rpath", |
| 33 |
"--set-rpath", |
| 34 |
f"{existing_rpath.decode()}:$ORIGIN/.lib", |
| 35 |
full_lib_name, |
| 36 |
], |
| 37 |
), |
| 38 |
] |
| 39 |
|
| 40 |
assert check_output.call_args_list == check_output_expected_args |
| 41 |
assert check_call.call_args_list == check_call_expected_args |
| 42 |
|
| 43 |
def test_append_rpath_reject_outside_wheel(self, check_call, check_output, _): # noqa: PT019 |
| 44 |
patcher = Patchelf() |
| 45 |
# When a library has an existing RPATH entry outside wheel_dir |
| 46 |
existing_rpath = b"/outside/wheel/dir" |
| 47 |
check_output.return_value = existing_rpath |
| 48 |
wheel_dir = Path("/not/outside") |
| 49 |
lib_name = Path("test.so") |
| 50 |
full_lib_name = lib_name.absolute() |
| 51 |
append_rpath_within_wheel(lib_name, "$ORIGIN/.lib", wheel_dir, patcher) |
| 52 |
check_output_expected_args = [ |
| 53 |
call(["patchelf", "--print-rpath", full_lib_name]), |
| 54 |
] |
| 55 |
# Then that entry is eliminated when updating the RPATH |
| 56 |
check_call_expected_args = [ |
| 57 |
call(["patchelf", "--remove-rpath", full_lib_name]), |
| 58 |
call( |
| 59 |
[ |
| 60 |
"patchelf", |
| 61 |
"--force-rpath", |
| 62 |
"--set-rpath", |
| 63 |
"$ORIGIN/.lib", |
| 64 |
full_lib_name, |
| 65 |
], |
| 66 |
), |
| 67 |
] |
| 68 |
|
| 69 |
assert check_output.call_args_list == check_output_expected_args |
| 70 |
assert check_call.call_args_list == check_call_expected_args |
| 71 |
|
| 72 |
def test_append_rpath_ignore_duplicates(self, check_call, check_output, _): # noqa: PT019 |
| 73 |
patcher = Patchelf() |
| 74 |
# When a library has an existing RPATH entry and we try and append it again |
| 75 |
existing_rpath = b"$ORIGIN" |
| 76 |
check_output.return_value = existing_rpath |
| 77 |
wheel_dir = Path.cwd() |
| 78 |
lib_name = Path("test.so") |
| 79 |
full_lib_name = lib_name.absolute() |
| 80 |
append_rpath_within_wheel(lib_name, "$ORIGIN", wheel_dir, patcher) |
| 81 |
check_output_expected_args = [ |
| 82 |
call(["patchelf", "--print-rpath", full_lib_name]), |
| 83 |
] |
| 84 |
# Then that entry is ignored when updating the RPATH |
| 85 |
check_call_expected_args = [ |
| 86 |
call(["patchelf", "--remove-rpath", full_lib_name]), |
| 87 |
call( |
| 88 |
["patchelf", "--force-rpath", "--set-rpath", "$ORIGIN", full_lib_name], |
| 89 |
), |
| 90 |
] |
| 91 |
|
| 92 |
assert check_output.call_args_list == check_output_expected_args |
| 93 |
assert check_call.call_args_list == check_call_expected_args |
| 94 |
|
| 95 |
def test_append_rpath_ignore_relative(self, check_call, check_output, _): # noqa: PT019 |
| 96 |
patcher = Patchelf() |
| 97 |
# When a library has an existing RPATH entry but it cannot be resolved |
| 98 |
# to an absolute path, it is eliminated |
| 99 |
existing_rpath = b"not/absolute" |
| 100 |
check_output.return_value = existing_rpath |
| 101 |
wheel_dir = Path.cwd() |
| 102 |
lib_name = Path("test.so") |
| 103 |
full_lib_name = lib_name.absolute() |
| 104 |
append_rpath_within_wheel(lib_name, "$ORIGIN", wheel_dir, patcher) |
| 105 |
check_output_expected_args = [ |
| 106 |
call(["patchelf", "--print-rpath", full_lib_name]), |
| 107 |
] |
| 108 |
# Then that entry is ignored when updating the RPATH |
| 109 |
check_call_expected_args = [ |
| 110 |
call(["patchelf", "--remove-rpath", full_lib_name]), |
| 111 |
call( |
| 112 |
["patchelf", "--force-rpath", "--set-rpath", "$ORIGIN", full_lib_name], |
| 113 |
), |
| 114 |
] |
| 115 |
|
| 116 |
assert check_output.call_args_list == check_output_expected_args |
| 117 |
assert check_call.call_args_list == check_call_expected_args |