|
| 1 | +# Copyright 2024 Pex project contributors. |
| 2 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 3 | + |
| 4 | +from __future__ import absolute_import |
| 5 | + |
| 6 | +import os.path |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import tarfile |
| 10 | +from textwrap import dedent |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from pex.common import is_exe, safe_open |
| 15 | +from pex.compatibility import urlparse |
| 16 | +from pex.fetcher import URLFetcher |
| 17 | +from pex.pep_440 import Version |
| 18 | +from pex.pip.version import PipVersion |
| 19 | +from pex.typing import TYPE_CHECKING |
| 20 | +from testing import IS_LINUX, run_pex_command |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + from typing import Any |
| 24 | + |
| 25 | + |
| 26 | +# TODO(John Sirois): Include a test of >= Pip 24.2 when Pex adds support for it. |
| 27 | +# See: https://github.com/pex-tool/pex/issues/2471 |
| 28 | +@pytest.mark.skipif( |
| 29 | + PipVersion.DEFAULT > PipVersion.VENDORED and PipVersion.DEFAULT.version < Version("24.2"), |
| 30 | + reason=( |
| 31 | + "Although Pex's vendored Pip is patched to handle statically linked musl libc CPython, no " |
| 32 | + "version of Pip Pex supports handles these Pythons until Pip 24.2" |
| 33 | + ), |
| 34 | +) |
| 35 | +@pytest.mark.skipif( |
| 36 | + not IS_LINUX, |
| 37 | + reason="This test tests statically linked musl libc CPython which is only available for Linux.", |
| 38 | +) |
| 39 | +def test_statically_linked_musl_libc_cpython_support(tmpdir): |
| 40 | + # type: (Any) -> None |
| 41 | + |
| 42 | + pbs_distribution_url = ( |
| 43 | + "https://github.com/indygreg/python-build-standalone/releases/download/20221220/" |
| 44 | + "cpython-3.10.9+20221220-x86_64_v3-unknown-linux-musl-install_only.tar.gz" |
| 45 | + ) |
| 46 | + pbs_distribution = os.path.join( |
| 47 | + str(tmpdir), |
| 48 | + os.path.basename(urlparse.urlparse(pbs_distribution_url).path), |
| 49 | + ) |
| 50 | + with URLFetcher().get_body_stream(pbs_distribution_url) as read_fp, open( |
| 51 | + pbs_distribution, "wb" |
| 52 | + ) as write_fp: |
| 53 | + shutil.copyfileobj(read_fp, write_fp) |
| 54 | + with tarfile.open(pbs_distribution) as tf: |
| 55 | + tf.extractall(str(tmpdir)) |
| 56 | + statically_linked_musl_libc_cpython = os.path.join(str(tmpdir), "python", "bin", "python3") |
| 57 | + assert is_exe(statically_linked_musl_libc_cpython) |
| 58 | + |
| 59 | + pex = os.path.join(str(tmpdir), "pex") |
| 60 | + run_pex_command( |
| 61 | + args=["fortune==1.1.1", "-c", "fortune", "-o", pex], |
| 62 | + python=statically_linked_musl_libc_cpython, |
| 63 | + ).assert_success() |
| 64 | + |
| 65 | + fortune_db = os.path.join(str(tmpdir), "fortunes") |
| 66 | + with safe_open(fortune_db, "w") as fp: |
| 67 | + fp.write( |
| 68 | + dedent( |
| 69 | + """\ |
| 70 | + A day for firm decisions!!!!! Or is it? |
| 71 | + % |
| 72 | + """ |
| 73 | + ) |
| 74 | + ) |
| 75 | + output = subprocess.check_output(args=[pex, fortune_db]) |
| 76 | + assert b"A day for firm decisions!!!!! Or is it?\n" == output, output.decode("utf-8") |
0 commit comments