Skip to content

Commit 9608e23

Browse files
obestwalterjugmac00eumiro
authored
fix replacement of sections with dash (#2000)
Co-authored-by: Jürgen Gmach <[email protected]> Co-authored-by: Miroslav Šedivý <[email protected]>
1 parent 2b7f522 commit 9608e23

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

docs/changelog/1985.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Enable replacements (a.k.a section substitions) for section names containing a dash in sections
2+
without the ``testenv:`` prefix - by :user:`jugmac00`, :user:`obestwalter`, :user:`eumiro`.

src/tox/config/loader/ini/replace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _replace_match(
8989

9090
_REPLACE_REF = re.compile(
9191
rf"""
92-
(\[(?P<full_env>{BASE_TEST_ENV}(:(?P<env>[^]]+))?|(?P<section>\w+))\])? # env/section
92+
(\[(?P<full_env>{BASE_TEST_ENV}(:(?P<env>[^]]+))?|(?P<section>[-\w]+))\])? # env/section
9393
(?P<key>[a-zA-Z0-9_]+) # key
9494
(:(?P<default>.*))? # default value
9595
""",

tests/config/loader/ini/replace/test_replace_tox_env.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,18 @@ def test_replace_from_tox_other_tox_section_same_name(tox_ini_conf: ToxIniCreato
151151
conf_a = tox_ini_conf("[testenv:a]\nx={[testenv:b]c}\nc=d\n[testenv:b]}").get_env("a")
152152
conf_a.add_config(keys="x", of_type=str, default="", desc="d")
153153
assert conf_a["x"] == "{[testenv:b]c}"
154+
155+
156+
@pytest.mark.parametrize(
157+
("env_name", "exp"),
158+
[
159+
("testenv:foobar", "1"),
160+
("testenv:foo-bar", "1"),
161+
("foo-bar", "1"),
162+
("foobar", "1"),
163+
],
164+
)
165+
def test_replace_valid_section_names(tox_ini_conf: ToxIniCreator, env_name: str, exp: str) -> None:
166+
conf_a = tox_ini_conf(f"[{env_name}]\na={exp}\n[testenv:a]\nx = {{[{env_name}]a}}").get_env("a")
167+
conf_a.add_config(keys="x", of_type=str, default="o", desc="o")
168+
assert conf_a["x"] == exp

0 commit comments

Comments
 (0)