diff options
author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-02-28 09:53:28 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-28 09:53:28 +0000 |
commit | e30999503add90052a1bbf4526c3025baed947f7 (patch) | |
tree | 531f64a1ccf9ec0cddce606eb285f7b1e9f8f5b6 | |
parent | cb229fa27a86fc48bd40340eacbec60fe5aa609b (diff) | |
parent | 634dd7e1779663d98cc2fa0382656e8f578b669e (diff) | |
download | python-setuptools-git-e30999503add90052a1bbf4526c3025baed947f7.tar.gz |
Merge pull request #3133 from befeleme/fix-tests-for-fedora
Tests using virtual environments: pass the copy of existing env to the subprocesses
-rw-r--r-- | changelog.d/3133.misc.rst | 1 | ||||
-rw-r--r-- | setuptools/tests/environment.py | 13 | ||||
-rw-r--r-- | setuptools/tests/fixtures.py | 13 |
3 files changed, 26 insertions, 1 deletions
diff --git a/changelog.d/3133.misc.rst b/changelog.d/3133.misc.rst new file mode 100644 index 00000000..3377e061 --- /dev/null +++ b/changelog.d/3133.misc.rst @@ -0,0 +1 @@ +Enhanced isolation of tests using virtual environments - PYTHONPATH is not leaking to spawned subprocesses -- by :user:`befeleme` diff --git a/setuptools/tests/environment.py b/setuptools/tests/environment.py index a0c0ec6e..bcf29601 100644 --- a/setuptools/tests/environment.py +++ b/setuptools/tests/environment.py @@ -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) diff --git a/setuptools/tests/fixtures.py b/setuptools/tests/fixtures.py index 7599e655..e912399d 100644 --- a/setuptools/tests/fixtures.py +++ b/setuptools/tests/fixtures.py @@ -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 |