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

Custom prefix in .ini source #2927

Merged
merged 3 commits into from
Feb 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
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"