Skip to content

Commit a624b84

Browse files
authored
Merge pull request #4941 from blueyed/testdir-home
pytester: testdir: set $HOME to tmpdir
2 parents 5d14362 + c75dd10 commit a624b84

File tree

4 files changed

+6
-8
lines changed

4 files changed

+6
-8
lines changed

changelog/4941.feature.rst

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

src/_pytest/pytester.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ def __init__(self, request, tmpdir_factory):
469469
os.environ["PYTEST_DEBUG_TEMPROOT"] = str(self.test_tmproot)
470470
os.environ.pop("TOX_ENV_DIR", None) # Ensure that it is not used for caching.
471471
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"]
472474
self.plugins = []
473475
self._cwd_snapshot = CwdSnapshot()
474476
self._sys_path_snapshot = SysPathsSnapshot()

testing/test_junitxml.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -816,16 +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, monkeypatch):
819+
def test_logxml_path_expansion(tmpdir):
820820
home_tilde = py.path.local(os.path.expanduser("~")).join("test.xml")
821-
822821
xml_tilde = LogXML("~%stest.xml" % tmpdir.sep, None)
823822
assert xml_tilde.logfile == home_tilde
824823

825-
# this is here for when $HOME is not set correct
826-
monkeypatch.setenv("HOME", str(tmpdir))
827824
home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml"))
828-
829825
xml_var = LogXML("$HOME%stest.xml" % tmpdir.sep, None)
830826
assert xml_var.logfile == home_var
831827

testing/test_pdb.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,6 @@ def function_1():
470470
'''
471471
"""
472472
)
473-
# Prevent ~/.pdbrc etc to output anything.
474-
monkeypatch.setenv("HOME", str(testdir))
475-
476473
child = testdir.spawn_pytest("--doctest-modules --pdb %s" % p1)
477474
child.expect("Pdb")
478475

0 commit comments

Comments
 (0)