Skip to content

Commit

Permalink
Make docker_environment's Pants-provided-PBS an absolute path (pantsb…
Browse files Browse the repository at this point in the history
…uild#20314)

Fixes pantsbuild#20313 by ensuring that the `PEX_PYTHON` used for Pex CLI
invocations inside a docker environment is an absolute path.

I also did some drive-by improvements of the test code.
  • Loading branch information
thejcannon authored Dec 26, 2023
1 parent 54ec52a commit 550cc24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/python/pants/core/util_rules/adhoc_binaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ async def download_python_binary(
cp -r python "{installation_root}"
touch "{installation_root}/DONE"
fi
echo "$(realpath "{installation_root}")/bin/python3"
"""
)

await Get(
result = await Get(
ProcessResult,
Process(
[bash_binary.path, "-c", installation_script],
Expand All @@ -123,7 +124,7 @@ async def download_python_binary(
),
)

return _PythonBuildStandaloneBinary(f"{installation_root}/bin/python3")
return _PythonBuildStandaloneBinary(result.stdout.decode().splitlines()[-1].strip())


@dataclass(frozen=True)
Expand Down
42 changes: 21 additions & 21 deletions src/python/pants/core/util_rules/adhoc_binaries_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import pytest

from pants.build_graph.address import Address
from pants.core.target_types import FileTarget
from pants.core.util_rules import adhoc_binaries
from pants.core.util_rules.adhoc_binaries import (
PythonBuildStandaloneBinary,
_DownloadPythonBuildStandaloneBinaryRequest,
_PythonBuildStandaloneBinary,
)
from pants.core.util_rules.environments import EnvironmentTarget, LocalEnvironmentTarget
from pants.core.util_rules.environments import (
DockerEnvironmentTarget,
EnvironmentTarget,
LocalEnvironmentTarget,
)
from pants.engine.environment import EnvironmentName
from pants.testutil.rule_runner import MockGet, QueryRule, RuleRunner, run_rule_with_mocks


Expand All @@ -27,7 +30,7 @@ def rule_runner() -> RuleRunner:
[_DownloadPythonBuildStandaloneBinaryRequest],
),
],
target_types=[LocalEnvironmentTarget, FileTarget],
target_types=[LocalEnvironmentTarget, DockerEnvironmentTarget],
)


Expand All @@ -47,32 +50,29 @@ def test_local(env_tgt) -> None:
assert result == adhoc_binaries.PythonBuildStandaloneBinary(sys.executable)


def test_docker_uses_helper() -> None:
result = run_rule_with_mocks(
adhoc_binaries.get_python_for_scripts,
rule_args=[EnvironmentTarget("docker", FileTarget({"source": ""}, address=Address("")))],
mock_gets=[
MockGet(
output_type=_PythonBuildStandaloneBinary,
input_types=(_DownloadPythonBuildStandaloneBinaryRequest,),
mock=lambda _: _PythonBuildStandaloneBinary(""),
)
def test_docker_uses_helper(rule_runner: RuleRunner) -> None:
rule_runner = RuleRunner(
rules=[
*adhoc_binaries.rules(),
QueryRule(
_PythonBuildStandaloneBinary,
[_DownloadPythonBuildStandaloneBinaryRequest],
),
],
target_types=[DockerEnvironmentTarget],
inherent_environment=EnvironmentName("docker"),
)
assert result == PythonBuildStandaloneBinary("")


def test_docker_helper(rule_runner: RuleRunner):
rule_runner.write_files(
{
"BUILD": "local_environment(name='local')",
"BUILD": "docker_environment(name='docker', image='ubuntu:latest')",
}
)
rule_runner.set_options(
["--environments-preview-names={'local': '//:local'}"], env_inherit={"PATH"}
["--environments-preview-names={'docker': '//:docker'}"], env_inherit={"PATH"}
)
pbs = rule_runner.request(
_PythonBuildStandaloneBinary,
[_DownloadPythonBuildStandaloneBinaryRequest()],
)
assert not pbs.path.startswith("/")
assert pbs.path.startswith("/pants-named-caches")
assert pbs.path.endswith("/bin/python3")

0 comments on commit 550cc24

Please sign in to comment.