Skip to content

Commit af8847f

Browse files
committed
passenv respects PYTHONPATH. Fixes tox-dev#457
1 parent 7676d4b commit af8847f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

tests/test_venv.py

+25
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,31 @@ def test_envbinddir_path(self, newmocksession, monkeypatch):
561561
assert 'PIP_REQUIRE_VIRTUALENV' not in os.environ
562562
assert '__PYVENV_LAUNCHER__' not in os.environ
563563

564+
def test_pythonpath_usage(self, newmocksession, monkeypatch):
565+
monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")
566+
mocksession = newmocksession([], """
567+
[testenv:python]
568+
commands=abc
569+
""")
570+
venv = mocksession.getenv("python")
571+
action = mocksession.newaction(venv, "getenv")
572+
venv.run_install_command(['qwe'], action=action)
573+
assert 'PYTHONPATH' not in os.environ
574+
mocksession.report.expect("warning", "*Discarding $PYTHONPATH from environment*")
575+
576+
# passenv = PYTHONPATH allows PYTHONPATH to stay in environment
577+
monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")
578+
mocksession = newmocksession([], """
579+
[testenv:python]
580+
commands=abc
581+
passenv = PYTHONPATH
582+
""")
583+
venv = mocksession.getenv("python")
584+
action = mocksession.newaction(venv, "getenv")
585+
venv.run_install_command(['qwe'], action=action)
586+
assert 'PYTHONPATH' in os.environ
587+
mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
588+
564589

565590
def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch):
566591
pkg = tmpdir.ensure("package.tar.gz")

tox/venv.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,18 @@ def run_install_command(self, packages, action, options=()):
275275
argv[i:i + 1] = list(options)
276276

277277
for x in ('PIP_RESPECT_VIRTUALENV', 'PIP_REQUIRE_VIRTUALENV',
278-
'__PYVENV_LAUNCHER__', 'PYTHONPATH'):
278+
'__PYVENV_LAUNCHER__'):
279279
os.environ.pop(x, None)
280280

281+
if 'PYTHONPATH' not in self.envconfig.passenv:
282+
# If PYTHONPATH not explicitly asked for, remove it.
283+
if 'PYTHONPATH' in os.environ:
284+
self.session.report.warning(
285+
"Discarding $PYTHONPATH from environment, to override "
286+
"specify PYTHONPATH in 'passenv' in your environment."
287+
)
288+
os.environ.pop('PYTHONPATH')
289+
281290
old_stdout = sys.stdout
282291
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
283292
self._pcall(argv, cwd=self.envconfig.config.toxinidir, action=action,

0 commit comments

Comments
 (0)