Beispiel #1
0
def test_inject_dependencies() -> None:
    rule_runner = RuleRunner(
        rules=[
            *python_protobuf_subsystem.rules(),
            QueryRule(InjectedDependencies, (InjectPythonProtobufDependencies,)),
        ],
        target_types=[ProtobufLibrary, Files],
    )
    rule_runner.set_options(
        [
            "--backend-packages=pants.backend.codegen.protobuf.python",
            "--python-protobuf-runtime-dependencies=protos:injected_dep",
        ]
    )
    # Note that injected deps can be any target type for `--python-protobuf-runtime-dependencies`.
    rule_runner.add_to_build_file(
        "protos",
        dedent(
            """\
            protobuf_library()
            files(name="injected_dep", sources=[])
            """
        ),
    )
    tgt = rule_runner.get_target(Address("protos"))
    injected = rule_runner.request(
        InjectedDependencies, [InjectPythonProtobufDependencies(tgt[ProtobufDependencies])]
    )
    assert injected == InjectedDependencies([Address("protos", target_name="injected_dep")])
Beispiel #2
0
def rules():
    return [
        *additional_fields.rules(),
        *python_protobuf_subsystem.rules(),
        *python_rules(),
        *target_rules(),
    ]
Beispiel #3
0
def test_inject_dependencies() -> None:
    rule_runner = RuleRunner(
        rules=[
            *python_protobuf_subsystem.rules(),
            *target_types.rules(),
            QueryRule(InjectedDependencies,
                      (InjectPythonProtobufDependencies, )),
        ],
        target_types=[ProtobufSourcesGeneratorTarget, GenericTarget],
    )
    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": "",
    })

    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")])

    assert_injected(Address("protos"))
    assert_injected(Address("protos", relative_file_path="f.proto"))
Beispiel #4
0
def rules():
    return [
        *additional_fields.rules(),
        *python_protobuf_subsystem.rules(),
        *python_rules(),
        *python_protobuf_module_mapper.rules(),
        *protobuf_dependency_inference.rules(),
        *export_codegen_goal.rules(),
    ]
Beispiel #5
0
def rules():
    return [
        *additional_fields.rules(),
        *python_protobuf_subsystem.rules(),
        *python_rules(),
        *python_protobuf_module_mapper.rules(),
        *protobuf_dependency_inference.rules(),
        *protobuf_tailor.rules(),
        *export_codegen_goal.rules(),
        *protobuf_target_rules(),
        *module_mapper.rules(),
        *stripped_source_files.rules(),
    ]
Beispiel #6
0
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, PythonRequirementTarget],
    )

    rule_runner.write_files({
        "codegen/dir/f.proto": "",
        "codegen/dir/BUILD": "protobuf_sources(grpc=True)"
    })
    rule_runner.set_options([
        "--python-resolves={'python-default': '', 'another': ''}",
        "--python-enable-resolves"
    ])
    proto_tgt = rule_runner.get_target(
        Address("codegen/dir", relative_file_path="f.proto"))
    request = InjectPythonProtobufDependencies(proto_tgt[Dependencies])

    # 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")])

    # Multiple is fine if from other resolve.
    rule_runner.write_files({
        "another_resolve/BUILD":
        ("python_requirement(name='r1', requirements=['protobuf'], resolve='another')\n"
         "python_requirement(name='r2', requirements=['grpc'], resolve='another')\n"
         )
    })
    assert rule_runner.request(InjectedDependencies,
                               [request]) == InjectedDependencies(
                                   [Address("proto1"),
                                    Address("grpc1")])

    # If multiple from the same resolve, 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])