From 6d8a26c3b036dfcad08e32c3bf6e6c6af1f5b5cc Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 20 Feb 2024 10:46:28 +1100 Subject: [PATCH 1/3] Resolve adhoc_tool, code_quality_tool relative to target location --- src/python/pants/core/util_rules/adhoc_process_support.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/pants/core/util_rules/adhoc_process_support.py b/src/python/pants/core/util_rules/adhoc_process_support.py index 1df0d2d2b0f..f5ed03fdffd 100644 --- a/src/python/pants/core/util_rules/adhoc_process_support.py +++ b/src/python/pants/core/util_rules/adhoc_process_support.py @@ -411,7 +411,7 @@ async def create_tool_runner( Get( ResolvedExecutionDependencies, ResolveExecutionDependenciesRequest( - address=runnable_address, + address=request.target.address, execution_dependencies=request.execution_dependencies, runnable_dependencies=request.runnable_dependencies, ), From 18222327fe64eade5cace0fac94c6b9d97afd031 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 22 Feb 2024 16:34:10 +1100 Subject: [PATCH 2/3] Test for adhoc_tool dependencies --- .../pants/backend/adhoc/adhoc_tool_test.py | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/python/pants/backend/adhoc/adhoc_tool_test.py b/src/python/pants/backend/adhoc/adhoc_tool_test.py index 612bebb7ada..7b1620c8fda 100644 --- a/src/python/pants/backend/adhoc/adhoc_tool_test.py +++ b/src/python/pants/backend/adhoc/adhoc_tool_test.py @@ -268,3 +268,37 @@ def test_env_vars(rule_runner: PythonRuleRunner) -> None: Address("src", target_name="envvars"), expected_contents={"out.log": f"{envvar_value}\n"}, ) + + +def test_execution_dependencies_and_runnable_dependencies(rule_runner: PythonRuleRunner) -> None: + file_contents = "example contents" + + rule_runner.write_files( + { + # put the runnable in its own directory, so we're sure that the dependencies are + # resolved relative to the adhoc_tool target + "a/BUILD": """system_binary(name="bash", binary_name="bash")""", + "b/BUILD": dedent( + """ + system_binary(name="renamed_cat", binary_name="cat") + files(name="f", sources=["f.txt"]) + + adhoc_tool( + name="deps", + runnable="a:bash", + args=["-c", "renamed_cat f.txt"], + execution_dependencies=[":f"], + runnable_dependencies=[":renamed_cat"], + stdout="stdout", + ) + """ + ), + "b/f.txt": file_contents, + } + ) + + assert_adhoc_tool_result( + rule_runner, + Address("b", target_name="deps"), + expected_contents={"b/stdout": file_contents}, + ) From 40c30fb5663907f914db69073efd83bc0bb4739d Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Thu, 22 Feb 2024 17:18:10 +1100 Subject: [PATCH 3/3] Test for code_quality_tool dependencies --- .../code_quality_tool_integration_test.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/python/pants/backend/adhoc/code_quality_tool_integration_test.py b/src/python/pants/backend/adhoc/code_quality_tool_integration_test.py index 0f796fb4778..09d0d1b1fc7 100644 --- a/src/python/pants/backend/adhoc/code_quality_tool_integration_test.py +++ b/src/python/pants/backend/adhoc/code_quality_tool_integration_test.py @@ -10,6 +10,8 @@ CodeQualityToolUnsupportedGoalError, base_rules, ) +from pants.backend.adhoc.run_system_binary import rules as run_system_binary_rules +from pants.backend.adhoc.target_types import SystemBinaryTarget from pants.backend.python import register as register_python from pants.backend.python.target_types import PythonSourceTarget from pants.core.goals.fix import Fix @@ -35,6 +37,7 @@ def make_rule_runner(*cfgs: CodeQualityToolRuleBuilder): *core_rules(), *process.rules(), *register_python.rules(), + *run_system_binary_rules(), *base_rules(), ] for cfg in cfgs: @@ -45,6 +48,7 @@ def make_rule_runner(*cfgs: CodeQualityToolRuleBuilder): CodeQualityToolTarget, FileTarget, PythonSourceTarget, + SystemBinaryTarget, ], rules=rules, ) @@ -276,3 +280,48 @@ def test_several_formatters(): assert "badtogood made changes" in res.stderr assert "underscoreit" not in res.stderr assert "goodcode = 50\ngoodcode = 100\n" == rule_runner.read_file("do_not_underscore.py") + + +def test_execution_dependencies_and_runnable_dependencies(): + input_contents = "input contents\n" + output_contents = "original output contents" + # confirm that comparing these strings confirms that the test behaved as expected + assert input_contents != output_contents + + cfg = CodeQualityToolRuleBuilder( + goal="fmt", target="b:overwriter", name="Overwriter", scope="overwriter" + ) + + # Formatter that copies the `b/input.txt` file over `b/output.txt` file via bash (relying on + # there being only one file, and a workaround for #19103 meaning we can't use `cp` as a system + # binary) + rule_runner = make_rule_runner(cfg) + rule_runner.write_files( + { + # put the runnable in its own directory, so we're sure that the dependencies are + # resolved relative to the code_quality_tool target + "a/BUILD": """system_binary(name="bash", binary_name="bash")""", + "b/BUILD": dedent( + """ + system_binary(name="renamed_cat", binary_name="cat") + file(name="input", source="input.txt") + file(name="output", source="output.txt") + + code_quality_tool( + name="overwriter", + runnable="a:bash", + args=["-c", "renamed_cat b/input.txt > $1", "ignored"], + execution_dependencies=[":input"], + runnable_dependencies=[":renamed_cat"], + file_glob_include=["**/*.txt"], + ) + """ + ), + "b/input.txt": input_contents, + "b/output.txt": output_contents, + } + ) + res = rule_runner.run_goal_rule(Fmt, args=["b/output.txt"]) + assert res.exit_code == 0 + assert "overwriter made changes" in res.stderr + assert input_contents == rule_runner.read_file("b/output.txt")