| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import subprocess |
| 5 |
from pathlib import Path |
| 6 |
from unittest.mock import patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from auditwheel.error import InvalidLibcError |
| 11 |
from auditwheel.libc import Libc, LibcVersion, _find_musl_libc, _get_musl_version |
| 12 |
|
| 13 |
|
| 14 |
@patch("auditwheel.libc.Path") |
| 15 |
def test_find_musllinux_not_found(path_mock): |
| 16 |
path_mock.return_value.glob.return_value = [] |
| 17 |
with pytest.raises(InvalidLibcError): |
| 18 |
_find_musl_libc() |
| 19 |
assert Libc.detect() != Libc.MUSL |
| 20 |
|
| 21 |
|
| 22 |
@patch("auditwheel.libc.Path") |
| 23 |
def test_find_musllinux_found(path_mock): |
| 24 |
path_mock.return_value.glob.return_value = ["/lib/ld-musl-dummy.so.1"] |
| 25 |
musl = _find_musl_libc() |
| 26 |
assert str(musl) == "/lib/ld-musl-dummy.so.1" |
| 27 |
assert Libc.detect() == Libc.MUSL |
| 28 |
|
| 29 |
|
| 30 |
def test_get_musl_version_invalid_path(): |
| 31 |
with pytest.raises(InvalidLibcError): |
| 32 |
_get_musl_version(Path("/tmp/no/executable/here")) # noqa: S108 |
| 33 |
|
| 34 |
|
| 35 |
@patch("auditwheel.libc.subprocess.run") |
| 36 |
def test_get_musl_version_invalid_version(run_mock): |
| 37 |
run_mock.return_value = subprocess.CompletedProcess([], 1, None, "Version 1.1") |
| 38 |
with pytest.raises(InvalidLibcError): |
| 39 |
_get_musl_version(Path("anything")) |
| 40 |
|
| 41 |
|
| 42 |
@patch("auditwheel.libc.subprocess.run") |
| 43 |
def test_get_musl_version_valid_version(run_mock): |
| 44 |
run_mock.return_value = subprocess.CompletedProcess([], 1, None, "Version 5.6.7") |
| 45 |
version = _get_musl_version(Path("anything")) |
| 46 |
assert version.major == 5 |
| 47 |
assert version.minor == 6 |
| 48 |
|
| 49 |
|
| 50 |
@patch("auditwheel.libc.Path") |
| 51 |
def test_detect_glibc(path_mock): |
| 52 |
path_mock.return_value.glob.return_value = [] |
| 53 |
assert Libc.detect() == Libc.GLIBC |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.parametrize( |
| 57 |
"confstr", |
| 58 |
[ |
| 59 |
"glibc 42.42", |
| 60 |
"glibc 42.42-test", |
| 61 |
"glibc 42.42.0", |
| 62 |
"glibc 42.42~0", |
| 63 |
], |
| 64 |
) |
| 65 |
def test_glibc_version(monkeypatch, confstr): |
| 66 |
monkeypatch.setattr(os, "confstr", lambda _: confstr) |
| 67 |
assert Libc.GLIBC.get_current_version() == LibcVersion(42, 42) |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
"confstr", |
| 72 |
[ |
| 73 |
None, |
| 74 |
"glibc", |
| 75 |
"glibc 42.42 test", |
| 76 |
"glibc 42", |
| 77 |
"glibc 42.test", |
| 78 |
"glibc test.42", |
| 79 |
], |
| 80 |
) |
| 81 |
def test_bad_glibc_version(monkeypatch, confstr): |
| 82 |
monkeypatch.setattr(os, "confstr", lambda _: confstr) |
| 83 |
with pytest.raises(InvalidLibcError): |
| 84 |
Libc.GLIBC.get_current_version() |
| 85 |
|
| 86 |
|
| 87 |
# Android is cross-compiled, so autodetection is not possible. |
| 88 |
def test_android_version(): |
| 89 |
with pytest.raises(InvalidLibcError, match=r"can't determine version of libc 'android'"): |
| 90 |
Libc.ANDROID.get_current_version() |
| 91 |
|
| 92 |
|
| 93 |
@pytest.mark.parametrize( |
| 94 |
("libc", "expected"), |
| 95 |
[ |
| 96 |
(Libc.GLIBC, "manylinux"), |
| 97 |
(Libc.MUSL, "musllinux"), |
| 98 |
(Libc.ANDROID, "android"), |
| 99 |
], |
| 100 |
) |
| 101 |
def test_tag_prefix(libc, expected): |
| 102 |
assert libc.tag_prefix == expected |