Skip to content

Commit

Permalink
Fail on blanks in passed env vars (#2671)
Browse files Browse the repository at this point in the history
Co-authored-by: Eric L <ewl+git@lavar.de>
Co-authored-by: Bernát Gábor <bgabor8@bloomberg.net>
Resolves #2658
  • Loading branch information
ericzolf committed Dec 11, 2022
1 parent be3f8a1 commit f4840a0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changelog/2658.feature.rst
@@ -0,0 +1 @@
Fail on :ref:`pass_env`/:ref:`passenv` entries containing whitespace - by :user:`ericzolf`.
14 changes: 12 additions & 2 deletions src/tox/tox_env/api.py
Expand Up @@ -7,6 +7,7 @@
import logging
import os
import re
import string
import sys
from abc import ABC, abstractmethod
from contextlib import contextmanager
Expand All @@ -21,7 +22,7 @@
from tox.execute.request import ExecuteRequest
from tox.journal import EnvJournal
from tox.report import OutErr, ToxHandler
from tox.tox_env.errors import Recreate, Skip
from tox.tox_env.errors import Fail, Recreate, Skip
from tox.tox_env.info import Info
from tox.tox_env.installer import Installer
from tox.util.path import ensure_empty_dir
Expand Down Expand Up @@ -132,7 +133,16 @@ def register_config(self) -> None:

def pass_env_post_process(values: list[str]) -> list[str]:
values.extend(self._default_pass_env())
return sorted({k: None for k in values}.keys())
result = sorted({k: None for k in values}.keys())
invalid_chars = set(string.whitespace)
invalid = [v for v in result if any(c in invalid_chars for c in v)]
if invalid:
invalid_repr = ", ".join(repr(i) for i in invalid)
raise Fail(
f"pass_env values cannot contain whitespace, use comma to have multiple values in a single line, "
f"invalid values found {invalid_repr}",
)
return result

self.conf.add_config(
keys=["pass_env", "passenv"],
Expand Down
20 changes: 20 additions & 0 deletions tests/tox_env/test_tox_env_api.py
Expand Up @@ -80,6 +80,26 @@ def test_tox_env_pass_env_literal_miss() -> None:
assert not env


def test_tox_env_pass_env_fails_on_whitespace(tox_project: ToxProjectCreator) -> None:
first, second = "A B", "C D"
prj = tox_project({"tox.ini": f"[testenv]\npackage=skip\npass_env = {first}\n {second}\n E"})
result = prj.run("c", "-k", "pass_env")
result.assert_success()
msg = (
'[testenv:py]\npass_env = # Exception: Fail("pass_env values cannot contain whitespace, use comma to have '
f'multiple values in a single line, invalid values found {first!r}, {second!r}")\n\n[tox]\n'
)
assert result.out == msg

result = prj.run("r")
result.assert_failed(1)
msg = (
"py: failed with pass_env values cannot contain whitespace, use comma to have multiple values in a single line,"
" invalid values found 'A B', 'C D'"
)
assert msg in result.out


@pytest.mark.parametrize("glob", ["*", "?"])
@pytest.mark.parametrize("char", ["a", "A"])
def test_tox_env_pass_env_match_ignore_case(char: str, glob: str) -> None:
Expand Down

0 comments on commit f4840a0

Please sign in to comment.