Skip to content

Commit

Permalink
Fix PYTHONPATH passed to envreport / "pip freeze" (#2529)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
f3flight and Dmitrii Sutiagin committed Nov 14, 2022
1 parent 0f0c505 commit 6473221
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -33,6 +33,7 @@ Cyril Roelandt
Dane Hillard
David Staheli
David Diaz
Dmitrii Sutiagin a.k.a. f3flight
Ederag
Eli Collins
Eugene Yunak
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/2528.bugfix.rst
@@ -0,0 +1 @@
Add env cleanup to envreport - fix PYTHONPATH leak into "envreport" -- by :user:`f3flight`.
6 changes: 5 additions & 1 deletion src/tox/venv.py
Expand Up @@ -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")
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_venv.py
Expand Up @@ -14,6 +14,7 @@
VirtualEnv,
getdigest,
prepend_shebang_interpreter,
tox_runenvreport,
tox_testenv_create,
tox_testenv_install_deps,
)
Expand Down Expand Up @@ -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

0 comments on commit 6473221

Please sign in to comment.