From c75dd1067195753c47e0125ea436b60b1b0d68e7 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Mon, 18 Mar 2019 01:17:32 +0100 Subject: [PATCH] pytester: testdir: set $HOME to tmpdir This avoids loading user configuration, which might interfere with test results, e.g. a `~/.pdbrc.py` with pdb++. Also sets USERPROFILE, which will be required with Python 3.8 [1]. 1: https://bugs.python.org/issue36264 --- changelog/4941.feature.rst | 3 +++ src/_pytest/pytester.py | 2 ++ testing/test_junitxml.py | 6 +----- testing/test_pdb.py | 3 --- 4 files changed, 6 insertions(+), 8 deletions(-) create mode 100644 changelog/4941.feature.rst diff --git a/changelog/4941.feature.rst b/changelog/4941.feature.rst new file mode 100644 index 000000000..846b43f45 --- /dev/null +++ b/changelog/4941.feature.rst @@ -0,0 +1,3 @@ +``pytester``'s ``Testdir`` sets ``$HOME`` and ``$USERPROFILE`` to the temporary directory. + +This ensures to not load configuration files from the real user's home directory. diff --git a/src/_pytest/pytester.py b/src/_pytest/pytester.py index d5faed418..8f1a7d1d5 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -469,6 +469,8 @@ class Testdir(object): os.environ["PYTEST_DEBUG_TEMPROOT"] = str(self.test_tmproot) os.environ.pop("TOX_ENV_DIR", None) # Ensure that it is not used for caching. os.environ.pop("PYTEST_ADDOPTS", None) # Do not use outer options. + os.environ["HOME"] = str(self.tmpdir) # Do not load user config. + os.environ["USERPROFILE"] = os.environ["HOME"] self.plugins = [] self._cwd_snapshot = CwdSnapshot() self._sys_path_snapshot = SysPathsSnapshot() diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 2765dfc60..4748aa581 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -816,16 +816,12 @@ def test_invalid_xml_escape(): assert chr(i) == bin_xml_escape(unichr(i)).uniobj -def test_logxml_path_expansion(tmpdir, monkeypatch): +def test_logxml_path_expansion(tmpdir): home_tilde = py.path.local(os.path.expanduser("~")).join("test.xml") - xml_tilde = LogXML("~%stest.xml" % tmpdir.sep, None) assert xml_tilde.logfile == home_tilde - # this is here for when $HOME is not set correct - monkeypatch.setenv("HOME", str(tmpdir)) home_var = os.path.normpath(os.path.expandvars("$HOME/test.xml")) - xml_var = LogXML("$HOME%stest.xml" % tmpdir.sep, None) assert xml_var.logfile == home_var diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 5b62def2d..05cb9bd77 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -470,9 +470,6 @@ class TestPDB(object): ''' """ ) - # Prevent ~/.pdbrc etc to output anything. - monkeypatch.setenv("HOME", str(testdir)) - child = testdir.spawn_pytest("--doctest-modules --pdb %s" % p1) child.expect("Pdb")