Skip to content

Commit 8dda561

Browse files
authored
Merge pull request #4956 from blueyed/home2
pytester: set HOME only with inline_run/popen
2 parents da81c1e + a50b92e commit 8dda561

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

changelog/4941.feature.rst

Lines changed: 0 additions & 3 deletions
This file was deleted.

changelog/4956.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``pytester`` sets ``$HOME`` and ``$USERPROFILE`` to the temporary directory during test runs.
2+
3+
This ensures to not load configuration files from the real user's home directory.

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(

testing/test_junitxml.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,12 @@ def test_invalid_xml_escape():
816816
assert chr(i) == bin_xml_escape(unichr(i)).uniobj
817817

818818

819-
def test_logxml_path_expansion(tmpdir):
819+
def test_logxml_path_expansion(tmpdir, monkeypatch):
820820
home_tilde = py.path.local(os.path.expanduser("~")).join("test.xml")
821821
xml_tilde = LogXML("~%stest.xml" % tmpdir.sep, None)
822822
assert xml_tilde.logfile == home_tilde
823823

824+
monkeypatch.setenv("HOME", str(tmpdir))
824825
home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml"))
825826
xml_var = LogXML("$HOME%stest.xml" % tmpdir.sep, None)
826827
assert xml_var.logfile == home_var

0 commit comments

Comments
 (0)