1
0
mirror of https://github.com/fedora-python/tox-current-env.git synced 2025-01-11 17:06:13 +01:00

Always set passenv/pass_env to *

This commit is contained in:
Miro Hrončok 2022-12-14 13:48:10 +01:00
parent c3497d2b2a
commit 679fbc996e
6 changed files with 57 additions and 13 deletions

View File

@ -208,15 +208,13 @@ forcefully killing it before it finished, uninstalling the plugin,
and running ``tox``), you will get undefined results and running ``tox``), you will get undefined results
(such as installing packages from PyPI into your current environment). (such as installing packages from PyPI into your current environment).
Environment variables are not passed by default Environment variables are passed by default
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Although the plugin name suggests that current environment is used for tests, Since 0.0.9, all Shell environment variables are passed by default when using
it means the Python environment, not Shell. this plugin. The `passenv` tox configuration is set to `*`.
If you want the tests to see environment variables of the calling process, Read `the documentation for more information about passing environment variables to tox
use the ``TOX_TESTENV_PASSENV`` environment variable. <https://tox.wiki/en/latest/config.html#passenv>`_.
Read `the documentation for passing environment variables to tox
<https://tox.readthedocs.io/en/latest/config.html#conf-passenv>`_.
tox provisioning tox provisioning
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

View File

@ -85,6 +85,11 @@ def tox_configure(config):
for testenv in config.envconfigs: for testenv in config.envconfigs:
config.envconfigs[testenv].usedevelop = False config.envconfigs[testenv].usedevelop = False
_allow_all_externals(config.envconfigs[testenv]) _allow_all_externals(config.envconfigs[testenv])
# Because tox 4 no longer reads $TOX_TESTENV_PASSENV,
# this plugin always passes all environment variables by default,
# even on tox 3.
# Unfortunately at this point the set contains actual values, not globs:
config.envconfigs[testenv].passenv |= set(os.environ.keys())
# When printing dependencies/extras we don't run any commands. # When printing dependencies/extras we don't run any commands.
# Unfortunately tox_runtest_pre/tox_runtest_post hooks don't use firstresult=True, # Unfortunately tox_runtest_pre/tox_runtest_post hooks don't use firstresult=True,

View File

@ -87,11 +87,11 @@ def tox_add_core_config(core_conf, state):
def tox_add_env_config(env_conf, state): def tox_add_env_config(env_conf, state):
opt = state.conf.options opt = state.conf.options
# This allows all external commands. # This allows all external commands.
# All of them are extenal for us. # All of them are external for us.
# passenv is here because `TOX_TESTENV_PASSENV` # Because tox 4 no longer reads $TOX_TESTENV_PASSENV,
# no longer works in tox 4. # this plugin always passes all environment variables by default.
if opt.current_env: if opt.current_env:
allow_external_cmds = MemoryLoader(allowlist_externals=["*"], passenv=["*"]) allow_external_cmds = MemoryLoader(allowlist_externals=["*"], pass_env=["*"])
env_conf.loaders.insert(0, allow_external_cmds) env_conf.loaders.insert(0, allow_external_cmds)
# For print-deps-to and print-extras-to, use empty # For print-deps-to and print-extras-to, use empty
# list of commands so the tox does nothing. # list of commands so the tox does nothing.

View File

@ -564,3 +564,17 @@ def test_self_is_installed_with_regular_tox(projdir, usedevelop):
config["testenv"]["usedevelop"] = str(usedevelop) config["testenv"]["usedevelop"] = str(usedevelop)
result = tox("-e", NATIVE_TOXENV, quiet=False) result = tox("-e", NATIVE_TOXENV, quiet=False)
assert "test==0.0.0" in result.stdout or "test @ file://" in result.stdout assert "test==0.0.0" in result.stdout or "test @ file://" in result.stdout
@pytest.mark.parametrize("passenv", [None, "different list", "__var", "*"])
def test_passenv(projdir, passenv):
with modify_config(projdir / "tox.ini") as config:
config["testenv"]["commands"] = """python -c 'import os; print(os.getenv("__var"))'"""
if passenv is not None:
existing = config["testenv"].get("passenv", "") + " "
config["testenv"]["passenv"] = existing + passenv
env = {"__var": "assertme"}
result = tox("-e", NATIVE_TOXENV, "--current-env", env=env, quiet=False)
assert result.returncode == 0
assert "\nassertme\n" in result.stdout
assert "\nNone\n" not in result.stdout

View File

@ -395,3 +395,30 @@ def test_self_is_installed_with_regular_tox(projdir, usedevelop):
assert "test-0.0.0" in result.stdout assert "test-0.0.0" in result.stdout
if usedevelop: if usedevelop:
assert "test-0.0.0-0.editable" in result.stdout assert "test-0.0.0-0.editable" in result.stdout
@pytest.mark.parametrize("passenv", [None, "different list", "__var", "*"])
def test_passenv(projdir, passenv):
with modify_config(projdir / "tox.ini") as config:
config["testenv"]["commands"] = """python -c 'import os; print(os.getenv("__var"))'"""
if passenv is not None:
existing = config["testenv"].get("passenv", "") + " "
config["testenv"]["passenv"] = existing + passenv
env = {"__var": "assertme"}
result = tox("-e", NATIVE_TOXENV, "--current-env", env=env, quiet=False)
assert result.returncode == 0
assert "\nassertme\n" in result.stdout
assert "\nNone\n" not in result.stdout
@pytest.mark.parametrize("pass_env", [None, "different\nlist", "__var", "*"])
def test_pass_env(projdir, pass_env):
with modify_config(projdir / "tox.ini") as config:
config["testenv"]["commands"] = """python -c 'import os; print(os.getenv("__var"))'"""
if pass_env is not None:
config["testenv"]["pass_env"] = pass_env
env = {"__var": "assertme"}
result = tox("-e", NATIVE_TOXENV, "--current-env", env=env, quiet=False)
assert result.returncode == 0
assert "\nassertme\n" in result.stdout
assert "\nNone\n" not in result.stdout

View File

@ -39,7 +39,7 @@ def tox(*args, quiet=True, **kwargs):
q = ("-q",) if quiet else () q = ("-q",) if quiet else ()
env = dict(os.environ) env = dict(os.environ)
env.pop("TOX_WORK_DIR", None) env.pop("TOX_WORK_DIR", None)
kwargs.setdefault("env", env) kwargs["env"] = {**env, **kwargs.get("env", {})}
try: try:
print("current", os.getcwd(), "running in", kwargs["cwd"]) print("current", os.getcwd(), "running in", kwargs["cwd"])
cp = subprocess.run((sys.executable, "-m", "tox") + q + args, **kwargs) cp = subprocess.run((sys.executable, "-m", "tox") + q + args, **kwargs)