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

Support environment files and comments in setenv #1668

Merged
merged 1 commit into from
Sep 1, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaborbernat any specific reason you implemented it with a prefix vs a substitution? If I were to design this, I'd probably go for {dotenv:file_path} there... I'm not judging, it just seems untypical for tox's UX.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't want to worry about conflicts with conditional factors 🤷‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it's not really substitution at all happening here, but different load mechanism. Substitution implies we just string replace that expression with content of the file, but actually here we never do that. Conceptually.

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