Skip to content

Commit

Permalink
Add POE_PWD for original working directory and allow use of env vars …
Browse files Browse the repository at this point in the history
…for cwd
  • Loading branch information
Spiffyk committed Aug 7, 2023
1 parent 19882e3 commit 6274e6d
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 6 deletions.
11 changes: 6 additions & 5 deletions poethepoet/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Tuple
Expand Down Expand Up @@ -106,15 +105,17 @@ def get_executor(
) -> "PoeExecutor":
from .executor import PoeExecutor

cwd_option = task_options.get("cwd", ".")
if cwd_option == '$exec_cwd':
cwd_option = os.getcwd()
cwd_option = self.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=self.project_dir / cwd_option,
working_dir=working_dir,
dry=self.dry,
executor_config=task_options.get("executor"),
capture_stdout=task_options.get("capture_stdout", False),
Expand Down
4 changes: 4 additions & 0 deletions poethepoet/env/manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union

Expand Down Expand Up @@ -51,6 +52,9 @@ def __init__(

self._vars["POE_ROOT"] = str(self._config.project_dir)

if "POE_PWD" not in self._vars:
self._vars["POE_PWD"] = os.getcwd()

def get(self, key: str, default: Optional[str] = None) -> Optional[str]:
return self._vars.get(key, default)

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def run_poe_subproc(

subproc_env = dict(os.environ)
subproc_env.pop("VIRTUAL_ENV", None)
subproc_env.pop("POE_PWD", None) # do not inherit this from the test
if env:
subproc_env.update(env)

Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/cwd_project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[tool.poe.tasks.cwd]
cmd = "poe_test_pwd"
cwd = "./subdir/foo"

[tool.poe.tasks.cwd_env]
cmd = "poe_test_pwd"
cwd = "./subdir/${BAR_ENV}"

[tool.poe.tasks.cwd_poe_pwd]
cmd = "poe_test_pwd"
cwd = "${POE_PWD}"
Empty file.
2 changes: 2 additions & 0 deletions tests/fixtures/monorepo_project/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ path = "subproject_1/pyproject.toml"
[[tool.poe.include]]
path = "subproject_2/pyproject.toml"
cwd = "subproject_2"
[[tool.poe.include]]
path = "subproject_3/pyproject.toml"


[tool.poe.tasks.get_cwd_0]
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/monorepo_project/subproject_3/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@



[tool.poe.tasks.get_cwd_3]
interpreter = "python"
shell = "import os; print(os.getcwd())"
cwd = "${POE_PWD}"
59 changes: 59 additions & 0 deletions tests/test_cmd_tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


def test_call_echo_task(run_poe_subproc, projects, esc_prefix):
result = run_poe_subproc("echo", "foo", "!", project="cmds")
assert (
Expand Down Expand Up @@ -63,3 +66,59 @@ def test_cmd_task_with_cwd_option(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_cwd_option_env(run_poe_subproc, poe_project_path):
result = run_poe_subproc("cwd_env", project="cwd", env={"BAR_ENV": "bar"})
assert result.capture == "Poe => poe_test_pwd\n"
assert (
result.stdout
== f'{poe_project_path.joinpath("tests", "fixtures", "cwd_project", "subdir", "bar")}\n'
)
assert result.stderr == ""


def test_cmd_task_with_cwd_option_pwd(run_poe_subproc, poe_project_path):
prev_cwd = os.getcwd()
try:
os.chdir(
poe_project_path.joinpath(
"tests", "fixtures", "cwd_project", "subdir", "foo"
)
)
result = run_poe_subproc("cwd_poe_pwd", project="cwd")
assert result.capture == "Poe => poe_test_pwd\n"
assert (
result.stdout
== f'{poe_project_path.joinpath("tests", "fixtures", "cwd_project", "subdir", "foo")}\n'
)
assert result.stderr == ""
finally:
os.chdir(prev_cwd)


def test_cmd_task_with_cwd_option_pwd_override(run_poe_subproc, poe_project_path):
prev_cwd = os.getcwd()
try:
os.chdir(
poe_project_path.joinpath(
"tests", "fixtures", "cwd_project", "subdir", "foo"
)
)
result = run_poe_subproc(
"cwd_poe_pwd",
project="cwd",
env={
"POE_PWD": poe_project_path.joinpath(
"tests", "fixtures", "cwd_project", "subdir", "bar"
)
},
)
assert result.capture == "Poe => poe_test_pwd\n"
assert (
result.stdout
== f'{poe_project_path.joinpath("tests", "fixtures", "cwd_project", "subdir", "bar")}\n'
)
assert result.stderr == ""
finally:
os.chdir(prev_cwd)
21 changes: 20 additions & 1 deletion tests/test_includes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os


def test_docs_for_include_toml_file(run_poe_subproc):
result = run_poe_subproc(project="includes")
assert (
Expand Down Expand Up @@ -88,7 +91,8 @@ def test_monorepo_contains_only_expected_tasks(run_poe_subproc, projects):
"CONFIGURED TASKS\n"
" get_cwd_0 \n"
" get_cwd_1 \n"
" get_cwd_2 \n\n\n"
" get_cwd_2 \n"
" get_cwd_3 \n\n\n"
)
assert result.stdout == ""
assert result.stderr == ""
Expand Down Expand Up @@ -148,3 +152,18 @@ def test_monorepo_runs_each_task_with_expected_cwd(
"poethepoet/tests/fixtures/monorepo_project/subproject_2\n"
)
assert result.stderr == ""

prev_cwd = os.getcwd()
try:
os.chdir(projects["example"])
result = run_poe_subproc("get_cwd_3", project="monorepo")
assert result.capture == "Poe => import os; print(os.getcwd())\n"
if is_windows:
assert result.stdout.endswith(
"poethepoet\\tests\\fixtures\\example_project\n"
)
else:
assert result.stdout.endswith("poethepoet/tests/fixtures/example_project\n")
assert result.stderr == ""
finally:
os.chdir(prev_cwd)

0 comments on commit 6274e6d

Please sign in to comment.