Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/4956.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytester's ``testdir.spawn`` uses ``tmpdir`` as HOME/USERPROFILE directory.
19 changes: 13 additions & 6 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ def __init__(self, request, tmpdir_factory):
# Discard outer pytest options.
mp.delenv("PYTEST_ADDOPTS", raising=False)

# Environment (updates) for inner runs.
tmphome = str(self.tmpdir)
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome}

def __repr__(self):
return "<Testdir %r>" % (self.tmpdir,)

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

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

if stdin is Testdir.CLOSE_STDIN:
Expand Down Expand Up @@ -1233,7 +1235,12 @@ 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.update(self._env_run_update)

child = pexpect.spawn(cmd, logfile=logfile, env=env)
self.request.addfinalizer(logfile.close)
child.timeout = expect_timeout
return child
Expand Down
26 changes: 26 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,29 @@ 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

testdir._env_run_update["CUSTOMENV"] = "42"

p1 = testdir.makepyfile(
"""
import os

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