Skip to content

Commit 8527c61

Browse files
fix: report an invalid --skip-env regex as a handled error (#4029)
Co-authored-by: VXNCXNX <vxncxnx@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent fb859dc commit 8527c61

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

docs/changelog/4029.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Report an invalid ``--skip-env``/``TOX_SKIP_ENV`` regular expression as a handled error instead of raising an unhandled
2+
``re.error`` traceback - by :user:`VXNCXNX`

src/tox/session/env_select.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,15 @@ def __init__(self, state: State) -> None:
247247

248248
self._state.conf.core.add_config("labels", dict[str, EnvList], {}, "core labels")
249249
tox_env_filter_regex = getattr(state.conf.options, "skip_env", "").strip()
250-
self._filter_re = re.compile(tox_env_filter_regex) if tox_env_filter_regex else None
250+
self._filter_re = self._compile_filter(tox_env_filter_regex) if tox_env_filter_regex else None
251+
252+
@staticmethod
253+
def _compile_filter(pattern: str) -> re.Pattern[str]:
254+
try:
255+
return re.compile(pattern)
256+
except re.error as exc:
257+
msg = f"invalid environment skip filter {pattern!r}: {exc}"
258+
raise HandledError(msg) from exc
251259

252260
@property
253261
def _cli_envs(self) -> CliEnv | None:

tests/session/test_env_select.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ def test_tox_skip_env_logs(tox_project: ToxProjectCreator, monkeypatch: MonkeyPa
246246
outcome.assert_out_err("ROOT: skip environment mypy, matches filter 'm[y]py'\npy310\npy39\n", "")
247247

248248

249+
@pytest.mark.parametrize("bad_filter", ["[", "(", "*"])
250+
def test_tox_skip_env_invalid_regex(tox_project: ToxProjectCreator, monkeypatch: MonkeyPatch, bad_filter: str) -> None:
251+
monkeypatch.delenv("TOX_SKIP_ENV", raising=False)
252+
project = tox_project({"tox.ini": "[tox]\nenv_list = py3{10,9},mypy"})
253+
254+
leaked: BaseException | None = None
255+
try:
256+
outcome = project.run("l", "--no-desc", "--skip-env", bad_filter)
257+
except Exception as exception: # ruff:ignore[blind-except]
258+
leaked = exception
259+
assert leaked is None, f"unhandled {type(leaked).__name__}: {leaked}"
260+
261+
outcome.assert_failed()
262+
assert f"HandledError| invalid environment skip filter {bad_filter!r}" in outcome.out
263+
264+
249265
def test_multiple_e_flags_are_additive(tox_project: ToxProjectCreator) -> None:
250266
proj = tox_project({"tox.ini": "[tox]\nenv_list=a,b,c"})
251267
outcome = proj.run("c", "-e", "a", "-e", "b", "-k", "env_name")

0 commit comments

Comments
 (0)