Skip to content

Commit 52a5b83

Browse files
committed
pytester: set HOME only with inline_run/popen
Ref: #4955
1 parent 77c5191 commit 52a5b83

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/_pytest/pytester.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from _pytest.main import EXIT_INTERRUPTED
3030
from _pytest.main import EXIT_OK
3131
from _pytest.main import Session
32+
from _pytest.monkeypatch import MonkeyPatch
3233
from _pytest.pathlib import Path
3334

3435
IGNORE_PAM = [ # filenames added when obtaining details about the current user
@@ -469,8 +470,6 @@ def __init__(self, request, tmpdir_factory):
469470
os.environ["PYTEST_DEBUG_TEMPROOT"] = str(self.test_tmproot)
470471
os.environ.pop("TOX_ENV_DIR", None) # Ensure that it is not used for caching.
471472
os.environ.pop("PYTEST_ADDOPTS", None) # Do not use outer options.
472-
os.environ["HOME"] = str(self.tmpdir) # Do not load user config.
473-
os.environ["USERPROFILE"] = os.environ["HOME"]
474473
self.plugins = []
475474
self._cwd_snapshot = CwdSnapshot()
476475
self._sys_path_snapshot = SysPathsSnapshot()
@@ -788,6 +787,12 @@ def inline_run(self, *args, **kwargs):
788787
"""
789788
finalizers = []
790789
try:
790+
# Do not load user config.
791+
monkeypatch = MonkeyPatch()
792+
monkeypatch.setenv("HOME", str(self.tmpdir))
793+
monkeypatch.setenv("USERPROFILE", str(self.tmpdir))
794+
finalizers.append(monkeypatch.undo)
795+
791796
# When running pytest inline any plugins active in the main test
792797
# process are already imported. So this disables the warning which
793798
# will trigger to say they can no longer be rewritten, which is
@@ -1018,6 +1023,9 @@ def popen(self, cmdargs, stdout, stderr, **kw):
10181023
env["PYTHONPATH"] = os.pathsep.join(
10191024
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
10201025
)
1026+
# Do not load user config.
1027+
env["HOME"] = str(self.tmpdir)
1028+
env["USERPROFILE"] = env["HOME"]
10211029
kw["env"] = env
10221030

10231031
popen = subprocess.Popen(

0 commit comments

Comments
 (0)