Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RuleRunner.set_options() #10859

Merged
merged 3 commits into from Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,7 +16,6 @@
from pants.core.util_rules.pants_environment import PantsEnvironment
from pants.engine.addresses import Address
from pants.engine.fs import DigestContents
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import QueryRule, RuleRunner


Expand All @@ -32,16 +31,16 @@ def rule_runner() -> RuleRunner:


def create_python_awslambda(rule_runner: RuleRunner, addr: str) -> Tuple[str, bytes]:
bootstrapper = create_options_bootstrapper(
args=[
rule_runner.set_options(
[
"--backend-packages=pants.backend.awslambda.python",
"--source-root-patterns=src/python",
"--pants-distdir-legacy-paths=false",
]
)
target = rule_runner.get_target(Address.parse(addr), bootstrapper)
target = rule_runner.get_target(Address.parse(addr))
created_awslambda = rule_runner.request(
CreatedAWSLambda, [PythonAwsLambdaFieldSet.create(target), bootstrapper, PantsEnvironment()]
CreatedAWSLambda, [PythonAwsLambdaFieldSet.create(target), PantsEnvironment()]
)
created_awslambda_digest_contents = rule_runner.request(
DigestContents, [created_awslambda.digest]
Expand Down
21 changes: 8 additions & 13 deletions src/python/pants/backend/codegen/protobuf/target_types_test.py
Expand Up @@ -12,19 +12,23 @@
from pants.core.target_types import Files
from pants.engine.addresses import Address
from pants.engine.target import InjectedDependencies
from pants.option.options_bootstrapper import OptionsBootstrapper
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import QueryRule, RuleRunner


def test_inject_dependencies() -> None:
rule_runner = RuleRunner(
rules=[
*target_type_rules(),
QueryRule(InjectedDependencies, (InjectProtobufDependencies, OptionsBootstrapper)),
QueryRule(InjectedDependencies, (InjectProtobufDependencies,)),
],
target_types=[ProtobufLibrary, Files],
)
rule_runner.set_options(
[
"--backend-packages=pants.backend.codegen.protobuf.python",
"--protoc-runtime-targets=protos:injected_dep",
]
)
# Note that injected deps can be any target type for `--protobuf-runtime-targets`.
rule_runner.add_to_build_file(
"protos",
Expand All @@ -37,15 +41,6 @@ def test_inject_dependencies() -> None:
)
tgt = rule_runner.get_target(Address("protos"))
injected = rule_runner.request(
InjectedDependencies,
[
InjectProtobufDependencies(tgt[ProtobufDependencies]),
create_options_bootstrapper(
args=[
"--backend-packages=pants.backend.codegen.protobuf.python",
"--protoc-runtime-targets=protos:injected_dep",
]
),
],
InjectedDependencies, [InjectProtobufDependencies(tgt[ProtobufDependencies])]
)
assert injected == InjectedDependencies([Address("protos", target_name="injected_dep")])
Expand Up @@ -17,7 +17,6 @@
from pants.backend.python.target_types import PythonLibrary, PythonRequirementLibrary
from pants.core.util_rules import stripped_source_files
from pants.engine.addresses import Address
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import QueryRule, RuleRunner
from pants.util.frozendict import FrozenDict

Expand Down Expand Up @@ -83,8 +82,8 @@ def rule_runner() -> RuleRunner:


def test_map_first_party_modules_to_addresses(rule_runner: RuleRunner) -> None:
options_bootstrapper = create_options_bootstrapper(
args=["--source-root-patterns=['src/python', 'tests/python', 'build-support']"]
rule_runner.set_options(
["--source-root-patterns=['src/python', 'tests/python', 'build-support']"]
)
# Two modules belonging to the same target. We should generate subtargets for each file.
rule_runner.create_files("src/python/project/util", ["dirutil.py", "tarutil.py"])
Expand All @@ -98,7 +97,7 @@ def test_map_first_party_modules_to_addresses(rule_runner: RuleRunner) -> None:
# not generate subtargets.
rule_runner.create_file("tests/python/project_test/demo_test/__init__.py")
rule_runner.add_to_build_file("tests/python/project_test/demo_test", "python_library()")
result = rule_runner.request(FirstPartyModuleToAddressMapping, [options_bootstrapper])
result = rule_runner.request(FirstPartyModuleToAddressMapping, [])
assert result.mapping == FrozenDict(
{
"project.util.dirutil": Address(
Expand Down Expand Up @@ -150,7 +149,7 @@ def test_map_third_party_modules_to_addresses(rule_runner: RuleRunner) -> None:
"""
),
)
result = rule_runner.request(ThirdPartyModuleToAddressMapping, [create_options_bootstrapper()])
result = rule_runner.request(ThirdPartyModuleToAddressMapping, [])
assert result.mapping == FrozenDict(
{
"colors": Address("3rdparty/python", target_name="ansicolors"),
Expand All @@ -163,14 +162,10 @@ def test_map_third_party_modules_to_addresses(rule_runner: RuleRunner) -> None:


def test_map_module_to_address(rule_runner: RuleRunner) -> None:
options_bootstrapper = create_options_bootstrapper(
args=["--source-root-patterns=['source_root1', 'source_root2', '/']"]
)
rule_runner.set_options(["--source-root-patterns=['source_root1', 'source_root2', '/']"])

def get_owner(module: str) -> Optional[Address]:
return rule_runner.request(
PythonModuleOwner, [PythonModule(module), options_bootstrapper]
).address
return rule_runner.request(PythonModuleOwner, [PythonModule(module)]).address

# First check that we can map 3rd-party modules.
rule_runner.add_to_build_file(
Expand Down
26 changes: 11 additions & 15 deletions src/python/pants/backend/python/dependency_inference/rules_test.py
Expand Up @@ -24,7 +24,6 @@
from pants.engine.addresses import Address
from pants.engine.rules import SubsystemRule
from pants.engine.target import InferredDependencies
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import QueryRule, RuleRunner


Expand Down Expand Up @@ -89,11 +88,11 @@ def run_dep_inference(
args = ["--backend-packages=pants.backend.python", "--source-root-patterns=src/python"]
if enable_string_imports:
args.append("--python-infer-string-imports")
options_bootstrapper = create_options_bootstrapper(args=args)
target = rule_runner.get_target(address, options_bootstrapper)
rule_runner.set_options(args)
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferPythonDependencies(target[PythonSources]), options_bootstrapper],
[InferPythonDependencies(target[PythonSources])],
)

normal_address = Address("src/python")
Expand Down Expand Up @@ -134,8 +133,8 @@ def test_infer_python_inits() -> None:
],
target_types=[PythonLibrary],
)
options_bootstrapper = create_options_bootstrapper(
args=[
rule_runner.set_options(
[
"--backend-packages=pants.backend.python",
"--python-infer-inits",
"--source-root-patterns=src/python",
Expand All @@ -152,10 +151,10 @@ def test_infer_python_inits() -> None:
rule_runner.add_to_build_file("src/python/root/mid/leaf", "python_library()")

def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address, options_bootstrapper)
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferInitDependencies(target[PythonSources]), options_bootstrapper],
[InferInitDependencies(target[PythonSources])],
)

assert run_dep_inference(Address.parse("src/python/root/mid/leaf")) == InferredDependencies(
Expand All @@ -177,11 +176,8 @@ def test_infer_python_conftests() -> None:
],
target_types=[PythonTests],
)
options_bootstrapper = create_options_bootstrapper(
args=[
"--backend-packages=pants.backend.python",
"--source-root-patterns=src/python",
]
rule_runner.set_options(
["--backend-packages=pants.backend.python", "--source-root-patterns=src/python"]
)

rule_runner.create_file("src/python/root/conftest.py")
Expand All @@ -195,10 +191,10 @@ def test_infer_python_conftests() -> None:
rule_runner.add_to_build_file("src/python/root/mid/leaf", "python_tests()")

def run_dep_inference(address: Address) -> InferredDependencies:
target = rule_runner.get_target(address, options_bootstrapper)
target = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferConftestDependencies(target[PythonSources]), options_bootstrapper],
[InferConftestDependencies(target[PythonSources])],
)

assert run_dep_inference(Address.parse("src/python/root/mid/leaf")) == InferredDependencies(
Expand Down
Expand Up @@ -27,7 +27,6 @@
from pants.engine.addresses import Address
from pants.engine.fs import DigestContents, FileContent
from pants.engine.process import InteractiveRunner
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.python_interpreter_selection import skip_unless_python27_and_python3_present
from pants.testutil.rule_runner import QueryRule, RuleRunner

Expand Down Expand Up @@ -164,13 +163,11 @@ def run_pytest(
args.append("--test-use-coverage")
if execution_slot_var:
args.append(f"--pytest-execution-slot-var={execution_slot_var}")
subjects = [
PythonTestFieldSet.create(test_target),
pants_environment,
create_options_bootstrapper(args=args),
]
test_result = rule_runner.request(TestResult, subjects)
debug_request = rule_runner.request(TestDebugRequest, subjects)
rule_runner.set_options(args)

inputs = [PythonTestFieldSet.create(test_target), pants_environment]
test_result = rule_runner.request(TestResult, inputs)
debug_request = rule_runner.request(TestDebugRequest, inputs)
if debug_request.process is not None:
debug_result = InteractiveRunner(rule_runner.scheduler).run(debug_request.process)
assert test_result.exit_code == debug_result.exit_code
Expand Down
24 changes: 7 additions & 17 deletions src/python/pants/backend/python/goals/setup_py_test.py
Expand Up @@ -47,7 +47,6 @@
from pants.engine.rules import rule
from pants.engine.target import Targets
from pants.engine.unions import UnionRule
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.rule_runner import QueryRule, RuleRunner

_namespace_decl = "__import__('pkg_resources').declare_namespace(__name__)"
Expand Down Expand Up @@ -106,10 +105,7 @@ def assert_chroot(
tgt = rule_runner.get_target(Address.parse(addr))
chroot = rule_runner.request(
SetupPyChroot,
[
SetupPyChrootRequest(ExportedTarget(tgt), py2=False),
create_options_bootstrapper(),
],
[SetupPyChrootRequest(ExportedTarget(tgt), py2=False)],
)
snapshot = rule_runner.request(Snapshot, [chroot.digest])
assert sorted(expected_files) == sorted(snapshot.files)
Expand All @@ -121,10 +117,7 @@ def assert_chroot_error(rule_runner: RuleRunner, addr: str, exc_cls: Type[Except
with pytest.raises(ExecutionError) as excinfo:
rule_runner.request(
SetupPyChroot,
[
SetupPyChrootRequest(ExportedTarget(tgt), py2=False),
create_options_bootstrapper(),
],
[SetupPyChrootRequest(ExportedTarget(tgt), py2=False)],
)
ex = excinfo.value
assert len(ex.wrapped_exceptions) == 1
Expand Down Expand Up @@ -293,7 +286,7 @@ def assert_sources(
targets = Targets(rule_runner.get_target(Address.parse(addr)) for addr in addrs)
srcs = rule_runner.request(
SetupPySources,
[SetupPySourcesRequest(targets, py2=False), create_options_bootstrapper()],
[SetupPySourcesRequest(targets, py2=False)],
)
chroot_snapshot = rule_runner.request(Snapshot, [srcs.digest])

Expand Down Expand Up @@ -440,7 +433,7 @@ def assert_requirements(expected_req_strs, addr):
tgt = rule_runner.get_target(Address.parse(addr))
reqs = rule_runner.request(
ExportedTargetRequirements,
[DependencyOwner(ExportedTarget(tgt)), create_options_bootstrapper()],
[DependencyOwner(ExportedTarget(tgt))],
)
assert sorted(expected_req_strs) == list(reqs)

Expand Down Expand Up @@ -514,10 +507,7 @@ def assert_owned(owned: Iterable[str], exported: str):
od.target.address.spec
for od in rule_runner.request(
OwnedDependencies,
[
DependencyOwner(ExportedTarget(tgt)),
create_options_bootstrapper(),
],
[DependencyOwner(ExportedTarget(tgt))],
)
)

Expand Down Expand Up @@ -553,7 +543,7 @@ def assert_is_owner(rule_runner: RuleRunner, owner: str, owned: str):
owner
== rule_runner.request(
ExportedTarget,
[OwnedDependency(tgt), create_options_bootstrapper()],
[OwnedDependency(tgt)],
).target.address.spec
)

Expand All @@ -563,7 +553,7 @@ def assert_owner_error(rule_runner, owned: str, exc_cls: Type[Exception]):
with pytest.raises(ExecutionError) as excinfo:
rule_runner.request(
ExportedTarget,
[OwnedDependency(tgt), create_options_bootstrapper()],
[OwnedDependency(tgt)],
)
ex = excinfo.value
assert len(ex.wrapped_exceptions) == 1
Expand Down
Expand Up @@ -13,7 +13,6 @@
from pants.engine.addresses import Address
from pants.engine.fs import DigestContents, FileContent
from pants.engine.target import Target
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.python_interpreter_selection import skip_unless_python27_and_python3_present
from pants.testutil.rule_runner import QueryRule, RuleRunner

Expand Down Expand Up @@ -67,13 +66,10 @@ def run_bandit(
args.append("--bandit-skip")
if additional_args:
args.extend(additional_args)
rule_runner.set_options(args)
results = rule_runner.request(
LintResults,
[
BanditRequest(BanditFieldSet.create(tgt) for tgt in targets),
create_options_bootstrapper(args=args),
PantsEnvironment(),
],
[BanditRequest(BanditFieldSet.create(tgt) for tgt in targets), PantsEnvironment()],
)
return results.results

Expand Down
Expand Up @@ -16,7 +16,6 @@
from pants.engine.addresses import Address
from pants.engine.fs import CreateDigest, Digest, FileContent
from pants.engine.target import Target
from pants.testutil.option_util import create_options_bootstrapper
from pants.testutil.python_interpreter_selection import skip_unless_python38_present
from pants.testutil.rule_runner import QueryRule, RuleRunner

Expand Down Expand Up @@ -73,25 +72,21 @@ def run_black(
args.append(f"--black-args='{passthrough_args}'")
if skip:
args.append("--black-skip")
options_bootstrapper = create_options_bootstrapper(args=args)
rule_runner.set_options(args)
field_sets = [BlackFieldSet.create(tgt) for tgt in targets]
pants_env = PantsEnvironment()
lint_results = rule_runner.request(
LintResults, [BlackRequest(field_sets), options_bootstrapper, pants_env]
)
lint_results = rule_runner.request(LintResults, [BlackRequest(field_sets), pants_env])
input_sources = rule_runner.request(
SourceFiles,
[
SourceFilesRequest(field_set.sources for field_set in field_sets),
options_bootstrapper,
pants_env,
],
)
fmt_result = rule_runner.request(
FmtResult,
[
BlackRequest(field_sets, prior_formatter_result=input_sources.snapshot),
options_bootstrapper,
pants_env,
],
)
Expand Down