From 429ca5aa1979ffe4bf05a3ba8d5ca8f8c351d061 Mon Sep 17 00:00:00 2001 From: Dmitrii Sutiagin Date: Thu, 3 Nov 2022 14:25:02 -0700 Subject: [PATCH 1/3] Fix PYTHONPATH passed to envreport / "pip freeze" --- CONTRIBUTORS | 1 + docs/changelog/2528.bugfix.rst | 1 + src/tox/venv.py | 6 +++++- 3 files changed, 7 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") From 239f2d8eaafaeac29d35bd16b24768253ff0bf44 Mon Sep 17 00:00:00 2001 From: Dmitrii Sutiagin Date: Fri, 4 Nov 2022 12:47:39 -0700 Subject: [PATCH 2/3] Add unit test for envreport PYTHONPATH handling --- tests/unit/test_venv.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/unit/test_venv.py b/tests/unit/test_venv.py index aa78a48b5..cd0d8902f 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,13 @@ 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) + assert "PYTHONPATH" not in mock_pcall.mock_calls[0].kwargs["env"] From d35aa4cde350724f52208c927183abaf8e0b31ca Mon Sep 17 00:00:00 2001 From: Dmitrii Sutiagin Date: Fri, 4 Nov 2022 13:45:54 -0700 Subject: [PATCH 3/3] Fix failing envreport test on Python 3.7 and below --- tests/unit/test_venv.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_venv.py b/tests/unit/test_venv.py index cd0d8902f..3da4e22e1 100644 --- a/tests/unit/test_venv.py +++ b/tests/unit/test_venv.py @@ -1243,4 +1243,8 @@ def test_runenvreport_pythonpath_discarded(newmocksession, mocker): mock_os_environ.return_value = dict(PYTHONPATH="/some/path/") mock_pcall = mocker.patch.object(venv, "_pcall") tox_runenvreport(venv, None) - assert "PYTHONPATH" not in mock_pcall.mock_calls[0].kwargs["env"] + 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