Skip to content

Commit

Permalink
Fix environment regression
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondEvil committed Aug 19, 2023
1 parent 29b5e42 commit 8b2bc1f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changelog/3099.bugfix.rst
@@ -0,0 +1 @@
Fix regression introduced in ``4.9.0`` affecting hyphenated and combined environments.
22 changes: 17 additions & 5 deletions src/tox/session/env_select.py
Expand Up @@ -4,7 +4,7 @@
import re
from collections import Counter
from dataclasses import dataclass
from itertools import chain
from itertools import chain, permutations
from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, cast

from tox.config.loader.str_convert import StrConvert
Expand Down Expand Up @@ -155,10 +155,12 @@ def _collect_names(self) -> Iterator[tuple[Iterable[str], bool]]:
cli_envs_not_in_config = set(self._cli_envs) - set(self._state.conf)
if cli_envs_not_in_config:
# allow cli_envs matching ".pkg" and starting with "py" to be implicitly created.
disallowed_cli_envs = [
env for env in cli_envs_not_in_config if not env.startswith("py") and env not in (".pkg",)
]
if disallowed_cli_envs:
disallowed_cli_envs = [env for env in cli_envs_not_in_config if not _is_valid_exception(env)]

# allow disallowed cli envs that match hyphenated combinations
has_match = any(_find_env_match(cli_env, set(self._state.conf)) for cli_env in disallowed_cli_envs)

if disallowed_cli_envs and not has_match:
msg = f"provided environments not found in configuration file: {disallowed_cli_envs}"
raise HandledError(msg)
yield self._cli_envs, True
Expand Down Expand Up @@ -389,6 +391,16 @@ def _mark_provision(self, on: bool, provision_tox_env: str) -> None: # noqa: FB
self._provision = on, provision_tox_env


def _find_env_match(value: str, state_conf: set[str]) -> bool:
return any(value in conf.split("-") for conf in state_conf) or any(
value == "-".join(combo) for combo in permutations(state_conf, 2)
)


def _is_valid_exception(env: str) -> bool:
return env.startswith("py") or env in (".pkg",)


__all__ = [
"register_env_select_flags",
"EnvSelector",
Expand Down
37 changes: 37 additions & 0 deletions tests/session/test_env_select.py
Expand Up @@ -164,3 +164,40 @@ def test_allowed_implicit_cli_envs(env_name: str, tox_project: ToxProjectCreator
outcome.assert_success()
assert env_name in outcome.out
assert not outcome.err


@pytest.mark.parametrize("env_name", ["a", "b", "a-b"])
def test_matches_hyphenated_env(env_name: str, tox_project: ToxProjectCreator) -> None:
tox_ini = """
[tox]
env_list=a-b
[testenv]
package=skip
commands_pre =
a: python -c 'print("a")'
b: python -c 'print("b")'
commands=python -c 'print("ok")'
"""
proj = tox_project({"tox.ini": tox_ini})
outcome = proj.run("r", "-e", env_name)
outcome.assert_success()
assert env_name in outcome.out
assert not outcome.err


@pytest.mark.parametrize("env_name", ["3.9", "3.9-cov"])
def test_matches_combined_env(env_name: str, tox_project: ToxProjectCreator) -> None:
tox_ini = """
[tox]
env_list=3.9
[testenv]
package=skip
commands =
!cov: python -c 'print("without cov")'
cov: python -c 'print("with cov")'
"""
proj = tox_project({"tox.ini": tox_ini})
outcome = proj.run("r", "-e", env_name)
outcome.assert_success()
assert env_name in outcome.out
assert not outcome.err

0 comments on commit 8b2bc1f

Please sign in to comment.