pytester: use temporary HOME with spawn

Followup to https://github.com/pytest-dev/pytest/issues/4956.
This commit is contained in:
Daniel Hahler 2019-05-24 16:46:50 +02:00
parent fbd8ff9502
commit f013a5e8c1
3 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1 @@
pytester's ``testdir.spawn`` uses ``tmpdir`` as HOME/USERPROFILE directory.

View File

@ -1233,7 +1233,13 @@ class Testdir(object):
if sys.platform.startswith("freebsd"):
pytest.xfail("pexpect does not work reliably on freebsd")
logfile = self.tmpdir.join("spawn.out").open("wb")
child = pexpect.spawn(cmd, logfile=logfile)
# Do not load user config.
env = os.environ.copy()
env["HOME"] = str(self.tmpdir)
env["USERPROFILE"] = env["HOME"]
child = pexpect.spawn(cmd, logfile=logfile, env=env)
self.request.addfinalizer(logfile.close)
child.timeout = expect_timeout
return child

View File

@ -559,3 +559,26 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir):
assert stdout.splitlines() == [b"", b"stdout"]
assert stderr.splitlines() == [b"stderr"]
assert proc.returncode == 0
def test_spawn_uses_tmphome(testdir):
import os
tmphome = str(testdir.tmpdir)
# Does use HOME only during run.
assert os.environ.get("HOME") != tmphome
p1 = testdir.makepyfile(
"""
import os
def test():
assert os.environ["HOME"] == {tmphome!r}
""".format(
tmphome=tmphome
)
)
child = testdir.spawn_pytest(str(p1))
out = child.read()
assert child.wait() == 0, out.decode("utf8")