Skip to content

Commit

Permalink
expand section names (tox-dev#1545)
Browse files Browse the repository at this point in the history
* expand section names

* fix linting

* Added changelog

* trim whitespaces around factor names

* added docs
  • Loading branch information
bruchar1 authored and ssbarnea committed Apr 19, 2021
1 parent e7851c5 commit fe48d2f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changelog/1545.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow generative section name expansion. - by :user:`bruchar1`
21 changes: 21 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,27 @@ are stripped, so the following line defines the same environment names::
flake


.. _generative-sections:

Generative section names
++++++++++++++++++++++++

.. versionadded:: 3.15

Using similar syntax, it is possible to generate sections::

[testenv:py{27,36}-flake]

This is equivalent to defining distinct sections::

$ tox -a
py27-flake
py36-flake

It is useful when you need an environment different from the default one,
but still want to take advantage of factor-conditional settings.


.. _factors:

Factors and factor-conditional settings
Expand Down
23 changes: 23 additions & 0 deletions docs/example/basic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,29 @@ use :ref:`generative-envlist` and :ref:`conditional settings <factors>` to expre
# mocking sqlite on 2.7 and 3.6 if factor "sqlite" is present
py{27,36}-sqlite: mock
Using generative section names
------------------------------

Suppose you have some binary packages, and need to run tests both in 32 and 64 bits.
You also want an environment to create your virtual env for the developers.

.. code-block:: ini
[testenv]
basepython =
py38-x86: python3.8-32
py38-x64: python3.8-64
commands = pytest
[testenv:py38-{x86,x64}-venv]
usedevelop = true
envdir =
x86: .venv-x86
x64: .venv-x64
commands =
Prevent symbolic links in virtualenv
------------------------------------
By default virtualenv will use symlinks to point to the system's python files, modules, etc.
Expand Down
24 changes: 24 additions & 0 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,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 @@ -1353,6 +1355,28 @@ 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"\{\s*([\w\s,]+)\s*\}")
split_re = re.compile(r"\s*,\s*")
to_remove = set()
for section in list(config.sections):
split_section = factor_re.split(section)
for parts in itertools.product(*map(split_re.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 @@ -859,6 +859,15 @@ def test_getbool(self, newconfig):
(msg,) = excinfo.value.args
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):
Expand Down

0 comments on commit fe48d2f

Please sign in to comment.