Skip to content

Commit cc25a27

Browse files
committed
pytester: factor out testdir._env_run_update
1 parent 58cb207 commit cc25a27

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/_pytest/pytester.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,10 @@ def __init__(self, request, tmpdir_factory):
516516
# Discard outer pytest options.
517517
mp.delenv("PYTEST_ADDOPTS", raising=False)
518518

519+
# Environment (updates) for inner runs.
520+
tmphome = str(self.tmpdir)
521+
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome}
522+
519523
def __repr__(self):
520524
return "<Testdir %r>" % (self.tmpdir,)
521525

@@ -812,8 +816,8 @@ def inline_run(self, *args, **kwargs):
812816
try:
813817
# Do not load user config (during runs only).
814818
mp_run = MonkeyPatch()
815-
mp_run.setenv("HOME", str(self.tmpdir))
816-
mp_run.setenv("USERPROFILE", str(self.tmpdir))
819+
for k, v in self._env_run_update.items():
820+
mp_run.setenv(k, v)
817821
finalizers.append(mp_run.undo)
818822

819823
# When running pytest inline any plugins active in the main test
@@ -1053,9 +1057,7 @@ def popen(
10531057
env["PYTHONPATH"] = os.pathsep.join(
10541058
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
10551059
)
1056-
# Do not load user config.
1057-
env["HOME"] = str(self.tmpdir)
1058-
env["USERPROFILE"] = env["HOME"]
1060+
env.update(self._env_run_update)
10591061
kw["env"] = env
10601062

10611063
if stdin is Testdir.CLOSE_STDIN:
@@ -1244,8 +1246,7 @@ def spawn(self, cmd, expect_timeout=10.0):
12441246

12451247
# Do not load user config.
12461248
env = os.environ.copy()
1247-
env["HOME"] = str(self.tmpdir)
1248-
env["USERPROFILE"] = env["HOME"]
1249+
env.update(self._env_run_update)
12491250

12501251
child = pexpect.spawn(cmd, logfile=logfile, env=env)
12511252
self.request.addfinalizer(logfile.close)

testing/test_pytester.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,12 +569,15 @@ def test_spawn_uses_tmphome(testdir):
569569
# Does use HOME only during run.
570570
assert os.environ.get("HOME") != tmphome
571571

572+
testdir._env_run_update["CUSTOMENV"] = "42"
573+
572574
p1 = testdir.makepyfile(
573575
"""
574576
import os
575577
576578
def test():
577579
assert os.environ["HOME"] == {tmphome!r}
580+
assert os.environ["CUSTOMENV"] == "42"
578581
""".format(
579582
tmphome=tmphome
580583
)

0 commit comments

Comments
 (0)