Skip to content

Commit 6473221

Browse files
f3flightDmitrii Sutiagin
and
Dmitrii Sutiagin
authored
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 <[email protected]>
1 parent 0f0c505 commit 6473221

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CONTRIBUTORS

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Cyril Roelandt
3333
Dane Hillard
3434
David Staheli
3535
David Diaz
36+
Dmitrii Sutiagin a.k.a. f3flight
3637
Ederag
3738
Eli Collins
3839
Eugene Yunak

docs/changelog/2528.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add env cleanup to envreport - fix PYTHONPATH leak into "envreport" -- by :user:`f3flight`.

src/tox/venv.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,11 @@ def tox_runtest_post(venv):
840840
def tox_runenvreport(venv, action):
841841
# write out version dependency information
842842
args = venv.envconfig.list_dependencies_command
843-
output = venv._pcall(args, cwd=venv.envconfig.config.toxinidir, action=action, returnout=True)
843+
env = venv._get_os_environ()
844+
venv.ensure_pip_os_environ_ok(env)
845+
output = venv._pcall(
846+
args, cwd=venv.envconfig.config.toxinidir, action=action, returnout=True, env=env
847+
)
844848
# the output contains a mime-header, skip it
845849
output = output.split("\n\n")[-1]
846850
packages = output.strip().split("\n")

tests/unit/test_venv.py

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
VirtualEnv,
1515
getdigest,
1616
prepend_shebang_interpreter,
17+
tox_runenvreport,
1718
tox_testenv_create,
1819
tox_testenv_install_deps,
1920
)
@@ -1233,3 +1234,17 @@ def test_path_change(tmpdir, mocksession, newconfig, monkeypatch):
12331234
path = x.env["PATH"]
12341235
assert os.environ["PATH"] in path
12351236
assert path.endswith(str(venv.envconfig.config.toxinidir) + "/bin")
1237+
1238+
1239+
def test_runenvreport_pythonpath_discarded(newmocksession, mocker):
1240+
mock_os_environ = mocker.patch("tox.venv.VirtualEnv._get_os_environ")
1241+
mocksession = newmocksession([], "")
1242+
venv = mocksession.getvenv("python")
1243+
mock_os_environ.return_value = dict(PYTHONPATH="/some/path/")
1244+
mock_pcall = mocker.patch.object(venv, "_pcall")
1245+
tox_runenvreport(venv, None)
1246+
try:
1247+
env = mock_pcall.mock_calls[0].kwargs["env"]
1248+
except TypeError: # older pytest (python 3.7 and below)
1249+
env = mock_pcall.mock_calls[0][2]["env"]
1250+
assert "PYTHONPATH" not in env

0 commit comments

Comments
 (0)