Skip to content

Commit

Permalink
Work with _OLD_VIRTUAL_PATH env var like a venv activate script
Browse files Browse the repository at this point in the history
Also set _OLD_VIRTUAL_PYTHONHOME like in an activate script.

This makes the virtualenv activation behaviour of the poetry and virtualenv
executors behave in a way closer to the standard.

Fix a test that previously relied on accidental PATH inheritance from the
root project venv, which is no longer possible.
  • Loading branch information
nat-n committed Sep 23, 2023
1 parent e6e031b commit f487993
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 10 additions & 2 deletions poethepoet/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,23 @@ def valid(self) -> bool:

def get_env_vars(self, base_env: Mapping[str, str]) -> Dict[str, str]:
bin_dir = str(self.bin_dir())
path_var = os.environ.get("PATH", "")
# Revert path update from existing virtualenv if applicable
path_var = os.environ.get("_OLD_VIRTUAL_PATH", "") or os.environ.get("PATH", "")
old_path_var = path_var

if not path_var.startswith(bin_dir):
path_delim = ";" if self._is_windows else ":"
path_var = bin_dir + path_delim + path_var

result = dict(base_env, VIRTUAL_ENV=str(self.path), PATH=path_var)
result = dict(
base_env,
VIRTUAL_ENV=str(self.path),
_OLD_VIRTUAL_PATH=old_path_var,
PATH=path_var,
)

if "PYTHONHOME" in result:
result["_OLD_VIRTUAL_PYTHONHOME"] = result["PYTHONHOME"]
result.pop("PYTHONHOME")

return result
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ def use_virtualenv(
did_exist = location.is_dir()
assert not require_empty or not did_exist, (
f"Test requires no directory already exists at {location}, "
"maybe try delete it and run again"
"maybe try delete it (via `poe clean`) and try again"
)

# create new virtualenv
Expand Down Expand Up @@ -352,7 +352,7 @@ def try_rm_dir(location: Path):
shutil.rmtree(location)
except:
print(
"Cleanup failed. You might need to run poe clean before tests can be "
"Cleanup failed. You might need to run `poe clean` before tests can be "
"run again."
)

Expand Down
6 changes: 4 additions & 2 deletions tests/test_executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def test_virtualenv_executor_fails_without_venv_dir(run_poe_subproc, projects):


@pytest.mark.slow
def test_virtualenv_executor_activates_venv(
def test_virtualenv_executor_activates_venv( # TODO: fix dependency to get poe_test_env!!
run_poe_subproc, with_virtualenv_and_venv, projects
):
venv_path = projects["venv"].joinpath("myvenv")
for _ in with_virtualenv_and_venv(venv_path):
for _ in with_virtualenv_and_venv(
venv_path, ["./tests/fixtures/packages/poe_test_helpers"]
):
result = run_poe_subproc("show-env", project="venv")
assert result.capture == "Poe => poe_test_env\n"
assert f"VIRTUAL_ENV={venv_path}" in result.stdout
Expand Down

0 comments on commit f487993

Please sign in to comment.