Skip to content

Commit ed8e89d

Browse files
authored
Make switch task work as expected with 'uses' option (#186)
Fixes #182 by making a switch task capture the contents of the case task for the next task to use. Also tweak implementation of script tasks to make variable collisions less likely.
1 parent 217a8b4 commit ed8e89d

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

poethepoet/task/script.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ def _handle_run(
4848

4949
script = [
5050
"import asyncio,os,sys;",
51-
"from inspect import iscoroutinefunction as ic;",
51+
"from inspect import iscoroutinefunction as _c;",
5252
"from os import environ;",
53-
"from importlib import import_module as im;",
53+
"from importlib import import_module as _i;",
5454
f"sys.argv = {argv!r}; sys.path.append('src');",
5555
f"{format_class(named_arg_values)}",
56-
f"_m = im('{target_module}');",
57-
f"_r = asyncio.run(_m.{function_call}) if ic(_m.{function_ref})",
56+
f"_m = _i('{target_module}');",
57+
f"_r = asyncio.run(_m.{function_call}) if _c(_m.{function_ref})",
5858
f" else _m.{function_call};",
5959
]
6060

poethepoet/task/switch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def __init__(
8888
config=config,
8989
invocation=task_invocation,
9090
ui=ui,
91+
capture_stdout=self.options.get("capture_stdout", capture_stdout),
9192
inheritance=TaskInheritance.from_task(self),
9293
)
9394

pyproject.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@ _clean_docs.script = "shutil:rmtree('docs/_build', ignore_errors=1)"
145145
help = "Execute poe from this repo (useful for testing)"
146146
script = "poethepoet:main"
147147

148-
[tool.poe.tasks.y]
149-
cmd = "pwd"
150-
151-
[tool.poe.tasks.x]
152-
sequence = ["y", {cmd = "pwd"}]
153-
cwd = "tests"
154-
155148

156149
[tool.rstcheck]
157150
ignore_messages = [

tests/fixtures/switch_project/pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,18 @@ control = "poe_test_echo ${WHATEVER}"
7171
[[tool.poe.tasks.multivalue_case.switch]]
7272
shell = "import sys; print('It is not in 1-6')"
7373
interpreter = "python"
74+
75+
76+
[tool.poe.tasks.switcher]
77+
control.expr = "42"
78+
79+
[[tool.poe.tasks.switcher.switch]]
80+
case = "42"
81+
cmd = "echo 'matched'"
82+
83+
[[tool.poe.tasks.switcher.switch]]
84+
cmd = "echo other"
85+
86+
[tool.poe.tasks.switcher_user]
87+
uses = { switched = "switcher" }
88+
cmd = "echo switched=$switched"

tests/test_switch_task.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ def test_switch_dry_run(run_poe_subproc):
8686
assert result.stderr == ""
8787

8888

89+
def test_switch_in_in_graph(run_poe_subproc):
90+
result = run_poe_subproc("switcher_user", project="switch")
91+
assert result.capture == (
92+
"Poe <= 42\n" "Poe <= echo matched\n" "Poe => echo switched=matched\n"
93+
)
94+
assert result.stdout == "switched=matched\n"
95+
assert result.stderr == ""
96+
97+
8998
def test_switch_multivalue_case(run_poe_subproc):
9099
for num in ("1", "3", "5"):
91100
result = run_poe_subproc(

0 commit comments

Comments
 (0)