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

expand section names #1545

Merged
merged 5 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,8 @@ def __init__(self, config, ini_path, ini_data): # noqa
self._cfg = py.iniconfig.IniConfig(config.toxinipath, ini_data)
previous_line_of = self._cfg.lineof

self.expand_section_names(self._cfg)

def line_of_default_to_zero(section, name=None):
at = previous_line_of(section, name=name)
if at is None:
Expand Down Expand Up @@ -1348,6 +1350,27 @@ def _getenvdata(self, reader, config):
raise tox.exception.ConfigError(msg)
return env_list, all_envs, _split_env(from_config), envlist_explicit

@staticmethod
def expand_section_names(config):
"""Generative section names.

Allow writing section as [testenv:py{36,37}-cov]
The parser will see it as two different sections: [testenv:py36-cov], [testenv:py37-cov]

"""
factor_re = re.compile(r"\{([\w,]+)\}")
to_remove = set()
for section in list(config.sections):
split_section = factor_re.split(section)
for parts in itertools.product(*map(lambda g: g.split(","), split_section)):
section_name = "".join(parts)
if section_name not in config.sections:
config.sections[section_name] = config.sections[section]
to_remove.add(section)

for section in to_remove:
del config.sections[section]


def _split_env(env):
"""if handed a list, action="append" was used for -e """
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,15 @@ def test_getbool(self, newconfig):
assert msg == "key5: boolean value 'yes' needs to be 'True' or 'False'"


def test_expand_section_name(self, newconfig):
config = newconfig("""
[testenv:custom-{one,two,three}-{four,five}-six]
"""
)
assert "testenv:custom-one-five-six" in config._cfg.sections
assert "testenv:custom-{one,two,three}-{four,five}-six" not in config._cfg.sections


class TestIniParserPrefix:
def test_basic_section_access(self, newconfig):
config = newconfig(
Expand Down