Skip to content

pytester: set HOME only with inline_run/popen #4956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions changelog/4941.feature.rst

This file was deleted.

3 changes: 3 additions & 0 deletions changelog/4956.feature.rst
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 10 additions & 2 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
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
Expand Down Expand Up @@ -469,8 +470,6 @@ def __init__(self, request, tmpdir_factory):
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()
Expand Down Expand Up @@ -788,6 +787,12 @@ def inline_run(self, *args, **kwargs):
"""
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
Expand Down Expand Up @@ -1018,6 +1023,9 @@ def popen(self, cmdargs, stdout, stderr, **kw):
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(
Expand Down
3 changes: 2 additions & 1 deletion testing/test_junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down