Skip to content

Commit d0d60e8

Browse files
authored
Merge pull request #459 from jarshwah/master
passenv respects PYTHONPATH. Fixes #457 and #458
2 parents de0903d + c0ebe47 commit d0d60e8

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

CONTRIBUTORS

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Barry Warsaw
99
Chris Rose
1010
Jannis Leidel
1111
Ronny Pfannschmidt
12-
Lukasz Balcerzak
12+
Lukasz Balcerzak
1313
Philip Thiem
1414
Monty Taylor
1515
Bruno Oliveira
@@ -39,3 +39,4 @@ Eli Collins
3939
Andrii Soldatenko
4040
Igor Duarte Cardoso
4141
Allan Feldman
42+
Josh Smeaton

doc/config.txt

+6
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ Complete list of settings that you can put into ``testenv*`` sections:
222222
invocation environment) can define additional space-separated variable
223223
names that are to be passed down to the test command environment.
224224

225+
.. versionchanged:: 2.7
226+
227+
``PYTHONPATH`` will be passed down if explicitly defined. If ``PYTHONPATH``
228+
exists in the host environment but is **not** declared in ``passenv`` a
229+
warning will be emitted.
230+
225231
.. confval:: recreate=True|False(default)
226232

227233
Always recreate virtual environment if this option is True.

tests/test_venv.py

+39-1
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,39 @@ 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+
l = mocksession._pcalls
577+
assert len(l) == 1
578+
assert 'PYTHONPATH' not in l[0].env
579+
580+
# passenv = PYTHONPATH allows PYTHONPATH to stay in environment
581+
monkeypatch.setenv("PYTHONPATH", "/my/awesome/library")
582+
mocksession = newmocksession([], """
583+
[testenv:python]
584+
commands=abc
585+
passenv = PYTHONPATH
586+
""")
587+
venv = mocksession.getenv("python")
588+
action = mocksession.newaction(venv, "getenv")
589+
venv.run_install_command(['qwe'], action=action)
590+
assert 'PYTHONPATH' in os.environ
591+
mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
592+
593+
l = mocksession._pcalls
594+
assert len(l) == 2
595+
assert l[1].env['PYTHONPATH'] == '/my/awesome/library'
596+
564597

565598
def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatch):
566599
pkg = tmpdir.ensure("package.tar.gz")
@@ -572,11 +605,11 @@ def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatc
572605
passenv = x123
573606
setenv =
574607
ENV_VAR = value
608+
PYTHONPATH = value
575609
""")
576610
mocksession._clearmocks()
577611

578612
venv = VirtualEnv(config.envconfigs['python'], session=mocksession)
579-
# import pdb; pdb.set_trace()
580613
mocksession.installpkg(venv, pkg)
581614
venv.test()
582615

@@ -589,13 +622,18 @@ def test_env_variables_added_to_pcall(tmpdir, mocksession, newconfig, monkeypatc
589622
assert env['ENV_VAR'] == 'value'
590623
assert env['VIRTUAL_ENV'] == str(venv.path)
591624
assert env['X123'] == "123"
625+
assert 'PYTHONPATH' in env
626+
assert env['PYTHONPATH'] == 'value'
592627
# all env variables are passed for installation
593628
assert l[0].env["YY"] == "456"
594629
assert "YY" not in l[1].env
595630

596631
assert set(["ENV_VAR", "VIRTUAL_ENV", "PYTHONHASHSEED", "X123", "PATH"])\
597632
.issubset(l[1].env)
598633

634+
# setenv does not trigger PYTHONPATH warnings
635+
mocksession.report.not_expect("warning", "*Discarding $PYTHONPATH from environment*")
636+
599637
# for e in os.environ:
600638
# assert e in env
601639

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 configuration."
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)