Skip to content

Commit 4b90bae

Browse files
committed
pass through CI as __TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI
1 parent 59aaee9 commit 4b90bae

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

docs/changelog/3442.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add ``__TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI``, which passes through the ``CI`` variable if present. This is intended for use by other libraries to detect if tox is running under CI.

docs/config.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ Base options
561561
- ✅
562562
- ❌
563563

564+
If the environment variable ``CI`` is present, ``__TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI`` will be set to the value of ``CI``. The ``CI`` variable itself will not be passed through.
565+
564566
More environment variable-related information can be found in :ref:`environment variable substitutions`.
565567

566568
.. conf::

src/tox/tox_env/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ def environment_variables(self) -> dict[str, str]:
379379
result["TOX_ENV_NAME"] = self.name
380380
result["TOX_WORK_DIR"] = str(self.core["work_dir"])
381381
result["TOX_ENV_DIR"] = str(self.conf["env_dir"])
382+
if (ci := os.environ.get("CI")) is not None:
383+
result["__TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI"] = ci
382384
return result
383385

384386
@staticmethod
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
5+
import pytest
6+
7+
if TYPE_CHECKING:
8+
from tox.pytest import ToxProjectCreator
9+
10+
11+
@pytest.mark.parametrize("value", ["1", "0", "", "arbitrary_value"])
12+
def test_ci_passthrough_present(value: str, tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch) -> None:
13+
monkeypatch.setenv("CI", value)
14+
prj = tox_project({"tox.ini": "[testenv]\npackage=skip\ncommands=python -c 'print(0)'\n"})
15+
execute_calls = prj.patch_execute(lambda _r: 0)
16+
result = prj.run("r", "-e", "py")
17+
result.assert_success()
18+
req = execute_calls.call_args[0][3]
19+
assert req.env["__TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI"] == value
20+
21+
22+
def test_ci_passthrough_absent(tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch) -> None:
23+
monkeypatch.delenv("CI", raising=False)
24+
prj = tox_project({"tox.ini": "[testenv]\npackage=skip\ncommands=python -c 'print(0)'\n"})
25+
execute_calls = prj.patch_execute(lambda _r: 0)
26+
result = prj.run("r", "-e", "py")
27+
result.assert_success()
28+
req = execute_calls.call_args[0][3]
29+
assert "__TOX_ENVIRONMENT_VARIABLE_ORIGINAL_CI" not in req.env

0 commit comments

Comments
 (0)