forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
[python-protobuf].runtime_dependencies
in favor of Pants dis…
…covering the dependency (pantsbuild#14691) @tdyas had the idea that rather than you having to tell Pants where to load `protobuf`, `grpcio`, and `thrift`, we can simply discover it! We do this for Scala already and it works well. There's minimal performance hit because we already will have created a third-party module mapping. Two motivations: 1. Ergonomics. 2. Unblock codegen supporting multiple resolves: pantsbuild#14484. Otherwise we would need to add `[python-protobuf].runtime_dependencies_per_resolve`, which violates our goal for resolves to not make things more complex for the average user who only wants one resolve. Note that this was already deprecated in 2.10, so we can outright remove here. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
- Loading branch information
1 parent
f14359e
commit 5344b97
Showing
12 changed files
with
259 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 40 additions & 23 deletions
63
src/python/pants/backend/codegen/protobuf/python/python_protobuf_subsystem_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,62 @@ | ||
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from pants.backend.codegen.protobuf import target_types | ||
from pants.backend.codegen.protobuf.python import python_protobuf_subsystem | ||
from pants.backend.codegen.protobuf.python.python_protobuf_subsystem import ( | ||
InjectPythonProtobufDependencies, | ||
) | ||
from pants.backend.codegen.protobuf.target_types import ( | ||
ProtobufDependenciesField, | ||
ProtobufSourcesGeneratorTarget, | ||
from pants.backend.codegen.protobuf.target_types import ProtobufSourcesGeneratorTarget | ||
from pants.backend.codegen.utils import ( | ||
AmbiguousPythonCodegenRuntimeLibrary, | ||
MissingPythonCodegenRuntimeLibrary, | ||
) | ||
from pants.core.target_types import GenericTarget | ||
from pants.backend.python.dependency_inference import module_mapper | ||
from pants.backend.python.target_types import PythonRequirementTarget | ||
from pants.core.util_rules import stripped_source_files | ||
from pants.engine.addresses import Address | ||
from pants.engine.target import InjectedDependencies | ||
from pants.testutil.rule_runner import QueryRule, RuleRunner | ||
from pants.engine.target import Dependencies, InjectedDependencies | ||
from pants.testutil.rule_runner import QueryRule, RuleRunner, engine_error | ||
|
||
|
||
def test_inject_dependencies() -> None: | ||
def test_find_protobuf_python_requirement() -> None: | ||
rule_runner = RuleRunner( | ||
rules=[ | ||
*python_protobuf_subsystem.rules(), | ||
*target_types.rules(), | ||
*module_mapper.rules(), | ||
*stripped_source_files.rules(), | ||
QueryRule(InjectedDependencies, (InjectPythonProtobufDependencies,)), | ||
], | ||
target_types=[ProtobufSourcesGeneratorTarget, GenericTarget], | ||
target_types=[ProtobufSourcesGeneratorTarget, PythonRequirementTarget], | ||
) | ||
rule_runner.set_options(["--python-protobuf-runtime-dependencies=protos:injected_dep"]) | ||
# Note that injected deps can be any target type for `--python-protobuf-runtime-dependencies`. | ||
|
||
rule_runner.write_files( | ||
{ | ||
"protos/BUILD": "protobuf_sources()\ntarget(name='injected_dep')", | ||
"protos/f.proto": "", | ||
} | ||
{"codegen/dir/f.proto": "", "codegen/dir/BUILD": "protobuf_sources(grpc=True)"} | ||
) | ||
proto_tgt = rule_runner.get_target(Address("codegen/dir", relative_file_path="f.proto")) | ||
request = InjectPythonProtobufDependencies(proto_tgt[Dependencies]) | ||
|
||
def assert_injected(addr: Address) -> None: | ||
tgt = rule_runner.get_target(addr) | ||
injected = rule_runner.request( | ||
InjectedDependencies, [InjectPythonProtobufDependencies(tgt[ProtobufDependenciesField])] | ||
) | ||
assert injected == InjectedDependencies([Address("protos", target_name="injected_dep")]) | ||
# Start with no relevant requirements. | ||
with engine_error(MissingPythonCodegenRuntimeLibrary, contains="protobuf"): | ||
rule_runner.request(InjectedDependencies, [request]) | ||
rule_runner.write_files({"proto1/BUILD": "python_requirement(requirements=['protobuf'])"}) | ||
with engine_error(MissingPythonCodegenRuntimeLibrary, contains="grpcio"): | ||
rule_runner.request(InjectedDependencies, [request]) | ||
|
||
# If exactly one, match it. | ||
rule_runner.write_files({"grpc1/BUILD": "python_requirement(requirements=['grpc'])"}) | ||
assert rule_runner.request(InjectedDependencies, [request]) == InjectedDependencies( | ||
[Address("proto1"), Address("grpc1")] | ||
) | ||
|
||
assert_injected(Address("protos")) | ||
assert_injected(Address("protos", relative_file_path="f.proto")) | ||
# If multiple, error. | ||
rule_runner.write_files({"grpc2/BUILD": "python_requirement(requirements=['grpc'])"}) | ||
with engine_error( | ||
AmbiguousPythonCodegenRuntimeLibrary, contains="['grpc1:grpc1', 'grpc2:grpc2']" | ||
): | ||
rule_runner.request(InjectedDependencies, [request]) | ||
rule_runner.write_files({"proto2/BUILD": "python_requirement(requirements=['protobuf'])"}) | ||
with engine_error( | ||
AmbiguousPythonCodegenRuntimeLibrary, contains="['proto1:proto1', 'proto2:proto2']" | ||
): | ||
rule_runner.request(InjectedDependencies, [request]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.