cold-soccer-63228
06/20/2023, 4:03 PMpants.testutil.rule_runner.RuleRunner
. Basically, I’m using rule_runner.write_files
to create some files as specified in https://www.pantsbuild.org/v2.15/docs/rules-api-testing.
However, I’m noticing that despite the test codepath creating and recognizing the existence of the file, the non-test codepath does not recognize the existence of the file. In my logs, I’m seeing the following.
created file at /private/var/folders/yw/gqb55c1j1y354lwwlksln1mw0000gp/T/pants-sandbox-08iAjL/front_porch/modules/a/_package_config.py
there exists no file at /private/var/folders/yw/gqb55c1j1y354lwwlksln1mw0000gp/T/pants-sandbox-08iAjL/front_porch/modules/a/_package_config.py
I’ve attached the code that I’m running in the thread below for more context. Perhaps I’m missing something silly?cold-soccer-63228
06/20/2023, 4:04 PMdef _retrieve_package_config_path(path: Path) -> Optional[Path]:
potential_package_config_path = Path(*path.parts[:3]) / PACKAGE_CONFIG_FILENAME
if potential_package_config_path.is_file():
return potential_package_config_path
return None
@rule(desc="Inferring Python package config dependencies by analyzing source")
async def infer_python_package_config_dependencies(
request: InferPackageConfigDependenciesRequest,
) -> InferredDependencies:
filepath = Path(request.field_set.source.file_path)
package_config_path = _retrieve_package_config_path(filepath)
if package_config_path is None:
print(f"there exists no file at {potential_package_config_path.resolve()}")
return InferredDependencies([])
cold-soccer-63228
06/20/2023, 4:04 PMclass TestRules:
def test_dependency_inference(self, rule_runner: RuleRunner) -> None:
rule_runner.write_files(
{
os.path.join("front_porch", "modules", "a", PACKAGE_CONFIG_FILENAME): textwrap.dedent(
"""\
package_config = PackageConfig(
check=CheckConfig(dependencies=["b"]),
code_reviews=[CodeReviewConfig(reviewer=OwnerValues.PLATFORM, required=True)],
owner=OwnerValues.PLATFORM,
domain=DomainValues.PLATFORM,
package_name="a",
)
"""
),
os.path.join("front_porch", "modules", "a", "service_interface.py"): "",
os.path.join("front_porch", "modules", "a", "BUILD"): "python_sources()",
os.path.join("front_porch", "modules", "b", PACKAGE_CONFIG_FILENAME): textwrap.dedent(
"""\
package_config = PackageConfig(
check=CheckConfig(dependencies=["c"]),
code_reviews=[CodeReviewConfig(reviewer=OwnerValues.PLATFORM, required=True)],
owner=OwnerValues.PLATFORM,
domain=DomainValues.PLATFORM,
package_name="b",
)
"""
),
os.path.join("front_porch", "modules", "b", "service_interface.py"): "",
os.path.join("front_porch", "modules", "b", "BUILD"): "python_sources()",
}
)
print(f'created file at {Path(os.path.join("front_porch", "modules", "a", PACKAGE_CONFIG_FILENAME)).resolve()}')
def run_dep_inference(address: Address) -> InferredDependencies:
rule_runner.set_options([], env_inherit=PYTHON_BOOTSTRAP_ENV)
tgt = rule_runner.get_target(address)
return rule_runner.request(
InferredDependencies,
[InferPackageConfigDependenciesRequest(PythonImportDependenciesInferenceFieldSet.create(tgt))],
)
assert run_dep_inference(
Address(os.path.join("front_porch", "modules", "a"), relative_file_path="service_interface.py")
) == InferredDependencies(
[Address(os.path.join("front_porch", "modules", "b"), relative_file_path=PACKAGE_CONFIG_FILENAME)]
)
cold-soccer-63228
06/20/2023, 4:24 PMrule_runner.build_root
. But since rule_runner
is only available in the test file, how can I get this to work in the non-test file when testing the actual rule?cold-soccer-63228
06/20/2023, 8:35 PMrule_runner.build_root
? I want it to be able to see the fake files created in my tests.
My tests create files in /private/tmp/_BUILD_ROOTr12w_yvr
, but my plugin seems to think that these files are in /private/var/folders/yw/gqb55c1j1y354lwwlksln1mw0000gp/T/pants-sandbox-P7tznM
.