Skip to content

Commit

Permalink
Replace undefined settings with overrides when appending (#3101)
Browse files Browse the repository at this point in the history
Co-authored-by: Bernát Gábor <bgabor8@bloomberg.net>
  • Loading branch information
stefanor and gaborbernat committed Aug 21, 2023
1 parent 0ac7121 commit 6b8e83a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions docs/changelog/3100.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``--override foo+=bar`` appending syntax will now work correctly when ``foo`` wasn't defined in ``tox.ini``.
28 changes: 17 additions & 11 deletions src/tox/config/loader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Loader(Convert[T]):

def __init__(self, section: Section, overrides: list[Override]) -> None:
self._section = section
self.overrides = {o.key: o for o in overrides}
self.overrides: dict[str, Override] = {o.key: o for o in overrides}
self.parent: Loader[Any] | None = None

@property
Expand Down Expand Up @@ -130,18 +130,24 @@ def load( # noqa: PLR0913
from tox.config.set_env import SetEnv

override = self.overrides.get(key)
if override and not override.append:
return _STR_CONVERT.to(override.value, of_type, factory)
raw = self.load_raw(key, conf, args.env_name)
if override:
converted_override = _STR_CONVERT.to(override.value, of_type, factory)
if not override.append:
return converted_override
try:
raw = self.load_raw(key, conf, args.env_name)
except KeyError:
if override:
return converted_override
raise
converted = self.build(key, of_type, factory, conf, raw, args)
if override and override.append:
appends = _STR_CONVERT.to(override.value, of_type, factory)
if isinstance(converted, list) and isinstance(appends, list):
converted += appends
elif isinstance(converted, dict) and isinstance(appends, dict):
converted.update(appends)
elif isinstance(converted, SetEnv) and isinstance(appends, SetEnv):
converted.update(appends, override=True)
if isinstance(converted, list) and isinstance(converted_override, list):
converted += converted_override
elif isinstance(converted, dict) and isinstance(converted_override, dict):
converted.update(converted_override)
elif isinstance(converted, SetEnv) and isinstance(converted_override, SetEnv):
converted.update(converted_override, override=True)
else:
msg = "Only able to append to lists and dicts"
raise ValueError(msg)
Expand Down
11 changes: 11 additions & 0 deletions tests/config/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ def test_config_override_appends_to_list(tox_ini_conf: ToxIniCreator) -> None:
assert conf["passenv"] == ["foo", "bar"]


def test_config_override_appends_to_empty_list(tox_ini_conf: ToxIniCreator) -> None:
conf = tox_ini_conf("[testenv]", override=[Override("testenv.passenv+=bar")]).get_env("testenv")
conf.add_config("passenv", of_type=List[str], default=[], desc="desc")
assert conf["passenv"] == ["bar"]


def test_config_override_appends_to_setenv(tox_ini_conf: ToxIniCreator) -> None:
example = """
[testenv]
Expand All @@ -87,6 +93,11 @@ def test_config_override_appends_to_setenv(tox_ini_conf: ToxIniCreator) -> None:
assert conf["setenv"].load("baz") == "quux"


def test_config_override_appends_to_empty_setenv(tox_ini_conf: ToxIniCreator) -> None:
conf = tox_ini_conf("[testenv]", override=[Override("testenv.setenv+=foo=bar")]).get_env("testenv")
assert conf["setenv"].load("foo") == "bar"


def test_config_override_cannot_append(tox_ini_conf: ToxIniCreator) -> None:
example = """
[testenv]
Expand Down

0 comments on commit 6b8e83a

Please sign in to comment.