| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
from subprocess import CalledProcessError |
| 5 |
from unittest.mock import call, patch |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from auditwheel.patcher import Patchelf |
| 10 |
|
| 11 |
|
| 12 |
@patch("auditwheel.patcher.which") |
| 13 |
def test_patchelf_unavailable(which): |
| 14 |
which.return_value = False |
| 15 |
with pytest.raises(ValueError, match="Cannot find required utility"): |
| 16 |
Patchelf() |
| 17 |
|
| 18 |
|
| 19 |
@patch("auditwheel.patcher.which") |
| 20 |
@patch("auditwheel.patcher.check_output") |
| 21 |
def test_patchelf_check_output_fail(check_output, which): |
| 22 |
which.return_value = True |
| 23 |
check_output.side_effect = CalledProcessError(1, "patchelf --version") |
| 24 |
with pytest.raises(ValueError, match="Could not call"): |
| 25 |
Patchelf() |
| 26 |
|
| 27 |
|
| 28 |
@patch("auditwheel.patcher.which") |
| 29 |
@patch("auditwheel.patcher.check_output") |
| 30 |
@pytest.mark.parametrize("version", ["0.14", "0.14.1", "0.15"]) |
| 31 |
def test_patchelf_version_check(check_output, which, version): |
| 32 |
which.return_value = True |
| 33 |
check_output.return_value.decode.return_value = f"patchelf {version}" |
| 34 |
Patchelf() |
| 35 |
|
| 36 |
|
| 37 |
@patch("auditwheel.patcher.which") |
| 38 |
@patch("auditwheel.patcher.check_output") |
| 39 |
@pytest.mark.parametrize("version", ["0.13.99", "0.13", "0.9", "0.1"]) |
| 40 |
def test_patchelf_version_check_fail(check_output, which, version): |
| 41 |
which.return_value = True |
| 42 |
check_output.return_value.decode.return_value = f"patchelf {version}" |
| 43 |
with pytest.raises(ValueError, match=f"patchelf {version} found"): |
| 44 |
Patchelf() |
| 45 |
|
| 46 |
|
| 47 |
@patch("auditwheel.patcher._verify_patchelf") |
| 48 |
@patch("auditwheel.patcher.check_output") |
| 49 |
@patch("auditwheel.patcher.check_call") |
| 50 |
class TestPatchElf: |
| 51 |
""" "Validate that patchelf is invoked with the correct arguments.""" |
| 52 |
|
| 53 |
def test_replace_needed_one(self, check_call, _0, _1): # noqa: PT019 |
| 54 |
patcher = Patchelf() |
| 55 |
filename = Path("test.so") |
| 56 |
soname_old = "TEST_OLD" |
| 57 |
soname_new = "TEST_NEW" |
| 58 |
patcher.replace_needed(filename, (soname_old, soname_new)) |
| 59 |
check_call.assert_called_once_with( |
| 60 |
["patchelf", "--replace-needed", soname_old, soname_new, filename], |
| 61 |
) |
| 62 |
|
| 63 |
def test_replace_needed_multple(self, check_call, _0, _1): # noqa: PT019 |
| 64 |
patcher = Patchelf() |
| 65 |
filename = Path("test.so") |
| 66 |
replacements = [ |
| 67 |
("TEST_OLD1", "TEST_NEW1"), |
| 68 |
("TEST_OLD2", "TEST_NEW2"), |
| 69 |
] |
| 70 |
patcher.replace_needed(filename, *replacements) |
| 71 |
check_call.assert_called_once_with( |
| 72 |
[ |
| 73 |
"patchelf", |
| 74 |
"--replace-needed", |
| 75 |
*replacements[0], |
| 76 |
"--replace-needed", |
| 77 |
*replacements[1], |
| 78 |
filename, |
| 79 |
], |
| 80 |
) |
| 81 |
|
| 82 |
def test_set_soname(self, check_call, _0, _1): # noqa: PT019 |
| 83 |
patcher = Patchelf() |
| 84 |
filename = Path("test.so") |
| 85 |
soname_new = "TEST_NEW" |
| 86 |
patcher.set_soname(filename, soname_new) |
| 87 |
check_call.assert_called_once_with( |
| 88 |
["patchelf", "--set-soname", soname_new, filename], |
| 89 |
) |
| 90 |
|
| 91 |
@pytest.mark.parametrize("platform", ["android_24_x86_64", "manylinux_2_26_x86_64"]) |
| 92 |
def test_set_rpath(self, check_call, _0, _1, platform): # noqa: PT019 |
| 93 |
patcher = Patchelf(platform) |
| 94 |
filename = Path("test.so") |
| 95 |
patcher.set_rpath(filename, "$ORIGIN/.lib") |
| 96 |
check_call_expected_args = [ |
| 97 |
call(["patchelf", "--remove-rpath", filename]), |
| 98 |
call( |
| 99 |
["patchelf"] |
| 100 |
+ ([] if platform.startswith("android") else ["--force-rpath"]) |
| 101 |
+ ["--set-rpath", "$ORIGIN/.lib", filename], |
| 102 |
), |
| 103 |
] |
| 104 |
|
| 105 |
assert check_call.call_args_list == check_call_expected_args |
| 106 |
|
| 107 |
def test_set_rpath_android_old(self, check_call, _0, _1): # noqa: PT019 |
| 108 |
patcher = Patchelf("android_23_x86_64") |
| 109 |
filename = Path("test.so") |
| 110 |
with pytest.raises(ValueError, match="RUNPATH requires API level 24 or higher"): |
| 111 |
patcher.set_rpath(filename, "$ORIGIN/.lib") |
| 112 |
check_call.assert_not_called() |
| 113 |
|
| 114 |
def test_get_rpath(self, _0, check_output, _1): # noqa: PT019 |
| 115 |
patcher = Patchelf() |
| 116 |
filename = Path("test.so") |
| 117 |
check_output.return_value = b"existing_rpath" |
| 118 |
result = patcher.get_rpath(filename) |
| 119 |
check_output_expected_args = [call(["patchelf", "--print-rpath", filename])] |
| 120 |
|
| 121 |
assert result == check_output.return_value.decode() |
| 122 |
assert check_output.call_args_list == check_output_expected_args |
| 123 |
|
| 124 |
def test_remove_needed(self, check_call, _0, _1): # noqa: PT019 |
| 125 |
patcher = Patchelf() |
| 126 |
filename = Path("test.so") |
| 127 |
soname_1 = "TEST_REM_1" |
| 128 |
soname_2 = "TEST_REM_2" |
| 129 |
patcher.remove_needed(filename, soname_1, soname_2) |
| 130 |
check_call.assert_called_once_with( |
| 131 |
[ |
| 132 |
"patchelf", |
| 133 |
"--remove-needed", |
| 134 |
soname_1, |
| 135 |
"--remove-needed", |
| 136 |
soname_2, |
| 137 |
filename, |
| 138 |
], |
| 139 |
) |
| 140 |
|
| 141 |
def test_clear_rpath(self, check_call, _0, _1): # noqa: PT019 |
| 142 |
patcher = Patchelf() |
| 143 |
filename = Path("test.so") |
| 144 |
patcher.clear_rpath(filename) |
| 145 |
check_call_expected_args = [ |
| 146 |
call(["patchelf", "--remove-rpath", filename]), |
| 147 |
] |
| 148 |
|
| 149 |
assert check_call.call_args_list == check_call_expected_args |