Skip to content

Commit be5373b

Browse files
authored
Keep v3 injected environment variable list (#2313)
1 parent dae4fc1 commit be5373b

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

docs/changelog/2259.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Insert ``TOX_WORK_DIR``, ``TOX_ENV_NAME``, ``TOX_ENV_DIR`` and ``VIRTUAL_ENV`` environment variables for each tox
2+
environments environment variables to keep contract with tox version 3 - by :user:`gaborbernat`.

src/tox/tox_env/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ def environment_variables(self) -> dict[str, str]:
308308
result["PATH"] = self._make_path()
309309
for key in set_env:
310310
result[key] = set_env.load(key)
311+
result["TOX_ENV_NAME"] = self.name
312+
result["TOX_WORK_DIR"] = str(self.core["work_dir"])
313+
result["TOX_ENV_DIR"] = str(self.conf["env_dir"])
311314
return result
312315

313316
@staticmethod

src/tox/tox_env/python/virtual_env/api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,9 @@ def env_bin_dir(self) -> Path:
161161
@property
162162
def runs_on_platform(self) -> str:
163163
return sys.platform
164+
165+
@property
166+
def environment_variables(self) -> dict[str, str]:
167+
environment_variables = super().environment_variables
168+
environment_variables["VIRTUAL_ENV"] = str(self.conf["env_dir"])
169+
return environment_variables

tests/session/cmd/test_sequential.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import json
4+
import os
45
import re
56
import sys
67
from pathlib import Path
@@ -417,3 +418,22 @@ def test_sequential_clears_pkg_at_most_once(tox_project: ToxProjectCreator, demo
417418
project = tox_project({"tox.ini": ""})
418419
result = project.run("r", "--root", str(demo_pkg_inline), "-e", "a,b", "-r")
419420
result.assert_success()
421+
422+
423+
def test_sequential_inserted_env_vars(tox_project: ToxProjectCreator, demo_pkg_inline: Path) -> None:
424+
ini = """
425+
[testenv]
426+
commands=python -c 'import os; [print(f"{k}={v}") for k, v in os.environ.items() if \
427+
k.startswith("TOX_") or k == "VIRTUAL_ENV"]'
428+
"""
429+
project = tox_project({"tox.ini": ini})
430+
result = project.run("r", "--root", str(demo_pkg_inline))
431+
result.assert_success()
432+
433+
assert re.search(f"TOX_PACKAGE={re.escape(str(project.path))}.*.tar.gz{os.linesep}", result.out)
434+
assert f"TOX_ENV_NAME=py{os.linesep}" in result.out
435+
work_dir = project.path / ".tox" / "4"
436+
assert f"TOX_WORK_DIR={work_dir}{os.linesep}" in result.out
437+
env_dir = work_dir / "py"
438+
assert f"TOX_ENV_DIR={env_dir}{os.linesep}" in result.out
439+
assert f"VIRTUAL_ENV={env_dir}{os.linesep}" in result.out

0 commit comments

Comments
 (0)