Skip to content

Commit 9421783

Browse files
committed
pytester: factor out testdir._env_run_update
1 parent 8ea3194 commit 9421783

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
@@ -517,6 +517,10 @@ def __init__(self, request, tmpdir_factory):
517517
# Discard outer pytest options.
518518
mp.delenv("PYTEST_ADDOPTS", raising=False)
519519

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

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

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

10621064
if stdin is Testdir.CLOSE_STDIN:
@@ -1245,8 +1247,7 @@ def spawn(self, cmd, expect_timeout=10.0):
12451247

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

12511252
child = pexpect.spawn(cmd, logfile=logfile, env=env)
12521253
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)