Skip to content

Commit

Permalink
Allow extending lists with --override foo+=bar
Browse files Browse the repository at this point in the history
Allow appending to a list with += syntax, instead of replacing the
existing value.

Fixes: tox-dev#3087
  • Loading branch information
stefanor committed Aug 11, 2023
1 parent 0a3d578 commit 70c327c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/tox/config/loader/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def __init__(self, value: str) -> None:
key, equal, self.value = value.partition("=")
if not equal:
raise ArgumentTypeError(f"override {value} has no = sign in it")

self.append = False
if key.endswith('+'): # key += value appends to a list
key = key[:-1]
self.append = True

self.namespace, _, self.key = key.rpartition(".")

def __repr__(self) -> str:
Expand Down Expand Up @@ -119,13 +125,16 @@ def load(
:param args: the config load arguments
:return: the converted type
"""
if key in self.overrides:
return _STR_CONVERT.to(self.overrides[key].value, of_type, factory)
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)
future: Future[V] = Future()
with self.build(future, key, of_type, conf, raw, args) as prepared:
converted = self.to(prepared, of_type, factory)
future.set_result(converted)
if override and override.append:
converted += _STR_CONVERT.to(override.value, of_type, factory)
return converted

@contextmanager
Expand Down
12 changes: 12 additions & 0 deletions tests/config/loader/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def test_override_add(flag: str) -> None:
assert value.key == "magic"
assert value.value == "true"
assert value.namespace == ""
assert value.append == False


@pytest.mark.parametrize("flag", ["-x", "--override"])
def test_override_append(flag: str) -> None:
parsed, _, __, ___, ____ = get_options(flag, "magic+=true")
assert len(parsed.override) == 1
value = parsed.override[0]
assert value.key == "magic"
assert value.value == "true"
assert value.namespace == ""
assert value.append == True


def test_override_equals() -> None:
Expand Down
12 changes: 12 additions & 0 deletions tests/config/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
from pathlib import Path
from typing import List

from tests.conftest import ToxIniCreator
from tox.config.loader.api import Override
Expand Down Expand Up @@ -61,6 +62,17 @@ def test_config_override_wins_memory_loader(tox_ini_conf: ToxIniCreator) -> None
assert conf["c"] == "ok"


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


def test_args_are_paths_when_disabled(tox_project: ToxProjectCreator) -> None:
ini = "[testenv]\npackage=skip\ncommands={posargs}\nargs_are_paths=False"
project = tox_project({"tox.ini": ini, "w": {"a.txt": "a"}})
Expand Down

0 comments on commit 70c327c

Please sign in to comment.