Skip to content

Commit 5e5e2f3

Browse files
committed
pytester: use temporary HOME with spawn
Followup to #4956.
1 parent b4d75ad commit 5e5e2f3

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

changelog/4956.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester's ``testdir.spawn`` uses ``tmpdir`` as HOME/USERPROFILE directory.

src/_pytest/pytester.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,13 @@ def spawn(self, cmd, expect_timeout=10.0):
12411241
if sys.platform.startswith("freebsd"):
12421242
pytest.xfail("pexpect does not work reliably on freebsd")
12431243
logfile = self.tmpdir.join("spawn.out").open("wb")
1244-
child = pexpect.spawn(cmd, logfile=logfile)
1244+
1245+
# Do not load user config.
1246+
env = os.environ.copy()
1247+
env["HOME"] = str(self.tmpdir)
1248+
env["USERPROFILE"] = env["HOME"]
1249+
1250+
child = pexpect.spawn(cmd, logfile=logfile, env=env)
12451251
self.request.addfinalizer(logfile.close)
12461252
child.timeout = expect_timeout
12471253
return child

testing/test_pytester.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,3 +559,24 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir):
559559
assert stdout.splitlines() == [b"", b"stdout"]
560560
assert stderr.splitlines() == [b"stderr"]
561561
assert proc.returncode == 0
562+
563+
564+
def test_spawn_uses_tmphome(testdir):
565+
import os
566+
567+
# Does use HOME only during run.
568+
assert os.environ.get("HOME") != str(testdir.tmpdir)
569+
570+
p1 = testdir.makepyfile(
571+
"""
572+
import os
573+
574+
def test():
575+
assert os.environ["HOME"] == {tmphome!r}
576+
""".format(
577+
tmphome=str(testdir.tmpdir)
578+
)
579+
)
580+
child = testdir.spawn_pytest(str(p1))
581+
out = child.read()
582+
assert child.wait() == 0, out.decode("utf8")

0 commit comments

Comments
 (0)