def test_target_generator() -> None: rule_runner = RuleRunner( rules=( *pants_requirements.rules(), QueryRule(_TargetParametrizations, [_TargetParametrizationsRequest]), ), target_types=[PantsRequirementsTargetGenerator], ) rule_runner.write_files({ "BUILD": ("pants_requirements(name='default')\n" "pants_requirements(\n" " name='no_testutil', testutil=False, resolve='a'\n" ")") }) result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("", target_name="default"), description_of_origin="tests") ], ).parametrizations assert len(result) == 2 pants_req = next(t for t in result.values() if t.address.generated_name == "pantsbuild.pants") testutil_req = next( t for t in result.values() if t.address.generated_name == "pantsbuild.pants.testutil") assert pants_req[PythonRequirementModulesField].value == ("pants", ) assert testutil_req[PythonRequirementModulesField].value == ( "pants.testutil", ) assert pants_req[PythonRequirementsField].value == ( PipRequirement.parse(f"pantsbuild.pants{determine_version()}"), ) assert testutil_req[PythonRequirementsField].value == ( PipRequirement.parse( f"pantsbuild.pants.testutil{determine_version()}"), ) for t in (pants_req, testutil_req): assert not t[PythonRequirementResolveField].value result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("", target_name="no_testutil"), description_of_origin="tests") ], ).parametrizations assert len(result) == 1 assert next(iter(result.keys())).generated_name == "pantsbuild.pants" pants_req = next(iter(result.values())) assert pants_req[PythonRequirementResolveField].value == "a"
def test_pex_binary_targets() -> None: rule_runner = RuleRunner( rules=[ *target_types_rules.rules(), *import_rules(), *python_sources.rules(), QueryRule(_TargetParametrizations, [_TargetParametrizationsRequest]), ], target_types=[PexBinariesGeneratorTarget], ) rule_runner.write_files( { "src/py/BUILD": dedent( """\ pex_binaries( name="pexes", entry_points=[ "f1.py", "f2:foo", "subdir.f.py", "subdir.f:main", ], overrides={ 'f2:foo': {'tags': ['overridden']}, 'subdir.f.py': {'tags': ['overridden']}, } ) """ ), } ) def gen_pex_binary_tgt(entry_point: str, tags: list[str] | None = None) -> PexBinary: return PexBinary( {PexEntryPointField.alias: entry_point, Tags.alias: tags}, Address("src/py", target_name="pexes", generated_name=entry_point.replace(":", "-")), residence_dir="src/py", ) result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest( Address("src/py", target_name="pexes"), description_of_origin="tests" ) ], ).parametrizations.values() assert set(result) == { gen_pex_binary_tgt("f1.py"), gen_pex_binary_tgt("f2:foo", tags=["overridden"]), gen_pex_binary_tgt("subdir.f.py", tags=["overridden"]), gen_pex_binary_tgt("subdir.f:main"), }
def test_generate_source_targets() -> None: rule_runner = RuleRunner( rules=[ *target_types.rules(), QueryRule(_TargetParametrizations, [_TargetParametrizationsRequest]), ], target_types=[AvroSourcesGeneratorTarget], ) rule_runner.write_files({ "src/avro/BUILD": dedent("""\ avro_sources( name='lib', sources=['**/*.avsc', '**/*.avpr'], overrides={'f1.avsc': {'tags': ['overridden']}}, ) """), "src/avro/f1.avsc": "", "src/avro/f2.avpr": "", "src/avro/subdir/f.avsc": "", }) def gen_tgt(rel_fp: str, tags: list[str] | None = None) -> AvroSourceTarget: return AvroSourceTarget( { SingleSourceField.alias: rel_fp, Tags.alias: tags }, Address("src/avro", target_name="lib", relative_file_path=rel_fp), residence_dir=os.path.dirname(os.path.join("src/avro", rel_fp)), ) generated = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("src/avro", target_name="lib"), description_of_origin="tests") ], ).parametrizations assert set(generated.values()) == { gen_tgt("f1.avsc", tags=["overridden"]), gen_tgt("f2.avpr"), gen_tgt("subdir/f.avsc"), }
def test_generate_source_targets() -> None: rule_runner = RuleRunner( rules=[ *target_types.rules(), QueryRule(_TargetParametrizations, [_TargetParametrizationsRequest]), ], target_types=[WsdlSourcesGeneratorTarget], ) source_root = "src/wsdl" rule_runner.write_files({ f"{source_root}/BUILD": dedent("""\ wsdl_sources( name="lib", sources=["**/*.wsdl"] ) """), f"{source_root}/f1.wsdl": "", f"{source_root}/sub/f2.wsdl": "", }) def gen_tgt(rel_fp: str, tags: list[str] | None = None) -> WsdlSourceTarget: return WsdlSourceTarget( { SingleSourceField.alias: rel_fp, Tags.alias: tags }, Address(source_root, target_name="lib", relative_file_path=rel_fp), residence_dir=os.path.dirname(os.path.join(source_root, rel_fp)), ) generated = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address(source_root, target_name="lib"), description_of_origin="tests") ], ).parametrizations assert set(generated.values()) == { gen_tgt("f1.wsdl"), gen_tgt("sub/f2.wsdl"), }
def test_generate_source_targets() -> None: rule_runner = RuleRunner( rules=[ *target_types.rules(), QueryRule(_TargetParametrizations, [_TargetParametrizationsRequest]), ], target_types=[HelmUnitTestTestsGeneratorTarget, HelmChartTarget], ) source_root = "src/chart" rule_runner.write_files( { f"{source_root}/BUILD": """helm_chart(name="foo")""", f"{source_root}/Chart.yaml": HELM_CHART_FILE, f"{source_root}/values.yaml": HELM_VALUES_FILE, f"{source_root}/templates/_helpers.tpl": HELM_TEMPLATE_HELPERS_FILE, f"{source_root}/templates/service.yaml": K8S_SERVICE_FILE, f"{source_root}/tests/BUILD": "helm_unittest_tests(name='foo_tests')", f"{source_root}/tests/service_test.yaml": "", } ) def gen_tgt(rel_fp: str, tags: list[str] | None = None) -> HelmUnitTestTestTarget: return HelmUnitTestTestTarget( { SingleSourceField.alias: rel_fp, Tags.alias: tags, }, Address(f"{source_root}/tests", target_name="foo_tests", relative_file_path=rel_fp), residence_dir=os.path.dirname(os.path.join(f"{source_root}/tests", rel_fp)), ) generated = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest( Address(f"{source_root}/tests", target_name="foo_tests"), description_of_origin="tests", ) ], ).parametrizations assert set(generated.values()) == { gen_tgt("service_test.yaml"), }
def assert_pipenv_requirements( rule_runner: RuleRunner, build_file_entry: str, pipfile_lock: dict, *, expected_targets: set[Target], ) -> None: rule_runner.write_files({ "BUILD": build_file_entry, "Pipfile.lock": dumps(pipfile_lock) }) result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("", target_name="reqs"), description_of_origin="tests") ], ) assert set(result.parametrizations.values()) == expected_targets
def assert_python_requirements( rule_runner: RuleRunner, build_file_entry: str, requirements_txt: str, *, expected_targets: set[Target], requirements_txt_relpath: str = "requirements.txt", ) -> None: rule_runner.write_files({ "BUILD": build_file_entry, requirements_txt_relpath: requirements_txt }) result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("", target_name="reqs"), description_of_origin="tests") ], ) assert set(result.parametrizations.values()) == expected_targets
def assert_poetry_requirements( rule_runner: RuleRunner, build_file_entry: str, pyproject_toml: str, *, expected_targets: set[Target], pyproject_toml_relpath: str = "pyproject.toml", ) -> None: rule_runner.write_files({ "BUILD": build_file_entry, pyproject_toml_relpath: pyproject_toml }) result = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("", target_name="reqs"), description_of_origin="tests") ], ) assert set(result.parametrizations.values()) == expected_targets
def test_generate_package_targets(rule_runner: RuleRunner) -> None: rule_runner.write_files({ "src/go/BUILD": "go_mod()\n", "src/go/go.mod": dedent("""\ module example.com/src/go go 1.17 require ( github.com/google/go-cmp v0.4.0 github.com/google/uuid v1.2.0 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect ) """), "src/go/go.sum": dedent("""\ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= """), "src/go/hello.go": "", "src/go/subdir/f.go": "", "src/go/subdir/f2.go": "", "src/go/another_dir/subdir/f.go": "", }) generated = rule_runner.request( _TargetParametrizations, [ _TargetParametrizationsRequest(Address("src/go"), description_of_origin="tests") ], ).parametrizations file_tgts = [ TargetGeneratorSourcesHelperTarget( {TargetGeneratorSourcesHelperSourcesField.alias: fp}, Address("src/go", relative_file_path=fp), ) for fp in ("go.mod", "go.sum") ] def gen_third_party_tgt(import_path: str) -> GoThirdPartyPackageTarget: return GoThirdPartyPackageTarget( { GoImportPathField.alias: import_path, Dependencies.alias: [t.address.spec for t in file_tgts], }, Address("src/go", generated_name=import_path), ) all_third_party = { gen_third_party_tgt(pkg) for pkg in ( "github.com/google/uuid", "github.com/google/go-cmp/cmp", "github.com/google/go-cmp/cmp/cmpopts", "github.com/google/go-cmp/cmp/internal/diff", "github.com/google/go-cmp/cmp/internal/flags", "github.com/google/go-cmp/cmp/internal/function", "github.com/google/go-cmp/cmp/internal/testprotos", "github.com/google/go-cmp/cmp/internal/teststructs", "github.com/google/go-cmp/cmp/internal/value", "golang.org/x/xerrors", "golang.org/x/xerrors/internal", ) } assert set(generated.values()) == {*file_tgts, *all_third_party}