Skip to content

Commit e8f877e

Browse files
committed
option to skip some envlist from the default envlist #824
1 parent 3b91d63 commit e8f877e

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

changelog/824.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use the os environment variable ``TOX_SKIP_ENV`` to filter out tox environment names from the run list (set by :confval:`envlist`) - by :user:`gaborbernat`

doc/config.rst

+7
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ and will first lookup global tox settings in this section:
7676
* environment variable ``TOXENV``
7777
* ``tox.ini`` file's ``envlist``
7878

79+
.. versionadded:: 3.4.0
80+
81+
What tox environments are ran during the tox invocation can be further filtered
82+
via the operating system environment variable ``TOX_SKIP_ENV`` regular expression
83+
(e.g. ``py27.*`` means **don't** evaluate environments that start with the key ``py27``).
84+
Skipped environments will be logged at level two verbosity level.
85+
7986
.. confval:: ignore_basepython_conflict=True|False(default)
8087

8188
.. versionadded:: 3.1.0

src/tox/session.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,26 @@ def __init__(self, config, popen=subprocess.Popen, Report=Reporter):
381381
self._spec2pkg = {}
382382
self._name2venv = {}
383383
try:
384-
self.venvlist = [self.getvenv(x) for x in self.config.envlist]
384+
self.venvlist = [self.getvenv(x) for x in self.evaluated_env_list()]
385385
except LookupError:
386386
raise SystemExit(1)
387387
except tox.exception.ConfigError as e:
388388
self.report.error(str(e))
389389
raise SystemExit(1)
390390
self._actions = []
391391

392+
def evaluated_env_list(self):
393+
tox_env_filter = os.environ.get("TOX_SKIP_ENV")
394+
tox_env_filter_re = re.compile(tox_env_filter) if tox_env_filter is not None else None
395+
for name in self.config.envlist:
396+
if tox_env_filter_re is not None and tox_env_filter_re.match(name):
397+
msg = "skip environment {}, matches filter {!r}".format(
398+
name, tox_env_filter_re.pattern
399+
)
400+
self.report.verbosity1(msg)
401+
continue
402+
yield name
403+
392404
@property
393405
def hook(self):
394406
return self.config.pluginmanager.hook

tests/unit/session/test_session.py

+70
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,73 @@ def test_skip_install_skip_package(cmd, initproj, mock_venv):
140140
)
141141
result = cmd("--notest")
142142
assert result.ret == 0
143+
144+
145+
@pytest.fixture()
146+
def venv_filter_project(initproj, cmd):
147+
def func(*args):
148+
initproj(
149+
"pkg123-0.7",
150+
filedefs={
151+
"tox.ini": """
152+
[tox]
153+
envlist = {py27,py36}-{nocov,cov,diffcov}{,-extra}
154+
skipsdist = true
155+
156+
[testenv]
157+
skip_install = true
158+
commands = python -c 'print("{envname}")'
159+
"""
160+
},
161+
)
162+
result = cmd(*args)
163+
assert result.ret == 0
164+
active = [i.name for i in result.session.venvlist]
165+
return active, result
166+
167+
yield func
168+
169+
170+
def test_venv_filter_empty_all_active(venv_filter_project, monkeypatch):
171+
monkeypatch.delenv("TOX_SKIP_ENV", raising=False)
172+
active, result = venv_filter_project("-a")
173+
assert result.outlines == [
174+
"py27-nocov",
175+
"py27-nocov-extra",
176+
"py27-cov",
177+
"py27-cov-extra",
178+
"py27-diffcov",
179+
"py27-diffcov-extra",
180+
"py36-nocov",
181+
"py36-nocov-extra",
182+
"py36-cov",
183+
"py36-cov-extra",
184+
"py36-diffcov",
185+
"py36-diffcov-extra",
186+
]
187+
assert active == result.outlines
188+
189+
190+
def test_venv_filter_match_all_none_active(venv_filter_project, monkeypatch):
191+
monkeypatch.setenv("TOX_SKIP_ENV", ".*")
192+
active, result = venv_filter_project("-a")
193+
assert not active
194+
existing_envs = result.outlines
195+
196+
_, result = venv_filter_project("-avv")
197+
for name in existing_envs:
198+
msg = "skip environment {}, matches filter '.*'".format(name)
199+
assert msg in result.outlines
200+
201+
202+
def test_venv_filter_match_some_some_active(venv_filter_project, monkeypatch):
203+
monkeypatch.setenv("TOX_SKIP_ENV", "py27.*")
204+
active, result = venv_filter_project("-avvv")
205+
assert active == [
206+
"py36-nocov",
207+
"py36-nocov-extra",
208+
"py36-cov",
209+
"py36-cov-extra",
210+
"py36-diffcov",
211+
"py36-diffcov-extra",
212+
]

0 commit comments

Comments
 (0)