Skip to content

Commit

Permalink
Pass the copy of existing environments to the subprocesses
Browse files Browse the repository at this point in the history
Unset PYTHONPATH in virtual environments as tests running there
only need to see that env's paths in sys.path.
  • Loading branch information
befeleme committed Feb 24, 2022
1 parent 6193a69 commit 7b56b5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
7 changes: 7 additions & 0 deletions setuptools/tests/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ 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
# If no environment was passed as an argument
# create a copy of the current env to use when spawning a subprocess
# Unset PYTHONPATH - tests in venv need only that venv's paths in sys.path
env = kwargs.get("env") or dict(os.environ)
if "PYTHONPATH" in env:
del env["PYTHONPATH"]
kwargs["env"] = env
return subprocess.check_output(cmd, *args, **kwargs)


Expand Down
5 changes: 4 additions & 1 deletion setuptools/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ def venv(tmp_path, setuptools_wheel):
env = environment.VirtualEnv()
env.root = path.Path(tmp_path / 'venv')
env.req = str(setuptools_wheel)
return env.create()
# Don't pass PYTHONPATH from the current environment to the created venv
# It only needs its own paths in sys.path
with contexts.environment(PYTHONPATH=None):
return env.create()


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions setuptools/tests/test_distutils_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_distutils_stdlib(venv):
"""
Ensure stdlib distutils is used when appropriate.
"""
env = dict(SETUPTOOLS_USE_DISTUTILS='stdlib')
env = {**os.environ, **dict(SETUPTOOLS_USE_DISTUTILS='stdlib')}
assert venv.name not in find_distutils(venv, env=env).split(os.sep)
assert count_meta_path(venv, env=env) == 0

Expand All @@ -62,7 +62,7 @@ def test_distutils_local_with_setuptools(venv):
"""
Ensure local distutils is used when appropriate.
"""
env = dict(SETUPTOOLS_USE_DISTUTILS='local')
env = {**os.environ, **dict(SETUPTOOLS_USE_DISTUTILS='local')}
loc = find_distutils(venv, imports='setuptools, distutils', env=env)
assert venv.name in loc.split(os.sep)
assert count_meta_path(venv, env=env) <= 1
Expand All @@ -74,7 +74,7 @@ def test_distutils_local(venv):
Even without importing, the setuptools-local copy of distutils is
preferred.
"""
env = dict(SETUPTOOLS_USE_DISTUTILS='local')
env = {**os.environ, **dict(SETUPTOOLS_USE_DISTUTILS='local')}
assert venv.name in find_distutils(venv, env=env).split(os.sep)
assert count_meta_path(venv, env=env) <= 1

Expand Down

0 comments on commit 7b56b5a

Please sign in to comment.