Skip to content

Commit

Permalink
A RuleRunner subclass to set ICs in Python-related tests. (#18709)
Browse files Browse the repository at this point in the history
Subsequent PRs will apply this in relevant tests, so we can remove
one of the hacks introduced in #18627.

I have applied this to several tests and verified that it works.
  • Loading branch information
benjyw authored Apr 10, 2023
1 parent 8b59d45 commit 72a08df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 26 additions & 0 deletions src/python/pants/testutil/python_rule_runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from typing import Iterable, Mapping

from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.testutil.rule_runner import RuleRunner


class PythonRuleRunner(RuleRunner):
"""Set common python rule-specific options."""

def create_options_bootstrapper(
self, args: Iterable[str], env: Mapping[str, str] | None
) -> OptionsBootstrapper:
env = dict(env) if env else {}
if (
not any("--python-interpreter-constraints=" in arg for arg in args)
and "PANTS_PYTHON_INTERPRETER_CONSTRAINTS" not in env
):
# We inject the test ICs via env and not args, because in a handful of cases
# we have different behavior when the option is set via flag.
env["PANTS_PYTHON_INTERPRETER_CONSTRAINTS"] = "['>=3.7,<3.10',]"
return super().create_options_bootstrapper(args=args, env=env)
9 changes: 7 additions & 2 deletions src/python/pants/testutil/rule_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def rewrite_rule_for_inherent_environment(rule):
self.build_config = build_config_builder.create()

self.environment = CompleteEnvironmentVars({})
self.options_bootstrapper = create_options_bootstrapper(args=bootstrap_args)
self.options_bootstrapper = self.create_options_bootstrapper(args=bootstrap_args, env=None)
self.extra_session_values = extra_session_values or {}
self.inherent_environment = inherent_environment
self.max_workunit_verbosity = max_workunit_verbosity
Expand Down Expand Up @@ -442,6 +442,11 @@ def run_goal_rule(
console.flush()
return GoalRuleResult(exit_code, stdout.getvalue(), stderr.getvalue())

def create_options_bootstrapper(
self, args: Iterable[str], env: Mapping[str, str] | None
) -> OptionsBootstrapper:
return create_options_bootstrapper(args=args, env=env)

def set_options(
self,
args: Iterable[str],
Expand All @@ -465,7 +470,7 @@ def set_options(
**{k: os.environ[k] for k in (env_inherit or set()) if k in os.environ},
**(env or {}),
}
self.options_bootstrapper = create_options_bootstrapper(args=args, env=env)
self.options_bootstrapper = self.create_options_bootstrapper(args=args, env=env)
self.environment = CompleteEnvironmentVars(env)
self._set_new_session(self.scheduler.scheduler)

Expand Down

0 comments on commit 72a08df

Please sign in to comment.