Example #1
0
def test_find_putative_targets() -> None:
    rule_runner = RuleRunner(
        rules=[
            *core_tailor_rules(),
            *terraform_tailor_rules(),
            QueryRule(PutativeTargets,
                      [PutativeTerraformTargetsRequest, AllOwnedSources]),
            QueryRule(AllOwnedSources, ()),
        ],
        target_types=[
            TerraformModule,
            TerraformModules,
        ],
    )
    rule_runner.write_files({
        fp: ""
        for fp in (
            "prod/terraform/resources/foo/versions.tf",
            "prod/terraform/resources/bar/versions.tf",
            "prod/terraform/modules/bar/versions.tf",
            "prod/terraform/modules/bar/hello/versions.tf",
            "prod/terraform/modules/world/versions.tf",
            "service1/src/terraform/versions.tf",
            "service1/src/terraform/foo/versions.tf",
            "service1/src/terraform/versions.tf",
            "service2/src/terraform/versions.tf",
        )
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeTerraformTargetsRequest(PutativeTargetsSearchPaths(
                ("", ))),
            AllOwnedSources([
                "src/terraform/root.tf",
                "src/terraform/owned-module/main.tf",
                "src/terraform/owned-module/foo.tf",
            ]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            TerraformModules,
            "prod/terraform",
            "tf_mods",
            ("prod/terraform/**/*.tf", ),
        ),
        PutativeTarget.for_target_type(
            TerraformModules,
            "service1/src/terraform",
            "tf_mods",
            ("service1/src/terraform/**/*.tf", ),
        ),
        PutativeTarget.for_target_type(
            TerraformModules,
            "service2/src/terraform",
            "tf_mods",
            ("service2/src/terraform/**/*.tf", ),
        ),
    ]) == pts)
Example #2
0
def test_find_putative_targets_subset(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        f"src/sh/foo/{fp}": ""
        for fp in (
            "bar/bar.sh",
            "bar/bar_test.sh",
            "baz/baz.sh",
            "baz/baz_test.sh",
            "qux/qux.sh",
        )
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeShellTargetsRequest(
                PutativeTargetsSearchPaths(
                    ("src/sh/foo/bar", "src/sh/foo/qux"))),
            AllOwnedSources(["src/sh/foo/bar/bar.sh"]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            Shunit2TestsGeneratorTarget,
            path="src/sh/foo/bar",
            name="tests",
            triggering_sources=["bar_test.sh"],
        ),
        PutativeTarget.for_target_type(
            ShellSourcesGeneratorTarget,
            path="src/sh/foo/qux",
            name=None,
            triggering_sources=["qux.sh"],
        ),
    ]) == pts)
Example #3
0
def test_find_putative_targets_for_entry_points(
        rule_runner: RuleRunner) -> None:
    mains = ("main1.py", "main2.py", "main3.py")
    rule_runner.write_files({
        f"src/python/foo/{name}": textwrap.dedent("""
            if __name__ == "__main__":
                main()
            """)
        for name in mains
    })
    rule_runner.write_files({
        "src/python/foo/BUILD":
        ("pex_binary(name='main1', entry_point='main1.py')\n"
         "pex_binary(name='main2', entry_point='foo.main2')\n"),
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativePythonTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources([f"src/python/foo/{name}" for name in mains]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            PexBinary,
            "src/python/foo",
            "main3",
            [],
            kwargs={"entry_point": "main3.py"},
        ),
    ]) == pts)
Example #4
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "avro/foo/f.avsc": "",
        "avro/foo/bar/baz1.avdl": "",
        "avro/foo/bar/baz2.avpr": "",
        "avro/foo/bar/baz3.avsc": "",
    })

    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeAvroTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources(["avro/foo/bar/baz1.avdl"]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            AvroSourcesGeneratorTarget,
            path="avro/foo",
            name=None,
            triggering_sources=["f.avsc"],
        ),
        PutativeTarget.for_target_type(
            AvroSourcesGeneratorTarget,
            path="avro/foo/bar",
            name=None,
            triggering_sources=["baz2.avpr", "baz3.avsc"],
        ),
    ]) == pts)
Example #5
0
def test_find_putative_targets_subset(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "protos/foo/f.proto": "",
        "protos/foo/bar/bar.proto": "",
        "protos/foo/baz/baz.proto": "",
        "protos/foo/qux/qux.proto": "",
    })

    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeProtobufTargetsRequest(
                PutativeTargetsSearchPaths(
                    ("protos/foo/bar", "protos/foo/qux"))),
            AllOwnedSources([]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            ProtobufSourcesGeneratorTarget,
            path="protos/foo/bar",
            name=None,
            triggering_sources=["bar.proto"],
        ),
        PutativeTarget.for_target_type(
            ProtobufSourcesGeneratorTarget,
            path="protos/foo/qux",
            name=None,
            triggering_sources=["qux.proto"],
        ),
    ]) == pts)
Example #6
0
def test_ignore_solitary_init(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        f"src/python/foo/{fp}": ""
        for fp in (
            "__init__.py",
            "bar/__init__.py",
            "bar/bar.py",
            "baz/__init__.py",
            "qux/qux.py",
        )
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativePythonTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources([]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            PythonSourcesGeneratorTarget,
            "src/python/foo/bar",
            "bar",
            ["__init__.py", "bar.py"],
        ),
        PutativeTarget.for_target_type(PythonSourcesGeneratorTarget,
                                       "src/python/foo/qux", "qux",
                                       ["qux.py"]),
    ]) == pts)
Example #7
0
def test_find_putative_targets() -> None:
    rule_runner = RuleRunner(
        rules=[
            *docker_tailor_rules(),
            QueryRule(PutativeTargets,
                      [PutativeDockerTargetsRequest, AllOwnedSources]),
        ],
        target_types=[DockerImage],
    )
    rule_runner.write_files({
        "src/docker_ok/Dockerfile": "",
        "src/docker_orphan/Dockerfile": ""
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeDockerTargetsRequest(PutativeTargetsSearchPaths(
                ("src/", ))),
            AllOwnedSources(["src/docker_ok/Dockerfile"]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            DockerImage,
            "src/docker_orphan",
            "docker",
            ["Dockerfile"],
            kwargs={"name": "docker"},
        ),
    ]) == pts)
Example #8
0
def test_find_putative_targets_subset(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        f"src/python/foo/{fp}": ""
        for fp in (
            "__init__.py",
            "bar/__init__.py",
            "bar/bar.py",
            "bar/bar_test.py",
            "baz/baz.py",
            "baz/baz_test.py",
            "qux/qux.py",
        )
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativePythonTargetsRequest(
                PutativeTargetsSearchPaths(
                    ("src/python/foo/bar", "src/python/foo/qux"))),
            AllOwnedSources([
                "src/python/foo/bar/__init__.py", "src/python/foo/bar/bar.py"
            ]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            PythonTestsGeneratorTarget,
            "src/python/foo/bar",
            "tests",
            ["bar_test.py"],
        ),
        PutativeTarget.for_target_type(PythonSourcesGeneratorTarget,
                                       "src/python/foo/qux", None, ["qux.py"]),
    ]) == pts)
Example #9
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {
            "src/wsdl/simple.wsdl": "",
            "src/wsdl/dir1/hello.wsdl": "",
            "src/wsdl/dir1/world.wsdl": "",
        }
    )

    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeWsdlTargetsRequest(PutativeTargetsSearchPaths(("",))),
            AllOwnedSources(["src/wsdl/simple.wsdl"]),
        ],
    )
    assert (
        PutativeTargets(
            [
                PutativeTarget.for_target_type(
                    WsdlSourcesGeneratorTarget,
                    path="src/wsdl/dir1",
                    name=None,
                    triggering_sources=["hello.wsdl", "world.wsdl"],
                ),
            ]
        )
        == pts
    )
Example #10
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "thrifts/foo/f.thrift": "",
        "thrifts/foo/bar/baz1.thrift": "",
        "thrifts/foo/bar/baz2.thrift": "",
        "thrifts/foo/bar/baz3.thrift": "",
    })

    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeThriftTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources(["thrifts/foo/bar/baz1.thrift"]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(
            ThriftSourcesGeneratorTarget,
            path="thrifts/foo",
            name=None,
            triggering_sources=["f.thrift"],
        ),
        PutativeTarget.for_target_type(
            ThriftSourcesGeneratorTarget,
            path="thrifts/foo/bar",
            name=None,
            triggering_sources=["baz2.thrift", "baz3.thrift"],
        ),
    ]) == pts)
Example #11
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {
            "src/java/owned/BUILD": "java_sources()\n",
            "src/java/owned/OwnedFile.java": "package owned",
            "src/java/unowned/UnownedFile.java": "package unowned\n",
            "src/java/unowned/UnownedFileTest.java": "package unowned\n",
        }
    )
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeJavaTargetsRequest(PutativeTargetsSearchPaths(("",))),
            AllOwnedSources(["src/java/owned/OwnedFile.java"]),
        ],
    )
    assert (
        PutativeTargets(
            [
                PutativeTarget.for_target_type(
                    JavaSourcesGeneratorTarget, "src/java/unowned", "unowned", ["UnownedFile.java"]
                ),
                PutativeTarget.for_target_type(
                    JunitTestsGeneratorTarget,
                    "src/java/unowned",
                    "tests",
                    ["UnownedFileTest.java"],
                    kwargs={"name": "tests"},
                ),
            ]
        )
        == putative_targets
    )
Example #12
0
def test_find_go_package_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "unowned/f.go": "",
        "unowned/f1.go": "",
        "owned/f.go": "",
        "owned/BUILD": "go_package()",
        # Any `.go` files under a `testdata` or `vendor` folder should be ignored.
        "unowned/testdata/f.go": "",
        "unowned/testdata/subdir/f.go": "",
        "unowned/vendor/example.com/foo/bar.go": "",
        # Except if `vendor` is the last directory.
        "unowned/cmd/vendor/main.go": "",
    })
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeGoTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources(["owned/f.go"]),
        ],
    )
    assert putative_targets == PutativeTargets([
        PutativeTarget.for_target_type(
            GoPackageTarget,
            path="unowned",
            name=None,
            triggering_sources=["f.go", "f1.go"],
        ),
        PutativeTarget.for_target_type(
            GoPackageTarget,
            path="unowned/cmd/vendor",
            name=None,
            triggering_sources=["main.go"],
        ),
    ])
Example #13
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.set_options(
        ["--no-python-setup-tailor-ignore-solitary-init-files"])
    rule_runner.write_files({
        "3rdparty/requirements.txt": "",
        "3rdparty/requirements-test.txt": "",
        **{
            f"src/python/foo/{fp}": ""
            for fp in (
                "__init__.py",
                "bar/__init__.py",
                "bar/baz1.py",
                "bar/baz1_test.py",
                "bar/baz2.py",
                "bar/baz2_test.py",
                "bar/baz3.py",
            )
        },
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativePythonTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources([
                "3rdparty/requirements.txt",
                "src/python/foo/bar/__init__.py",
                "src/python/foo/bar/baz1.py",
            ]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget(
            "3rdparty",
            "requirements-test.txt",
            "python_requirements",
            ("3rdparty/requirements-test.txt", ),
            ("3rdparty/requirements-test.txt", ),
            addressable=False,
            kwargs={"requirements_relpath": "requirements-test.txt"},
        ),
        PutativeTarget.for_target_type(PythonLibrary, "src/python/foo", "foo",
                                       ["__init__.py"]),
        PutativeTarget.for_target_type(PythonLibrary, "src/python/foo/bar",
                                       "bar", ["baz2.py", "baz3.py"]),
        PutativeTarget.for_target_type(
            PythonTests,
            "src/python/foo/bar",
            "tests",
            ["baz1_test.py", "baz2_test.py"],
            kwargs={"name": "tests"},
        ),
    ]) == pts)
Example #14
0
def test_find_go_binary_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "missing_binary_tgt/app.go":
        "package main",
        "missing_binary_tgt/BUILD":
        "go_package()",
        "tgt_already_exists/app.go":
        "package main",
        "tgt_already_exists/BUILD":
        "go_binary(name='bin')\ngo_package()",
        "missing_pkg_and_binary_tgt/app.go":
        "package main",
        "main_set_to_different_dir/subdir/app.go":
        "package main",
        "main_set_to_different_dir/subdir/BUILD":
        "go_package()",
        "main_set_to_different_dir/BUILD":
        "go_binary(main='main_set_to_different_dir/subdir')",
    })
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeGoTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources([
                "missing_binary_tgt/app.go",
                "tgt_already_exists/app.go",
                "main_set_to_different_dir/subdir/app.go",
            ]),
        ],
    )
    assert putative_targets == PutativeTargets([
        PutativeTarget.for_target_type(
            GoBinaryTarget,
            path="missing_binary_tgt",
            name="bin",
            triggering_sources=[],
        ),
        PutativeTarget.for_target_type(
            GoPackageTarget,
            path="missing_pkg_and_binary_tgt",
            name="missing_pkg_and_binary_tgt",
            triggering_sources=["app.go"],
            kwargs={},
        ),
        PutativeTarget.for_target_type(
            GoBinaryTarget,
            path="missing_pkg_and_binary_tgt",
            name="bin",
            triggering_sources=[],
        ),
    ])
Example #15
0
def test_find_putative_go_module_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {
            "src/go/owned/BUILD": "go_module()\n",
            "src/go/owned/go.mod": "module example.com/src/go/owned\n",
            "src/go/unowned/go.mod": "module example.com/src/go/unowned\n",
        }
    )
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeGoModuleTargetsRequest(PutativeTargetsSearchPaths(("src/",))),
            AllOwnedSources(["src/go/owned/go.mod"]),
        ],
    )
    assert putative_targets == PutativeTargets(
        [PutativeTarget.for_target_type(GoModule, "src/go/unowned", "unowned", ["go.mod"])]
    )
Example #16
0
def test_find_putative_go_package_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {
            "src/go/owned/BUILD": "go_package()\n",
            "src/go/owned/src.go": "package owned\n",
            "src/go/unowned/src.go": "package unowned\n",
        }
    )
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeGoPackageTargetsRequest(PutativeTargetsSearchPaths(("src/",))),
            AllOwnedSources(["src/go/owned/src.go"]),
        ],
    )
    assert putative_targets == PutativeTargets(
        [PutativeTarget.for_target_type(GoPackage, "src/go/unowned", "unowned", ["src.go"])]
    )
Example #17
0
def test_target_type_with_no_sources_field(rule_runner: RuleRunner) -> None:
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            MockPutativeFortranModuleRequest(
                PutativeTargetsSearchPaths(tuple("")))
        ],
    )
    assert putative_targets == PutativeTargets(
        [PutativeTarget.for_target_type(FortranModule, "dir", "dir", [])])

    with pytest.raises(ValueError) as excinfo:
        _ = PutativeTarget.for_target_type(FortranModule, "dir", "dir",
                                           ["a.f90"])
    expected_msg = (
        "A target of type FortranModule was proposed at address dir:dir with explicit sources a.f90, "
        "but this target type does not have a `sources` field.")
    assert str(excinfo.value) == expected_msg
Example #18
0
def test_find_go_mod_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "unowned/go.mod": "",
        "owned/go.mod": "",
        "owned/BUILD": "go_mod()"
    })
    putative_targets = rule_runner.request(
        PutativeTargets,
        [
            PutativeGoTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources(["owned/go.mod"]),
        ],
    )
    assert putative_targets == PutativeTargets([
        PutativeTarget.for_target_type(GoModTarget,
                                       path="unowned",
                                       name=None,
                                       triggering_sources=["go.mod"])
    ])
Example #19
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files(
        {
            f"src/sh/foo/{fp}": ""
            for fp in (
                "f.sh",
                "bar/baz1.sh",
                "bar/baz1_test.sh",
                "bar/baz2.sh",
                "bar/baz2_test.sh",
                "bar/baz3.sh",
            )
        }
    )
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeShellTargetsRequest(PutativeTargetsSearchPaths(("",))),
            AllOwnedSources(["src/sh/foo/bar/baz1.sh", "src/sh/foo/bar/baz1_test.sh"]),
        ],
    )
    assert (
        PutativeTargets(
            [
                PutativeTarget.for_target_type(
                    ShellSourcesGeneratorTarget, "src/sh/foo", "foo", ["f.sh"]
                ),
                PutativeTarget.for_target_type(
                    ShellSourcesGeneratorTarget, "src/sh/foo/bar", "bar", ["baz2.sh", "baz3.sh"]
                ),
                PutativeTarget.for_target_type(
                    Shunit2TestsGeneratorTarget,
                    "src/sh/foo/bar",
                    "tests",
                    ["baz2_test.sh"],
                    kwargs={"name": "tests"},
                ),
            ]
        )
        == pts
    )
Example #20
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.write_files({
        "protos/foo/f.proto": "",
        "protos/foo/bar/baz1.proto": "",
        "protos/foo/bar/baz2.proto": "",
        "protos/foo/bar/baz3.proto": "",
    })

    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativeProtobufTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources(["protos/foo/bar/baz1.proto"]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget.for_target_type(ProtobufLibrary, "protos/foo", "foo",
                                       ["f.proto"]),
        PutativeTarget.for_target_type(ProtobufLibrary, "protos/foo/bar",
                                       "bar", ["baz2.proto", "baz3.proto"]),
    ]) == pts)
Example #21
0
def test_find_putative_targets(rule_runner: RuleRunner) -> None:
    rule_runner.set_options([
        "--no-python-tailor-ignore-solitary-init-files",
        "--no-use-deprecated-python-macros"
    ])
    rule_runner.write_files({
        "3rdparty/requirements.txt": "",
        "3rdparty/requirements-test.txt": "",
        **{
            f"src/python/foo/{fp}": ""
            for fp in (
                "__init__.py",
                "bar/__init__.py",
                "bar/baz1.py",
                "bar/baz1_test.py",
                "bar/baz2.py",
                "bar/baz2_test.py",
                "bar/baz3.py",
                "bar/conftest.py",
            )
        },
    })
    pts = rule_runner.request(
        PutativeTargets,
        [
            PutativePythonTargetsRequest(PutativeTargetsSearchPaths(("", ))),
            AllOwnedSources([
                "3rdparty/requirements.txt",
                "src/python/foo/bar/__init__.py",
                "src/python/foo/bar/baz1.py",
            ]),
        ],
    )
    assert (PutativeTargets([
        PutativeTarget(
            "3rdparty",
            "requirements-test.txt",
            "python_requirements",
            ("3rdparty/requirements-test.txt", ),
            ("3rdparty/requirements-test.txt", ),
            addressable=True,
            kwargs={"source": "requirements-test.txt"},
        ),
        PutativeTarget.for_target_type(PythonSourcesGeneratorTarget,
                                       "src/python/foo", None,
                                       ["__init__.py"]),
        PutativeTarget.for_target_type(
            PythonSourcesGeneratorTarget,
            "src/python/foo/bar",
            None,
            ["baz2.py", "baz3.py"],
        ),
        PutativeTarget.for_target_type(
            PythonTestsGeneratorTarget,
            "src/python/foo/bar",
            "tests",
            ["baz1_test.py", "baz2_test.py"],
        ),
        PutativeTarget.for_target_type(
            PythonTestUtilsGeneratorTarget,
            "src/python/foo/bar",
            "test_utils",
            ["conftest.py"],
        ),
    ]) == pts)