Skip to content

Commit

Permalink
pytester: use temporary HOME with spawn
Browse files Browse the repository at this point in the history
Followup to pytest-dev#4956.
  • Loading branch information
blueyed committed May 24, 2019
1 parent b4d75ad commit 5e5e2f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/4956.feature.rst
@@ -0,0 +1 @@
pytester's ``testdir.spawn`` uses ``tmpdir`` as HOME/USERPROFILE directory.
8 changes: 7 additions & 1 deletion src/_pytest/pytester.py
Expand Up @@ -1241,7 +1241,13 @@ def spawn(self, cmd, expect_timeout=10.0):
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
Expand Down
21 changes: 21 additions & 0 deletions testing/test_pytester.py
Expand Up @@ -559,3 +559,24 @@ 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

# Does use HOME only during run.
assert os.environ.get("HOME") != str(testdir.tmpdir)

p1 = testdir.makepyfile(
"""
import os
def test():
assert os.environ["HOME"] == {tmphome!r}
""".format(
tmphome=str(testdir.tmpdir)
)
)
child = testdir.spawn_pytest(str(p1))
out = child.read()
assert child.wait() == 0, out.decode("utf8")

0 comments on commit 5e5e2f3

Please sign in to comment.