Skip to content

Commit

Permalink
Merge pull request #3133 from befeleme/fix-tests-for-fedora
Browse files Browse the repository at this point in the history
Tests using virtual environments: pass the copy of existing env to the subprocesses
  • Loading branch information
abravalheri authored Feb 28, 2022
2 parents cb229fa + 634dd7e commit e309995
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/3133.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enhanced isolation of tests using virtual environments - PYTHONPATH is not leaking to spawned subprocesses -- by :user:`befeleme`
13 changes: 13 additions & 0 deletions setuptools/tests/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ class VirtualEnv(jaraco.envs.VirtualEnv):
def run(self, cmd, *args, **kwargs):
cmd = [self.exe(cmd[0])] + cmd[1:]
kwargs = {"cwd": self.root, **kwargs} # Allow overriding
# In some environments (eg. downstream distro packaging), where:
# - tox isn't used to run tests and
# - PYTHONPATH is set to point to a specific setuptools codebase and
# - no custom env is explicitly set by a test
# PYTHONPATH will leak into the spawned processes.
# In that case tests look for module in the wrong place (on PYTHONPATH).
# Unless the test sets its own special env, pass a copy of the existing
# environment with removed PYTHONPATH to the subprocesses.
if "env" not in kwargs:
env = dict(os.environ)
if "PYTHONPATH" in env:
del env["PYTHONPATH"]
kwargs["env"] = env
return subprocess.check_output(cmd, *args, **kwargs)


Expand Down
13 changes: 12 additions & 1 deletion setuptools/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,18 @@ def venv(tmp_path, setuptools_wheel):
env = environment.VirtualEnv()
env.root = path.Path(tmp_path / 'venv')
env.req = str(setuptools_wheel)
return env.create()
# In some environments (eg. downstream distro packaging),
# where tox isn't used to run tests and PYTHONPATH is set to point to
# a specific setuptools codebase, PYTHONPATH will leak into the spawned
# processes.
# env.create() should install the just created setuptools
# wheel, but it doesn't if it finds another existing matching setuptools
# installation present on PYTHONPATH:
# `setuptools is already installed with the same version as the provided
# wheel. Use --force-reinstall to force an installation of the wheel.`
# This prevents leaking PYTHONPATH to the created environment.
with contexts.environment(PYTHONPATH=None):
return env.create()


@pytest.fixture
Expand Down

0 comments on commit e309995

Please sign in to comment.