Skip to content

Commit

Permalink
Squash with environment removal.
Browse files Browse the repository at this point in the history
# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]
  • Loading branch information
stuhood committed Mar 5, 2021
1 parent 5c5a756 commit 10fc4e2
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
6 changes: 6 additions & 0 deletions pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ pytest_plugins.add = [
]
timeout_default = 60

[test]
# TODO: These are exposed to tests in order to allow for python interpreter discovery when
# Pants-tests-Pants: in particular, the [python-setup] subsystem consumes them.
# see https://github.com/pantsbuild/pants/issues/11638
extra_env_vars = ["PYENV_ROOT", "HOME", "PATH"]

[coverage-py]
interpreter_constraints = [">=3.7,<3.9"]

Expand Down
4 changes: 3 additions & 1 deletion src/python/pants/backend/python/util_rules/pex_cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def test_custom_ca_certs(rule_runner: RuleRunner) -> None:
with temporary_dir() as tmpdir:
certs_file = Path(tmpdir) / "certsfile"
certs_file.write_text("Some fake cert")
rule_runner.set_options([f"--ca-certs-path={certs_file}"])
rule_runner.set_options(
[f"--ca-certs-path={certs_file}"], env={"PATH": None, "PYENV_ROOT": None, "HOME": None}
)
proc = rule_runner.request(
Process,
[PexCliProcess(argv=["some", "--args"], description="")],
Expand Down
2 changes: 2 additions & 0 deletions src/python/pants/backend/python/util_rules/pex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def create_pex_and_get_all_data(
additional_inputs=additional_inputs,
additional_args=additional_pex_args,
)
env = {**{"PATH": None, "PYENV_ROOT": None, "HOME": None}, **(env or {})}
rule_runner.set_options(
["--backend-packages=pants.backend.python", *additional_pants_args], env=env
)
Expand Down Expand Up @@ -468,6 +469,7 @@ def test_pex_environment(rule_runner: RuleRunner, pex_type: type[Pex | VenvPex])
"--subprocess-environment-env-vars=LANG", # Value should come from environment.
"--subprocess-environment-env-vars=ftp_proxy=dummyproxy",
),
interpreter_constraints=PexInterpreterConstraints(["CPython>=3.6"]),
env={"LANG": "es_PY.UTF-8"},
)

Expand Down
6 changes: 5 additions & 1 deletion src/python/pants/option/global_options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest

from pants.base.build_environment import get_buildroot
from pants.core.util_rules.pants_environment import PantsEnvironment
from pants.init.options_initializer import OptionsInitializer
from pants.option.errors import OptionsError
from pants.option.global_options import ExecutionOptions, GlobalOptions
Expand Down Expand Up @@ -39,7 +40,10 @@ def create_execution_options(
if plugin:
args.append(f"--remote-auth-plugin={plugin}")
ob = create_options_bootstrapper(args)
_build_config, options = OptionsInitializer(ob).build_config_and_options(ob, raise_=False)
pants_env = PantsEnvironment({})
_build_config, options = OptionsInitializer(ob).build_config_and_options(
ob, pants_env, raise_=False
)
return ExecutionOptions.from_options(options)


Expand Down
11 changes: 7 additions & 4 deletions src/python/pants/python/python_setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

import logging
import multiprocessing
import os
Expand Down Expand Up @@ -278,6 +280,9 @@ def get_pyenv_paths(pants_env: PantsEnvironment, *, pyenv_local: bool = False) -
'.python-version' file under `build_root`.
"""
pyenv_root = get_pyenv_root(pants_env)
if not pyenv_root:
return []

versions_dir = Path(pyenv_root, "versions")
if not versions_dir.is_dir():
return []
Expand Down Expand Up @@ -305,14 +310,12 @@ def get_pyenv_paths(pants_env: PantsEnvironment, *, pyenv_local: bool = False) -
return paths


def get_pyenv_root(pants_env: PantsEnvironment) -> str:
def get_pyenv_root(pants_env: PantsEnvironment) -> str | None:
"""See https://github.com/pyenv/pyenv#environment-variables."""
from_env = pants_env.env.get("PYENV_ROOT")
if from_env:
return from_env
home_from_env = pants_env.env.get("HOME")
if home_from_env:
return os.path.join(home_from_env, ".cache")
raise ValueError(
f"The environment {pants_env} does not contain enough information to locate pyenv."
)
return None
3 changes: 1 addition & 2 deletions src/python/pants/python/python_setup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def test_get_pyenv_root() -> None:

assert explicit_root == get_pyenv_root(PantsEnvironment({"PYENV_ROOT": explicit_root}))
assert default_root == get_pyenv_root(PantsEnvironment({"HOME": home}))
with pytest.raises(ValueError):
assert get_pyenv_root(PantsEnvironment({}))
assert get_pyenv_root(PantsEnvironment({})) is None


def test_get_pyenv_paths(rule_runner: RuleRunner) -> None:
Expand Down
14 changes: 10 additions & 4 deletions src/python/pants/testutil/rule_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
build_config_builder.register_target_types(target_types or ())
self.build_config = build_config_builder.create()

self.pants_environment = PantsEnvironment({})
self.options_bootstrapper = create_options_bootstrapper()
options = self.options_bootstrapper.full_options(self.build_config)
global_options = self.options_bootstrapper.bootstrap_options.for_global_scope()
Expand Down Expand Up @@ -148,7 +149,7 @@ def __init__(
session_values=SessionValues(
{
OptionsBootstrapper: self.options_bootstrapper,
PantsEnvironment: PantsEnvironment(),
PantsEnvironment: self.pants_environment,
}
),
)
Expand Down Expand Up @@ -216,23 +217,28 @@ def run_goal_rule(
console.flush()
return GoalRuleResult(exit_code, stdout.getvalue(), stderr.getvalue())

def set_options(self, args: Iterable[str], *, env: Mapping[str, str] | None = None) -> None:
def set_options(
self, args: Iterable[str], *, env: Mapping[str, str | None] | None = None
) -> None:
"""Update the engine session with new options and/or environment variables.
The environment variables will be used to set the `PantsEnvironment`, which is the
environment variables captured by the parent Pants process. Some rules use this to be able
to read arbitrary env vars. Any options that start with `PANTS_` will also be used to set
options.
options. Giving an environment variable a value of `None` will cause it to be propagated from
the test runner's environment (os.environ).
This will override any previously configured values.
"""
env = {k: (v if v is not None else os.getenv(k, "")) for k, v in env.items()} if env else {}
self.options_bootstrapper = create_options_bootstrapper(args=args, env=env)
self.pants_environment = PantsEnvironment(env)
self.scheduler = self.scheduler.scheduler.new_session(
build_id="buildid_for_test",
session_values=SessionValues(
{
OptionsBootstrapper: self.options_bootstrapper,
PantsEnvironment: PantsEnvironment(env),
PantsEnvironment: self.pants_environment,
}
),
)
Expand Down
9 changes: 7 additions & 2 deletions tests/python/pants_test/init/test_plugin_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def rule_runner() -> RuleRunner:


def _create_pex(rule_runner: RuleRunner) -> Pex:
rule_runner.set_options(["--backend-packages=pants.backend.python"])
rule_runner.set_options(
["--backend-packages=pants.backend.python"],
env={"PATH": None, "PYENV_ROOT": None, "HOME": None},
)
request = PexRequest(
output_filename="setup-py-runner.pex",
internal_only=True,
Expand Down Expand Up @@ -157,7 +160,9 @@ def provide_chroot(existing):
bootstrap_scheduler, interpreter_constraints=interpreter_constraints
)
cache_dir = options_bootstrapper.bootstrap_options.for_global_scope().named_caches_dir
pants_env = PantsEnvironment(env)
pants_env = PantsEnvironment(
{**{k: os.getenv(k, "") for k in ["PATH", "HOME", "PYENV_ROOT"]}, **env}
)

working_set = plugin_resolver.resolve(
options_bootstrapper, pants_env, WorkingSet(entries=[])
Expand Down
8 changes: 6 additions & 2 deletions tests/python/pants_test/pantsd/test_pants_daemon_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from pants.core.util_rules.pants_environment import PantsEnvironment
from pants.engine.internals.native_engine import PyExecutor
from pants.pantsd.pants_daemon_core import PantsDaemonCore
from pants.pantsd.service.pants_service import PantsServices
Expand All @@ -13,12 +14,15 @@ def create_services(bootstrap_options, legacy_graph_scheduler):
return PantsServices()

core = PantsDaemonCore(create_options_bootstrapper([]), PyExecutor(2, 4), create_services)
pants_env = PantsEnvironment({})

first_scheduler, first_options_initializer = core.prepare(
create_options_bootstrapper(["-ldebug"])
create_options_bootstrapper(["-ldebug"]),
pants_env,
)
second_scheduler, second_options_initializer = core.prepare(
create_options_bootstrapper(["-lwarn"])
create_options_bootstrapper(["-lwarn"]),
pants_env,
)
assert first_scheduler is not second_scheduler
assert first_options_initializer is second_options_initializer

0 comments on commit 10fc4e2

Please sign in to comment.