Skip to content

Commit

Permalink
Respect configured cwd when evaluating glob patterns in cmd tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-n committed Sep 22, 2023
1 parent fda84a7 commit 2c548cb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
17 changes: 10 additions & 7 deletions poethepoet/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ def get_task_output(self, invocation: Tuple[str, ...]):
"""
return re.sub(r"\s+", " ", self.captured_stdout[invocation].strip("\r\n"))

def get_working_dir(self, env: "EnvVarsManager", task_options: Dict[str, Any]):
cwd_option = env.fill_template(task_options.get("cwd", "."))
working_dir = Path(cwd_option)

if not working_dir.is_absolute():
working_dir = self.project_dir / working_dir

return working_dir

def get_executor(
self,
invocation: Tuple[str, ...],
Expand All @@ -106,17 +115,11 @@ def get_executor(
) -> "PoeExecutor":
from .executor import PoeExecutor

cwd_option = env.fill_template(task_options.get("cwd", "."))
working_dir = Path(cwd_option)

if not working_dir.is_absolute():
working_dir = self.project_dir / working_dir

return PoeExecutor.get(
invocation=invocation,
context=self,
env=env,
working_dir=working_dir,
working_dir=self.get_working_dir(env, task_options),
dry=self.dry,
executor_config=task_options.get("executor"),
capture_stdout=task_options.get("capture_stdout", False),
Expand Down
9 changes: 4 additions & 5 deletions poethepoet/task/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ def _handle_run(
)

def _resolve_args(self, context: "RunContext", env: "EnvVarsManager"):
from glob import glob

from ..helpers.command import parse_poe_cmd, resolve_command_tokens
from ..helpers.command.ast_core import ParseError

Expand All @@ -69,14 +67,15 @@ def _resolve_args(self, context: "RunContext", env: "EnvVarsManager"):
f"Invalid cmd task {self.name!r} includes multiple command lines"
)

working_dir = context.get_working_dir(env, self.options)

result = []
for cmd_token, has_glob in resolve_command_tokens(
command_lines[0], env.to_dict()
):
if has_glob:
# Resolve glob path
# TODO: check whether cwd is correct here??
result.extend(glob(cmd_token, recursive=True))
# Resolve glob pattern from the working directory
result.extend([str(match) for match in working_dir.glob(cmd_token)])
else:
result.append(cmd_token)

Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/cwd_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ cwd = "${POE_PWD}"
cmd = "poe_test_pwd"
cwd = "./subdir/${foo_var}"
args = ["foo_var"]

[tool.poe.tasks.ls]
cmd = "ls ${path_arg}"
cwd = "${cwd_arg}"
env = {VIRTUAL_ENV = ""}
args = ["cwd-arg", "path-arg"]
1 change: 1 addition & 0 deletions tests/fixtures/venv_project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
myvenv
2 changes: 1 addition & 1 deletion tests/helpers/command/test_command_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_resolve_command_tokens():
)[0]

assert list(resolve_command_tokens(line, {"thing1": r" *\o/", "thing2": ""})) == [
("ab *\o/* and ? ' *\o/'", False),
(r"ab *\o/* and ? ' *\o/'", False),
("${thing1}", False),
("", False),
]
20 changes: 20 additions & 0 deletions tests/test_cmd_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,23 @@ def test_cmd_task_with_cwd_option_arg(run_poe_subproc, poe_project_path):
== f'{poe_project_path.joinpath("tests", "fixtures", "cwd_project", "subdir", "foo")}\n'
)
assert result.stderr == ""


def test_cmd_task_with_with_glob_arg_and_cwd(run_poe_subproc, poe_project_path):
result = run_poe_subproc("ls", "--path-arg", "./subdir", project="cwd")
assert result.capture == "Poe => ls ./subdir\n"
assert result.stdout == f"bar\nfoo\n"
assert result.stderr == ""

result = run_poe_subproc("ls", "--cwd-arg", "subdir", project="cwd")
assert result.capture == "Poe => ls\n"
assert result.stdout == f"bar\nfoo\n"
assert result.stderr == ""

result = run_poe_subproc(
"ls", "--path-arg", "./f*", "--cwd-arg", "subdir", project="cwd"
)
assert result.capture.startswith("Poe => ls ")
assert result.capture.endswith("foo\n")
assert result.stdout == f"bar.txt\n"
assert result.stderr == ""

0 comments on commit 2c548cb

Please sign in to comment.