Skip to content

Commit

Permalink
Support environment files and comments in setenv (#1668)
Browse files Browse the repository at this point in the history
Signed-off-by: Bernat Gabor <bgabor8@bloomberg.net>
  • Loading branch information
gaborbernat committed Sep 1, 2020
1 parent a790350 commit 8a25fa6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changelog/1667.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support for comments within ``setenv`` and environment files via the ``files|`` prefix. - by :user:`gaborbernat`
8 changes: 8 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,14 @@ Complete list of settings that you can put into ``testenv*`` sections:
setenv =
PYTHONPATH = {env:PYTHONPATH}{:}{toxinidir}
.. versionadded:: 3.20

Support for comments. Lines starting with ``#`` are ignored.

Support for environment files. Lines starting with the ``file|`` contain path to a environment
file to load. Rules within the environment file are the same as within the ``setenv``
(same replacement and comment support).

.. conf:: passenv ^ SPACE-SEPARATED-GLOBNAMES

.. versionadded:: 2.0
Expand Down
18 changes: 13 additions & 5 deletions src/tox/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,13 +1606,21 @@ def _getdict(self, value, default, sep, replace=True):
if value is None or not replace:
return default or {}

d = {}
env_values = {}
for line in value.split(sep):
if line.strip():
name, rest = line.split("=", 1)
d[name.strip()] = rest.strip()

return d
if line.startswith("#"): # comment lines are ignored
pass
elif line.startswith("file|"): # file markers contain paths to env files
file_path = line[5:].strip()
if os.path.exists(file_path):
with open(file_path, "rt") as file_handler:
content = file_handler.read()
env_values.update(self._getdict(content, "", sep, replace))
else:
name, value = line.split("=", 1)
env_values[name.strip()] = value.strip()
return env_values

def getfloat(self, name, default=None, replace=True):
s = self.getstring(name, default, replace=replace)
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import py
import pytest
from pluggy import PluginManager
from six import PY2

import tox
from tox.config import (
Expand Down Expand Up @@ -2668,6 +2669,49 @@ def test_setenv_cross_section_mixed(self, monkeypatch, newconfig):
assert envconfig.setenv["NOT_TEST"] == "defaultvalue"
assert envconfig.setenv["y"] == "7"

def test_setenv_comment(self, newconfig):
"""Check that setenv ignores comments."""
envconfig = newconfig(
"""
[testenv]
setenv =
# MAGIC = yes
""",
).envconfigs["python"]
assert "MAGIC" not in envconfig.setenv

@pytest.mark.parametrize(
"content, has_magic",
[
(None, False),
("\n", False),
("#MAGIC = yes", False),
("MAGIC=yes", True),
("\nMAGIC = yes", True),
],
)
def test_setenv_env_file(self, newconfig, content, has_magic, tmp_path):
"""Check that setenv handles env files."""
env_path = tmp_path / ".env" if content else None
if content:
env_path.write_text(content.decode() if PY2 else content)
env_config = newconfig(
"""
[testenv]
setenv =
ALPHA = 1
file| {}
""".format(
env_path,
),
).envconfigs["python"]
envs = env_config.setenv.definitions
assert envs["ALPHA"] == "1"
if has_magic:
assert envs["MAGIC"] == "yes"
else:
assert "MAGIC" not in envs


class TestIndexServer:
def test_indexserver(self, newconfig):
Expand Down

0 comments on commit 8a25fa6

Please sign in to comment.