From 64732211da05f98df940032ab4651c4bbf88aab3 Mon Sep 17 00:00:00 2001
From: Dmitrii Sutiagin <f3flight@gmail.com>
Date: Sun, 13 Nov 2022 18:20:57 -0800
Subject: [PATCH] Fix PYTHONPATH passed to envreport / "pip freeze" (#2529)

* Fix PYTHONPATH passed to envreport / "pip freeze"

* Add unit test for envreport PYTHONPATH handling

* Fix failing envreport test on Python 3.7 and below

Co-authored-by: Dmitrii Sutiagin <dsutiagi@linkedin.com>
---
 CONTRIBUTORS                   |  1 +
 docs/changelog/2528.bugfix.rst |  1 +
 src/tox/venv.py                |  6 +++++-
 tests/unit/test_venv.py        | 15 +++++++++++++++
 4 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 docs/changelog/2528.bugfix.rst

diff --git a/CONTRIBUTORS b/CONTRIBUTORS
index e0259323b..09ae13446 100644
--- a/CONTRIBUTORS
+++ b/CONTRIBUTORS
@@ -33,6 +33,7 @@ Cyril Roelandt
 Dane Hillard
 David Staheli
 David Diaz
+Dmitrii Sutiagin a.k.a. f3flight
 Ederag
 Eli Collins
 Eugene Yunak
diff --git a/docs/changelog/2528.bugfix.rst b/docs/changelog/2528.bugfix.rst
new file mode 100644
index 000000000..c5cecd418
--- /dev/null
+++ b/docs/changelog/2528.bugfix.rst
@@ -0,0 +1 @@
+Add env cleanup to envreport - fix PYTHONPATH leak into "envreport" -- by :user:`f3flight`.
diff --git a/src/tox/venv.py b/src/tox/venv.py
index 8acb0c775..47f281f68 100644
--- a/src/tox/venv.py
+++ b/src/tox/venv.py
@@ -840,7 +840,11 @@ def tox_runtest_post(venv):
 def tox_runenvreport(venv, action):
     # write out version dependency information
     args = venv.envconfig.list_dependencies_command
-    output = venv._pcall(args, cwd=venv.envconfig.config.toxinidir, action=action, returnout=True)
+    env = venv._get_os_environ()
+    venv.ensure_pip_os_environ_ok(env)
+    output = venv._pcall(
+        args, cwd=venv.envconfig.config.toxinidir, action=action, returnout=True, env=env
+    )
     # the output contains a mime-header, skip it
     output = output.split("\n\n")[-1]
     packages = output.strip().split("\n")
diff --git a/tests/unit/test_venv.py b/tests/unit/test_venv.py
index aa78a48b5..3da4e22e1 100644
--- a/tests/unit/test_venv.py
+++ b/tests/unit/test_venv.py
@@ -14,6 +14,7 @@
     VirtualEnv,
     getdigest,
     prepend_shebang_interpreter,
+    tox_runenvreport,
     tox_testenv_create,
     tox_testenv_install_deps,
 )
@@ -1233,3 +1234,17 @@ def test_path_change(tmpdir, mocksession, newconfig, monkeypatch):
         path = x.env["PATH"]
         assert os.environ["PATH"] in path
         assert path.endswith(str(venv.envconfig.config.toxinidir) + "/bin")
+
+
+def test_runenvreport_pythonpath_discarded(newmocksession, mocker):
+    mock_os_environ = mocker.patch("tox.venv.VirtualEnv._get_os_environ")
+    mocksession = newmocksession([], "")
+    venv = mocksession.getvenv("python")
+    mock_os_environ.return_value = dict(PYTHONPATH="/some/path/")
+    mock_pcall = mocker.patch.object(venv, "_pcall")
+    tox_runenvreport(venv, None)
+    try:
+        env = mock_pcall.mock_calls[0].kwargs["env"]
+    except TypeError:  # older pytest (python 3.7 and below)
+        env = mock_pcall.mock_calls[0][2]["env"]
+    assert "PYTHONPATH" not in env