Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace undefined settings with overrides when appending #3101

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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