Skip to content

Commit

Permalink
Evaluate factor conditions for command keys
Browse files Browse the repository at this point in the history
Due to the order how the configuration got processed, factors were not
identified and thus not evaluated for `command` keys.

This has been fixed now.

Closes #2002
  • Loading branch information
jugmac00 authored Apr 14, 2021
1 parent 8eb7b3a commit cd0a1b6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2002.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Evaluate factor conditions for ``command`` keys - by :user:`jugmac00`.
7 changes: 4 additions & 3 deletions src/tox/config/loader/ini/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def __init__(self, section: str, parser: ConfigParser, overrides: List[Override]

def load_raw(self, key: str, conf: Optional["Config"], env_name: Optional[str]) -> str:
value = self._section[key]
collapsed_newlines = value.replace("\r", "").replace("\\\n", "") # collapse explicit new-line escape

# strip comments
elements: List[str] = []
for line in collapsed_newlines.split("\n"):
for line in value.split("\n"):
if not line.startswith("#"):
part = _COMMENTS.sub("", line)
elements.append(part.replace("\\#", "#"))
Expand All @@ -43,7 +43,8 @@ def load_raw(self, key: str, conf: Optional["Config"], env_name: Optional[str])
factor_filtered = strip_comments # we don't support factor and replace functionality there
else:
factor_filtered = filter_for_env(strip_comments, env_name) # select matching factors
return factor_filtered
collapsed = factor_filtered.replace("\r", "").replace("\\\n", "") # collapse explicit new-line escape
return collapsed

@contextmanager
def build(
Expand Down
9 changes: 9 additions & 0 deletions tests/config/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

from tests.conftest import ToxIniCreator
from tox.config.main import Config


@pytest.fixture()
def empty_config(tox_ini_conf: ToxIniCreator) -> Config:
return tox_ini_conf("")
26 changes: 25 additions & 1 deletion tests/config/loader/ini/test_factor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from configparser import ConfigParser
from textwrap import dedent
from typing import List
from typing import Callable, List

import pytest

from tests.conftest import ToxIniCreator
from tox.config.loader.ini import IniLoader
from tox.config.loader.ini.factor import filter_for_env, find_envs
from tox.config.main import Config


def test_factor_env_discover_empty() -> None:
Expand Down Expand Up @@ -146,3 +149,24 @@ def test_factor_config_no_env_list_creates_env(tox_ini_conf: ToxIniCreator) -> N
)

assert list(config) == ["py37-django15", "py37-django16", "py36"]


@pytest.mark.parametrize(
("env", "result"),
[
("py35", "python -m coverage html -d cov"),
("py36", "python -m coverage html -d cov\n--show-contexts"),
],
)
def test_ini_loader_raw_with_factors(
mk_ini_conf: Callable[[str], ConfigParser], env: str, result: str, empty_config: Config
) -> None:
commands = "python -m coverage html -d cov \n !py35: --show-contexts"
loader = IniLoader(
section="testenv",
parser=mk_ini_conf(f"[tox]\nenvlist=py35,py36\n[testenv]\ncommands={commands}"),
overrides=[],
core_prefix="tox",
)
outcome = loader.load_raw(key="commands", conf=empty_config, env_name=env)
assert outcome == result
7 changes: 0 additions & 7 deletions tests/config/test_main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import pytest

from tests.conftest import ToxIniCreator
from tox.config.loader.api import Override
from tox.config.loader.memory import MemoryLoader
from tox.config.main import Config
from tox.config.sets import ConfigSet


@pytest.fixture()
def empty_config(tox_ini_conf: ToxIniCreator) -> Config:
return tox_ini_conf("")


def test_empty_config_repr(empty_config: Config) -> None:
text = repr(empty_config)
assert str(empty_config.core["tox_root"]) in text
Expand Down

0 comments on commit cd0a1b6

Please sign in to comment.