Exemplo n.º 1
0
def rules():
    return [
        *collect_rules(),
        *ancestor_files.rules(),
        *source_files.rules(),
        *stripped_source_files.rules(),
    ]
Exemplo n.º 2
0
def rule_runner() -> RuleRunner:
    return RuleRunner(
        rules=[
            *ancestor_files.rules(),
            QueryRule(AncestorFiles, (AncestorFilesRequest,)),
        ]
    )
Exemplo n.º 3
0
def rules():
    return (
        *ancestor_files.rules(),
        *coverage_py.rules(),
        *dependency_inference_rules.rules(),
        *export.rules(),
        *ipython.rules(),
        *local_dists.rules(),
        *lockfile.rules(),
        *package_pex_binary.rules(),
        *pex.rules(),
        *pex_cli.rules(),
        *pex_environment.rules(),
        *pex_from_targets.rules(),
        *pytest.rules(),
        *pytest_runner.rules(),
        *python_native_code.rules(),
        *python_sources.rules(),
        *repl.rules(),
        *run_pex_binary.rules(),
        *setup_py.rules(),
        *setuptools.rules(),
        *tailor.rules(),
        *target_types_rules.rules(),
        # Macros.
        *deprecation_fixers.rules(),
        *pipenv_requirements.rules(),
        *poetry_requirements.rules(),
        *python_requirements.rules(),
    )
Exemplo n.º 4
0
def rules():
    return [
        *import_rules(),
        infer_python_init_dependencies,
        infer_python_conftest_dependencies,
        *ancestor_files.rules(),
        UnionRule(InferDependenciesRequest, InferInitDependencies),
        UnionRule(InferDependenciesRequest, InferConftestDependencies),
    ]
Exemplo n.º 5
0
def rules():
    return [
        *collect_rules(),
        *ancestor_files.rules(),
        *module_mapper.rules(),
        UnionRule(InferDependenciesRequest, InferPythonDependencies),
        UnionRule(InferDependenciesRequest, InferInitDependencies),
        UnionRule(InferDependenciesRequest, InferConftestDependencies),
    ]
Exemplo n.º 6
0
def test_infer_python_conftests() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            *target_types_rules.rules(),
            *core_target_types_rules(),
            infer_python_conftest_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferConftestDependencies,)),
        ],
        target_types=[PythonTestsGeneratorTarget, PythonTestUtilsGeneratorTarget],
        objects={"parametrize": Parametrize},
    )
    rule_runner.set_options(
        [
            "--source-root-patterns=src/python",
            "--python-resolves={'a': '', 'b': ''}",
            "--python-default-resolve=a",
            "--python-enable-resolves",
        ],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    rule_runner.write_files(
        {
            "src/python/root/conftest.py": "",
            "src/python/root/BUILD": "python_test_utils(resolve=parametrize('a', 'b'))",
            "src/python/root/mid/conftest.py": "",
            "src/python/root/mid/BUILD": "python_test_utils()",
            "src/python/root/mid/leaf/conftest.py": "",
            "src/python/root/mid/leaf/this_is_a_test.py": "",
            "src/python/root/mid/leaf/BUILD": "python_test_utils()\npython_tests(name='tests')",
        }
    )

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

    assert run_dep_inference(
        Address(
            "src/python/root/mid/leaf", target_name="tests", relative_file_path="this_is_a_test.py"
        )
    ) == InferredDependencies(
        [
            Address(
                "src/python/root", relative_file_path="conftest.py", parameters={"resolve": "a"}
            ),
            Address("src/python/root/mid", relative_file_path="conftest.py"),
            Address("src/python/root/mid/leaf", relative_file_path="conftest.py"),
        ],
    )
Exemplo n.º 7
0
def test_infer_python_inits() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            *target_types_rules.rules(),
            *core_target_types_rules(),
            infer_python_init_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferInitDependencies, )),
        ],
        target_types=[PythonSourcesGeneratorTarget],
    )
    rule_runner.set_options(
        ["--python-infer-inits", "--source-root-patterns=src/python"],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    rule_runner.write_files({
        "src/python/root/__init__.py": "",
        "src/python/root/BUILD": "python_sources()",
        "src/python/root/mid/__init__.py": "",
        "src/python/root/mid/BUILD": "python_sources()",
        "src/python/root/mid/leaf/__init__.py": "",
        "src/python/root/mid/leaf/f.py": "",
        "src/python/root/mid/leaf/BUILD": "python_sources()",
        "src/python/type_stub/__init__.pyi": "",
        "src/python/type_stub/foo.pyi": "",
        "src/python/type_stub/BUILD": "python_sources()",
    })

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

    assert run_dep_inference(
        Address(
            "src/python/root/mid/leaf",
            relative_file_path="f.py")) == InferredDependencies([
                Address("src/python/root", relative_file_path="__init__.py"),
                Address("src/python/root/mid",
                        relative_file_path="__init__.py"),
                Address("src/python/root/mid/leaf",
                        relative_file_path="__init__.py"),
            ], )
    assert run_dep_inference(
        Address("src/python/type_stub",
                relative_file_path="foo.pyi")) == InferredDependencies([
                    Address("src/python/type_stub",
                            relative_file_path="__init__.pyi")
                ])
Exemplo n.º 8
0
def test_infer_python_conftests() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            *target_types_rules.rules(),
            infer_python_conftest_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferConftestDependencies, )),
        ],
        target_types=[
            PythonTestsGeneratorTarget, PythonTestUtilsGeneratorTarget
        ],
    )
    rule_runner.set_options(
        ["--source-root-patterns=src/python"],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    rule_runner.create_file("src/python/root/conftest.py")
    rule_runner.add_to_build_file("src/python/root", "python_test_utils()")

    rule_runner.create_file("src/python/root/mid/conftest.py")
    rule_runner.add_to_build_file("src/python/root/mid", "python_test_utils()")

    rule_runner.create_file("src/python/root/mid/leaf/conftest.py")
    rule_runner.create_file("src/python/root/mid/leaf/this_is_a_test.py")
    rule_runner.add_to_build_file(
        "src/python/root/mid/leaf",
        "python_test_utils()\npython_tests(name='tests')")

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

    assert run_dep_inference(
        Address(
            "src/python/root/mid/leaf",
            target_name="tests",
            relative_file_path="this_is_a_test.py")) == InferredDependencies([
                Address("src/python/root", relative_file_path="conftest.py"),
                Address("src/python/root/mid",
                        relative_file_path="conftest.py"),
                Address("src/python/root/mid/leaf",
                        relative_file_path="conftest.py"),
            ], )
Exemplo n.º 9
0
def test_infer_python_inits() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            *target_types_rules.rules(),
            infer_python_init_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferInitDependencies, )),
        ],
        target_types=[PythonSourcesGeneratorTarget],
    )
    rule_runner.set_options(
        [
            "--backend-packages=pants.backend.python",
            "--python-infer-inits",
            "--source-root-patterns=src/python",
        ],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    rule_runner.create_file("src/python/root/__init__.py")
    rule_runner.add_to_build_file("src/python/root", "python_sources()")

    rule_runner.create_file("src/python/root/mid/__init__.py")
    rule_runner.add_to_build_file("src/python/root/mid", "python_sources()")

    rule_runner.create_file("src/python/root/mid/leaf/__init__.py")
    rule_runner.create_file("src/python/root/mid/leaf/f.py")
    rule_runner.add_to_build_file("src/python/root/mid/leaf",
                                  "python_sources()")

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

    assert run_dep_inference(
        Address(
            "src/python/root/mid/leaf",
            relative_file_path="f.py")) == InferredDependencies([
                Address("src/python/root", relative_file_path="__init__.py"),
                Address("src/python/root/mid",
                        relative_file_path="__init__.py"),
                Address("src/python/root/mid/leaf",
                        relative_file_path="__init__.py"),
            ], )
Exemplo n.º 10
0
def test_infer_python_conftests() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            infer_python_conftest_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferConftestDependencies, )),
        ],
        target_types=[PythonTests],
    )
    rule_runner.set_options(
        [
            "--backend-packages=pants.backend.python",
            "--source-root-patterns=src/python"
        ],
        env_inherit={"PATH", "PYENV_ROOT", "HOME"},
    )

    rule_runner.create_file("src/python/root/conftest.py")
    rule_runner.add_to_build_file("src/python/root", "python_tests()")

    rule_runner.create_file("src/python/root/mid/conftest.py")
    rule_runner.add_to_build_file("src/python/root/mid", "python_tests()")

    rule_runner.create_file("src/python/root/mid/leaf/conftest.py")
    rule_runner.create_file("src/python/root/mid/leaf/this_is_a_test.py")
    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)
        return rule_runner.request(
            InferredDependencies,
            [InferConftestDependencies(target[PythonSources])],
        )

    assert run_dep_inference(
        Address("src/python/root/mid/leaf")) == InferredDependencies(
            [
                Address("src/python/root",
                        relative_file_path="conftest.py",
                        target_name="root"),
                Address("src/python/root/mid",
                        relative_file_path="conftest.py",
                        target_name="mid"),
            ],
            sibling_dependencies_inferrable=False,
        )
Exemplo n.º 11
0
def test_infer_python_inits() -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            infer_python_init_dependencies,
            SubsystemRule(PythonInference),
            QueryRule(InferredDependencies, (InferInitDependencies, )),
        ],
        target_types=[PythonLibrary],
    )
    rule_runner.set_options([
        "--backend-packages=pants.backend.python",
        "--python-infer-inits",
        "--source-root-patterns=src/python",
    ])

    rule_runner.create_file("src/python/root/__init__.py")
    rule_runner.add_to_build_file("src/python/root", "python_library()")

    rule_runner.create_file("src/python/root/mid/__init__.py")
    rule_runner.add_to_build_file("src/python/root/mid", "python_library()")

    rule_runner.create_file("src/python/root/mid/leaf/__init__.py")
    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)
        return rule_runner.request(
            InferredDependencies,
            [InferInitDependencies(target[PythonSources])],
        )

    assert run_dep_inference(
        Address("src/python/root/mid/leaf")) == InferredDependencies(
            [
                Address("src/python/root",
                        relative_file_path="__init__.py",
                        target_name="root"),
                Address("src/python/root/mid",
                        relative_file_path="__init__.py",
                        target_name="mid"),
            ],
            sibling_dependencies_inferrable=False,
        )
Exemplo n.º 12
0
def rules():
    return (
        *coverage_py.rules(),
        *tailor.rules(),
        *ancestor_files.rules(),
        *python_sources.rules(),
        *dependency_inference_rules.rules(),
        *pex.rules(),
        *pex_cli.rules(),
        *pex_environment.rules(),
        *pex_from_targets.rules(),
        *pytest_runner.rules(),
        *package_pex_binary.rules(),
        *python_native_code.rules(),
        *repl.rules(),
        *run_pex_binary.rules(),
        *target_types_rules.rules(),
        *setup_py.rules(),
    )
Exemplo n.º 13
0
def test_infer_python_inits(behavior: InitFilesInference) -> None:
    rule_runner = RuleRunner(
        rules=[
            *ancestor_files.rules(),
            *target_types_rules.rules(),
            *core_target_types_rules(),
            infer_python_init_dependencies,
            SubsystemRule(PythonInferSubsystem),
            QueryRule(InferredDependencies, (InferInitDependencies,)),
        ],
        target_types=[PythonSourcesGeneratorTarget],
        objects={"parametrize": Parametrize},
    )
    rule_runner.set_options(
        [
            f"--python-infer-init-files={behavior.value}",
            "--python-resolves={'a': '', 'b': ''}",
            "--python-default-resolve=a",
            "--python-enable-resolves",
        ],
        env_inherit=PYTHON_BOOTSTRAP_ENV,
    )
    rule_runner.write_files(
        {
            "src/python/root/__init__.py": "content",
            "src/python/root/BUILD": "python_sources(resolve=parametrize('a', 'b'))",
            "src/python/root/mid/__init__.py": "",
            "src/python/root/mid/BUILD": "python_sources()",
            "src/python/root/mid/leaf/__init__.py": "content",
            "src/python/root/mid/leaf/f.py": "",
            "src/python/root/mid/leaf/BUILD": "python_sources()",
            "src/python/type_stub/__init__.pyi": "content",
            "src/python/type_stub/foo.pyi": "",
            "src/python/type_stub/BUILD": "python_sources()",
        }
    )

    def check(address: Address, expected: list[Address]) -> None:
        target = rule_runner.get_target(address)
        result = rule_runner.request(
            InferredDependencies,
            [InferInitDependencies(target[PythonSourceField])],
        )
        if behavior == InitFilesInference.never:
            assert not result
        else:
            assert result == InferredDependencies(expected)

    check(
        Address("src/python/root/mid/leaf", relative_file_path="f.py"),
        [
            Address(
                "src/python/root", relative_file_path="__init__.py", parameters={"resolve": "a"}
            ),
            *(
                []
                if behavior is InitFilesInference.content_only
                else [Address("src/python/root/mid", relative_file_path="__init__.py")]
            ),
            Address("src/python/root/mid/leaf", relative_file_path="__init__.py"),
        ],
    )
    check(
        Address("src/python/type_stub", relative_file_path="foo.pyi"),
        [Address("src/python/type_stub", relative_file_path="__init__.pyi")],
    )