|
| 1 | +# Copyright 2025 Pex project contributors. |
| 2 | +# Licensed under the Apache License, Version 2.0 (see LICENSE). |
| 3 | + |
| 4 | +from __future__ import absolute_import |
| 5 | + |
| 6 | +import subprocess |
| 7 | +from textwrap import dedent |
| 8 | + |
| 9 | +from pex.typing import TYPE_CHECKING |
| 10 | +from testing import run_pex_command |
| 11 | +from testing.pytest_utils.tmp import Tempdir |
| 12 | + |
| 13 | +if TYPE_CHECKING: |
| 14 | + import colors # vendor:skip |
| 15 | +else: |
| 16 | + from pex.third_party import colors |
| 17 | + |
| 18 | + |
| 19 | +def build_cyan_penguin_pex_assert_only_cowsay_built( |
| 20 | + tmpdir, # type: Tempdir |
| 21 | + *extra_args # type: str |
| 22 | +): |
| 23 | + # type: (...) -> None |
| 24 | + |
| 25 | + with open(tmpdir.join("exe.py"), "w") as exe_fp: |
| 26 | + exe_fp.write( |
| 27 | + dedent( |
| 28 | + """\ |
| 29 | + # /// script |
| 30 | + # dependencies = [ |
| 31 | + # "ansicolors", |
| 32 | + # "cowsay<6", |
| 33 | + # ] |
| 34 | + # /// |
| 35 | +
|
| 36 | + import sys |
| 37 | +
|
| 38 | + import colors |
| 39 | + import cowsay |
| 40 | +
|
| 41 | +
|
| 42 | + cowsay.tux(colors.cyan(" ".join(sys.argv[1:]))) |
| 43 | + """ |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + pex_root = tmpdir.join("pex-root") |
| 48 | + pex = tmpdir.join("pex") |
| 49 | + pip_log = tmpdir.join("pip.log") |
| 50 | + run_pex_command( |
| 51 | + args=[ |
| 52 | + "--pex-root", |
| 53 | + pex_root, |
| 54 | + "--runtime-pex-root", |
| 55 | + pex_root, |
| 56 | + "--pip-log", |
| 57 | + pip_log, |
| 58 | + "--exe", |
| 59 | + exe_fp.name, |
| 60 | + "-o", |
| 61 | + pex, |
| 62 | + ] |
| 63 | + + list(extra_args) |
| 64 | + ).assert_success() |
| 65 | + |
| 66 | + assert "| {message} |".format(message=colors.cyan("Moo?")) in subprocess.check_output( |
| 67 | + args=[pex, "Moo?"] |
| 68 | + ).decode("utf-8") |
| 69 | + |
| 70 | + with open(pip_log) as fp: |
| 71 | + pip_source_log_lines = [line for line in fp if "Source in " in line] |
| 72 | + assert 1 == len(pip_source_log_lines), "Should have built 1 distribution." |
| 73 | + assert "which satisfies requirement cowsay<6" in pip_source_log_lines[0], pip_source_log_lines[ |
| 74 | + 0 |
| 75 | + ] |
| 76 | + |
| 77 | + |
| 78 | +def test_no_build_exception_allowed(tmpdir): |
| 79 | + # type: (Tempdir) -> None |
| 80 | + |
| 81 | + build_cyan_penguin_pex_assert_only_cowsay_built(tmpdir, "--no-build", "--only-build", "cowsay") |
| 82 | + |
| 83 | + |
| 84 | +def test_no_wheel_exception_allowed(tmpdir): |
| 85 | + # type: (Tempdir) -> None |
| 86 | + |
| 87 | + # Under modern versions of Pip, setuptools may also be built from source as part of setting up |
| 88 | + # the PEP-517 build environment for cowsay; so we prevent this to avoid complications in the |
| 89 | + # assertion. |
| 90 | + build_cyan_penguin_pex_assert_only_cowsay_built( |
| 91 | + tmpdir, "--no-wheel", "--only-wheel", "ansicolors", "--only-wheel", "setuptools" |
| 92 | + ) |
0 commit comments