Skip to content

Commit a3d3ec0

Browse files
authored
Show installed packages after setup in CI envs (#2794)
Resolves #2685
1 parent d8c4cb0 commit a3d3ec0

File tree

11 files changed

+119
-20
lines changed

11 files changed

+119
-20
lines changed

docs/changelog/2685.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Restore tox 3 behaviour of showing the output of pip freeze, however now only active when running inside a CI
2+
environment - by :user:`gaborbernat`.

pyproject.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
22
build-backend = "hatchling.build"
3-
requires = ["hatchling>=1.11.1", "hatch-vcs>=0.3"]
3+
requires = ["hatchling>=1.12", "hatch-vcs>=0.3"]
44

55
[project]
66
name = "tox"
@@ -25,18 +25,18 @@ dependencies = [
2525
"chardet>=5.1",
2626
"colorama>=0.4.6",
2727
"packaging>=22",
28-
"platformdirs>=2.6",
28+
"platformdirs>=2.6.2",
2929
"pluggy>=1",
3030
"pyproject-api>=1.2.1",
3131
'tomli>=2.0.1; python_version < "3.11"',
3232
"virtualenv>=20.17.1",
33-
"filelock>=3.8.2",
33+
"filelock>=3.9",
3434
'importlib-metadata>=5.2; python_version < "3.8"',
3535
'typing-extensions>=4.4; python_version < "3.8"',
3636
]
3737
optional-dependencies.docs = [
3838
"furo>=2022.12.7",
39-
"sphinx>=5.3",
39+
"sphinx>=6",
4040
"sphinx-argparse-cli>=1.10",
4141
"sphinx-autodoc-typehints>=1.19.5",
4242
"sphinx-copybutton>=0.5.1",
@@ -52,7 +52,7 @@ optional-dependencies.testing = [
5252
"distlib>=0.3.6",
5353
"flaky>=3.7",
5454
"hatch-vcs>=0.3",
55-
"hatchling>=1.11.1",
55+
"hatchling>=1.12",
5656
"psutil>=5.9.4",
5757
"pytest>=7.2",
5858
"pytest-cov>=4",

src/tox/tox_env/python/api.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44
from __future__ import annotations
55

6+
import logging
67
import sys
78
from abc import ABC, abstractmethod
89
from pathlib import Path
@@ -14,6 +15,7 @@
1415
from tox.config.main import Config
1516
from tox.tox_env.api import ToxEnv, ToxEnvCreateArgs
1617
from tox.tox_env.errors import Fail, Recreate, Skip
18+
from tox.util.ci import is_ci
1719

1820

1921
class VersionInfo(NamedTuple):
@@ -212,9 +214,13 @@ def prepend_env_var_path(self) -> list[Path]:
212214
def _done_with_setup(self) -> None:
213215
"""called when setup is done"""
214216
super()._done_with_setup()
215-
if self.journal:
217+
running_in_ci = is_ci()
218+
if self.journal or running_in_ci:
216219
outcome = self.installer.installed()
217-
self.journal["installed_packages"] = outcome
220+
if self.journal:
221+
self.journal["installed_packages"] = outcome
222+
if running_in_ci:
223+
logging.warning(",".join(outcome))
218224

219225
def python_cache(self) -> dict[str, Any]:
220226
return {

src/tox/tox_env/python/pip/pip_install.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from packaging.requirements import Requirement
88

9-
from tox.config.cli.parser import DEFAULT_VERBOSITY
109
from tox.config.main import Config
1110
from tox.config.types import Command
1211
from tox.execute.request import StdinSource
@@ -69,12 +68,7 @@ def post_process_install_command(self, cmd: Command) -> Command:
6968

7069
def installed(self) -> list[str]:
7170
cmd: Command = self._env.conf["list_dependencies_command"]
72-
result = self._env.execute(
73-
cmd=cmd.args,
74-
stdin=StdinSource.OFF,
75-
run_id="freeze",
76-
show=self._env.options.verbosity > DEFAULT_VERBOSITY,
77-
)
71+
result = self._env.execute(cmd=cmd.args, stdin=StdinSource.OFF, run_id="freeze", show=False)
7872
result.assert_success()
7973
return result.out.splitlines()
8074

src/tox/util/ci.py

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+
import os
4+
5+
_ENV_VARS = { # per https://adamj.eu/tech/2020/03/09/detect-if-your-tests-are-running-on-ci
6+
"CI": None, # generic flag
7+
"TF_BUILD": "true", # Azure Pipelines
8+
"bamboo.buildKey": None, # Bamboo
9+
"BUILDKITE": "true", # Buildkite
10+
"CIRCLECI": "true", # Circle CI
11+
"CIRRUS_CI": "true", # Cirrus CI
12+
"CODEBUILD_BUILD_ID": None, # CodeBuild
13+
"GITHUB_ACTIONS": "true", # GitHub Actions
14+
"GITLAB_CI": None, # GitLab CI
15+
"HEROKU_TEST_RUN_ID": None, # Heroku CI
16+
"BUILD_ID": None, # Hudson
17+
"TEAMCITY_VERSION": None, # TeamCity
18+
"TRAVIS": "true", # Travis CI
19+
}
20+
21+
22+
def is_ci() -> bool:
23+
""":return: a flag indicating if running inside a CI env or not"""
24+
return any(e in os.environ if v is None else os.environ.get(e) == v for e, v in _ENV_VARS.items())
25+
26+
27+
__all__ = [
28+
"is_ci",
29+
]

tests/tox_env/python/test_python_api.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,13 @@ def test_python_set_hash_seed_incorrect(tox_project: ToxProjectCreator) -> None:
179179
result = tox_project({"tox.ini": ""}).run("r", "-e", "py", "--hashseed", "ok")
180180
result.assert_failed(2)
181181
assert "tox run: error: argument --hashseed: invalid literal for int() with base 10: 'ok'" in result.err
182+
183+
184+
@pytest.mark.parametrize("in_ci", [True, False])
185+
def test_list_installed_deps(in_ci: bool, tox_project: ToxProjectCreator, mocker: MockerFixture) -> None:
186+
mocker.patch("tox.tox_env.python.api.is_ci", return_value=in_ci)
187+
result = tox_project({"tox.ini": "[testenv]\nskip_install = true"}).run("r", "-e", "py")
188+
if in_ci:
189+
assert "py: pip==" in result.out
190+
else:
191+
assert "py: pip==" not in result.out

tests/tox_env/test_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
from tox.pytest import ToxProjectCreator

tests/tox_env/test_tox_env_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
24

35
from tox.pytest import ToxProjectCreator

tests/util/test_ci.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from __future__ import annotations
2+
3+
import pytest
4+
5+
from tox.util.ci import _ENV_VARS, is_ci
6+
7+
8+
@pytest.mark.parametrize(
9+
"env_var",
10+
{
11+
"CI": None, # generic flag
12+
"TF_BUILD": "true", # Azure Pipelines
13+
"bamboo.buildKey": None, # Bamboo
14+
"BUILDKITE": "true", # Buildkite
15+
"CIRCLECI": "true", # Circle CI
16+
"CIRRUS_CI": "true", # Cirrus CI
17+
"CODEBUILD_BUILD_ID": None, # CodeBuild
18+
"GITHUB_ACTIONS": "true", # GitHub Actions
19+
"GITLAB_CI": None, # GitLab CI
20+
"HEROKU_TEST_RUN_ID": None, # Heroku CI
21+
"BUILD_ID": None, # Hudson
22+
"TEAMCITY_VERSION": None, # TeamCity
23+
"TRAVIS": "true", # Travis CI
24+
}.items(),
25+
ids=lambda v: v[0], # type: ignore
26+
)
27+
def test_is_ci(env_var: tuple[str, str | None], monkeypatch: pytest.MonkeyPatch) -> None:
28+
for var in _ENV_VARS:
29+
monkeypatch.delenv(var, raising=False)
30+
monkeypatch.setenv(env_var[0], env_var[1] or "")
31+
assert is_ci()
32+
33+
34+
@pytest.mark.parametrize(
35+
"env_var",
36+
{
37+
"TF_BUILD": "", # Azure Pipelines
38+
"BUILDKITE": "", # Buildkite
39+
"CIRCLECI": "", # Circle CI
40+
"CIRRUS_CI": "", # Cirrus CI
41+
"GITHUB_ACTIONS": "", # GitHub Actions
42+
"TRAVIS": "", # Travis CI
43+
}.items(),
44+
ids=lambda v: v[0], # type: ignore
45+
)
46+
def test_is_ci_bad_set(env_var: tuple[str, str], monkeypatch: pytest.MonkeyPatch) -> None:
47+
for var in _ENV_VARS:
48+
monkeypatch.delenv(var, raising=False)
49+
monkeypatch.setenv(env_var[0], env_var[1])
50+
assert not is_ci()
51+
52+
53+
def test_is_ci_not(monkeypatch: pytest.MonkeyPatch) -> None:
54+
for var in _ENV_VARS:
55+
monkeypatch.delenv(var, raising=False)
56+
assert not is_ci()

tox.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ commands =
6060

6161
[testenv:docs]
6262
description = build documentation
63-
basepython = python3.10
6463
extras =
6564
docs
6665
commands =
@@ -83,7 +82,7 @@ commands =
8382
description = do a release, required posarg of the version number
8483
skip_install = true
8584
deps =
86-
gitpython>=3.1.29
85+
gitpython>=3.1.30
8786
packaging>=22
8887
towncrier>=22.12
8988
commands =

0 commit comments

Comments
 (0)