From e056c26a460d1125186e959a67e4b024876b90b0 Mon Sep 17 00:00:00 2001 From: Nat Noordanus Date: Fri, 13 Jan 2023 21:25:56 +0100 Subject: [PATCH] Fix handling of switch tasks and task with uses option in dry-run mode #115 Since it's not possible to properly resolve the content of these tasks without actually executing the tasks they depend on, instead of outputting the task content we print a message that it is unresolved. --- poethepoet/task/base.py | 15 +++++++++++++-- poethepoet/task/switch.py | 6 ++++++ tests/test_graph_execution.py | 14 ++++++++++++++ tests/test_switch_task.py | 9 +++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/poethepoet/task/base.py b/poethepoet/task/base.py index cd546a5e4..ae4b3ad08 100644 --- a/poethepoet/task/base.py +++ b/poethepoet/task/base.py @@ -228,6 +228,15 @@ def run( Run this task """ upstream_invocations = self._get_upstream_invocations(context) + + if context.dry and upstream_invocations.get("uses", {}): + self._print_action( + f"unresolved dependency task results via uses option for task {self.name!r}", + dry=True, + unresolved=True, + ) + return 0 + return self._handle_run( context, extra_args, @@ -484,12 +493,14 @@ def _validate_task_def( issue = None return issue - def _print_action(self, action: str, dry: bool): + def _print_action(self, action: str, dry: bool, unresolved: bool = False): """ Print the action taken by a task just before executing it. """ min_verbosity = -1 if dry else 0 - arrow = "<=" if self.options.get("capture_stdout") else "=>" + arrow = ( + "??" if unresolved else "<=" if self.options.get("capture_stdout") else "=>" + ) self._ui.print_msg( f"Poe {arrow} {action}", min_verbosity ) diff --git a/poethepoet/task/switch.py b/poethepoet/task/switch.py index dca7d1036..1d8e1c38e 100644 --- a/poethepoet/task/switch.py +++ b/poethepoet/task/switch.py @@ -111,6 +111,12 @@ def _handle_run( f"Switch task {self.name!r} aborted after failed control task" ) + if context.dry: + self._print_action( + f"unresolved case for switch task", dry=True, unresolved=True + ) + return 0 + control_task_output = context.get_task_output(self.control_task.invocation) case_task = self.switch_tasks.get( control_task_output, self.switch_tasks.get(DEFAULT_CASE) diff --git a/tests/test_graph_execution.py b/tests/test_graph_execution.py index 14513c3db..6991fb38a 100644 --- a/tests/test_graph_execution.py +++ b/tests/test_graph_execution.py @@ -12,3 +12,17 @@ def test_call_attr_func(run_poe_subproc): "here we go...\n" "Thinking about and\n" "hello and hello\n" ) assert result.stderr == "" + + +def test_uses_dry_run(run_poe_subproc): + result = run_poe_subproc("-d", "deep-graph-with-args", project="graphs") + assert result.capture == ( + "Poe => poe_test_echo here we go...\n" + "Poe => :\n" + "Poe <= poe_test_echo about\n" + "Poe <= poe_test_echo hello\n" + "Poe ?? unresolved dependency task results via uses option for task 'think'\n" + "Poe ?? unresolved dependency task results via uses option for task 'deep-graph-with-args'\n" + ) + assert result.stdout == "" + assert result.stderr == "" diff --git a/tests/test_switch_task.py b/tests/test_switch_task.py index 2bc82490a..9bdcce445 100644 --- a/tests/test_switch_task.py +++ b/tests/test_switch_task.py @@ -80,6 +80,15 @@ def test_switch_default_fail(run_poe_subproc): assert result.stderr == "" +def test_switch_dry_run(run_poe_subproc): + result = run_poe_subproc("-d", "var_dependent", project="switch") + assert result.capture == ( + "Poe <= int(${FOO_VAR}) % 2\n" "Poe ?? unresolved case for switch task\n" + ) + assert result.stdout == "" + assert result.stderr == "" + + def test_switch_multivalue_case(run_poe_subproc): for num in ("1", "3", "5"): result = run_poe_subproc(