Skip to content

Commit

Permalink
Custom prefix in .ini source (#2927)
Browse files Browse the repository at this point in the history
* test_source_ini: custom config can overlap testenv

regression test for plugin behavior in #2926

* IniSource.get_loader: check section.prefix

ensure that loaders returned from .ini source are bound
to the correct section prefix, if specified.

add comment explaining why the code must look up the name
in the _section_mapping

fix #2926

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
masenf and pre-commit-ci[bot] committed Feb 21, 2023
1 parent 23ebd04 commit f4fcb7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/changelog/2926.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Plugins are now able to access tox.ini config sections using a custom prefix with the same suffix / name as a tox
``testenv`` - by :user:`masenf`
9 changes: 7 additions & 2 deletions src/tox/config/source/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ def sections(self) -> Iterator[IniSection]:
yield IniSection.from_key(section)

def get_loader(self, section: Section, override_map: OverrideMap) -> IniLoader | None:
sections = self._section_mapping.get(section.name)
key = sections[0] if sections else section.key
# look up requested section name in the generative testenv mapping to find the real config source
for key in self._section_mapping.get(section.name) or []:
if section.prefix is None or Section.from_key(key).prefix == section.prefix:
break
else:
# if no matching section/prefix is found, use the requested section key as-is (for custom prefixes)
key = section.key
if self._parser.has_section(key):
return IniLoader(
section=section,
Expand Down
26 changes: 26 additions & 0 deletions tests/config/source/test_source_ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from pathlib import Path

from tests.conftest import ToxIniCreator
from tox.config.loader.section import Section
from tox.config.sets import ConfigSet
from tox.config.source.ini import IniSource


Expand All @@ -22,3 +24,27 @@ def test_source_ini_ignore_invalid_factor_filters(tmp_path: Path) -> None:
loader = IniSource(tmp_path, content="[a]\nb= if c: d")
res = list(loader.envs({"env_list": []})) # type: ignore
assert not res


def test_source_ini_custom_non_testenv_sections(tox_ini_conf: ToxIniCreator) -> None:
"""Validate that a plugin can load section with custom prefix overlapping testenv name."""

class CustomConfigSet(ConfigSet):
def register_config(self) -> None:
self.add_config(
keys=["a"],
of_type=str,
default="",
desc="d",
)

config = tox_ini_conf("[testenv:foo]\n[custom:foo]\na = b")
known_envs = list(config._src.envs(config.core))
assert known_envs
custom_section = config.get_section_config(
section=Section("custom", "foo"),
base=[],
of_type=CustomConfigSet,
for_env=None,
)
assert custom_section["a"] == "b"

0 comments on commit f4fcb7b

Please sign in to comment.