From a50b92ea67ed46374c63486ce554245be9ca71e8 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Tue, 19 Mar 2019 22:30:50 +0100 Subject: [PATCH] pytester: set HOME only with inline_run/popen Ref: https://github.com/pytest-dev/pytest/issues/4955 --- changelog/4941.feature.rst | 3 --- changelog/4956.feature.rst | 3 +++ src/_pytest/pytester.py | 12 ++++++++++-- testing/test_junitxml.py | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) delete mode 100644 changelog/4941.feature.rst create mode 100644 changelog/4956.feature.rst diff --git a/changelog/4941.feature.rst b/changelog/4941.feature.rst deleted file mode 100644 index 846b43f45..000000000 --- a/changelog/4941.feature.rst +++ /dev/null @@ -1,3 +0,0 @@ -``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/changelog/4956.feature.rst b/changelog/4956.feature.rst new file mode 100644 index 000000000..1dfbd7e97 --- /dev/null +++ b/changelog/4956.feature.rst @@ -0,0 +1,3 @@ +``pytester`` sets ``$HOME`` and ``$USERPROFILE`` to the temporary directory during test runs. + +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 8f1a7d1d5..ffcd2982a 100644 --- a/src/_pytest/pytester.py +++ b/src/_pytest/pytester.py @@ -29,6 +29,7 @@ from _pytest.compat import Sequence from _pytest.main import EXIT_INTERRUPTED from _pytest.main import EXIT_OK from _pytest.main import Session +from _pytest.monkeypatch import MonkeyPatch from _pytest.pathlib import Path IGNORE_PAM = [ # filenames added when obtaining details about the current user @@ -469,8 +470,6 @@ 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() @@ -788,6 +787,12 @@ class Testdir(object): """ finalizers = [] try: + # Do not load user config. + monkeypatch = MonkeyPatch() + monkeypatch.setenv("HOME", str(self.tmpdir)) + monkeypatch.setenv("USERPROFILE", str(self.tmpdir)) + finalizers.append(monkeypatch.undo) + # When running pytest inline any plugins active in the main test # process are already imported. So this disables the warning which # will trigger to say they can no longer be rewritten, which is @@ -1018,6 +1023,9 @@ class Testdir(object): 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"] kw["env"] = env popen = subprocess.Popen( diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index 4748aa581..769e8e8a7 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -816,11 +816,12 @@ def test_invalid_xml_escape(): assert chr(i) == bin_xml_escape(unichr(i)).uniobj -def test_logxml_path_expansion(tmpdir): +def test_logxml_path_expansion(tmpdir, monkeypatch): 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 + 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