Skip to content

Commit ace3a02

Browse files
committed
pytester: factor out testdir._env_run_update
1 parent f013a5e commit ace3a02

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

511+
# Environment (updates) for inner runs.
512+
tmphome = str(self.tmpdir)
513+
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome}
514+
511515
def __repr__(self):
512516
return "<Testdir %r>" % (self.tmpdir,)
513517

@@ -804,8 +808,8 @@ def inline_run(self, *args, **kwargs):
804808
try:
805809
# Do not load user config (during runs only).
806810
mp_run = MonkeyPatch()
807-
mp_run.setenv("HOME", str(self.tmpdir))
808-
mp_run.setenv("USERPROFILE", str(self.tmpdir))
811+
for k, v in self._env_run_update.items():
812+
mp_run.setenv(k, v)
809813
finalizers.append(mp_run.undo)
810814

811815
# When running pytest inline any plugins active in the main test
@@ -1045,9 +1049,7 @@ def popen(
10451049
env["PYTHONPATH"] = os.pathsep.join(
10461050
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
10471051
)
1048-
# Do not load user config.
1049-
env["HOME"] = str(self.tmpdir)
1050-
env["USERPROFILE"] = env["HOME"]
1052+
env.update(self._env_run_update)
10511053
kw["env"] = env
10521054

10531055
if stdin is Testdir.CLOSE_STDIN:
@@ -1236,8 +1238,7 @@ def spawn(self, cmd, expect_timeout=10.0):
12361238

12371239
# Do not load user config.
12381240
env = os.environ.copy()
1239-
env["HOME"] = str(self.tmpdir)
1240-
env["USERPROFILE"] = env["HOME"]
1241+
env.update(self._env_run_update)
12411242

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