Merge pull request #6219 from blueyed/testdir-use-monkeypatch

pytester: remove special handling of env during inner runs
This commit is contained in:
Daniel Hahler 2019-11-22 22:41:48 +01:00 committed by GitHub
commit a4408eb9c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 29 deletions

View File

@ -0,0 +1 @@
pytester: the ``testdir`` fixture respects environment settings from the ``monkeypatch`` fixture for inner runs.

View File

@ -547,7 +547,8 @@ class Testdir:
# Environment (updates) for inner runs. # Environment (updates) for inner runs.
tmphome = str(self.tmpdir) tmphome = str(self.tmpdir)
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome} mp.setenv("HOME", tmphome)
mp.setenv("USERPROFILE", tmphome)
def __repr__(self): def __repr__(self):
return "<Testdir {!r}>".format(self.tmpdir) return "<Testdir {!r}>".format(self.tmpdir)
@ -853,12 +854,6 @@ class Testdir:
plugins = list(plugins) plugins = list(plugins)
finalizers = [] finalizers = []
try: try:
# Do not load user config (during runs only).
mp_run = MonkeyPatch()
for k, v in self._env_run_update.items():
mp_run.setenv(k, v)
finalizers.append(mp_run.undo)
# Any sys.module or sys.path changes done while running pytest # Any sys.module or sys.path changes done while running pytest
# inline should be reverted after the test run completes to avoid # inline should be reverted after the test run completes to avoid
# clashing with later inline tests run within the same pytest test, # clashing with later inline tests run within the same pytest test,
@ -1091,7 +1086,6 @@ class Testdir:
env["PYTHONPATH"] = os.pathsep.join( env["PYTHONPATH"] = os.pathsep.join(
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
) )
env.update(self._env_run_update)
kw["env"] = env kw["env"] = env
if stdin is Testdir.CLOSE_STDIN: if stdin is Testdir.CLOSE_STDIN:
@ -1261,11 +1255,7 @@ class Testdir:
pytest.skip("pexpect.spawn not available") pytest.skip("pexpect.spawn not available")
logfile = self.tmpdir.join("spawn.out").open("wb") logfile = self.tmpdir.join("spawn.out").open("wb")
# Do not load user config. child = pexpect.spawn(cmd, logfile=logfile)
env = os.environ.copy()
env.update(self._env_run_update)
child = pexpect.spawn(cmd, logfile=logfile, env=env)
self.request.addfinalizer(logfile.close) self.request.addfinalizer(logfile.close)
child.timeout = expect_timeout child.timeout = expect_timeout
return child return child

View File

@ -22,7 +22,7 @@ def pdb_env(request):
if "testdir" in request.fixturenames: if "testdir" in request.fixturenames:
# Disable pdb++ with inner tests. # Disable pdb++ with inner tests.
testdir = request.getfixturevalue("testdir") testdir = request.getfixturevalue("testdir")
testdir._env_run_update["PDBPP_HIJACK_PDB"] = "0" testdir.monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")
def runpdb_and_get_report(testdir, source): def runpdb_and_get_report(testdir, source):

View File

@ -550,17 +550,15 @@ def test_no_matching_after_match():
assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"] assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"]
def test_pytester_addopts(request, monkeypatch): def test_pytester_addopts_before_testdir(request, monkeypatch):
orig = os.environ.get("PYTEST_ADDOPTS", None)
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused") monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
testdir = request.getfixturevalue("testdir") testdir = request.getfixturevalue("testdir")
assert "PYTEST_ADDOPTS" not in os.environ
try: testdir.finalize()
assert "PYTEST_ADDOPTS" not in os.environ assert os.environ.get("PYTEST_ADDOPTS") == "--orig-unused"
finally: monkeypatch.undo()
testdir.finalize() assert os.environ.get("PYTEST_ADDOPTS") == orig
assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
def test_run_stdin(testdir): def test_run_stdin(testdir):
@ -640,14 +638,10 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir):
def test_spawn_uses_tmphome(testdir): def test_spawn_uses_tmphome(testdir):
import os
tmphome = str(testdir.tmpdir) tmphome = str(testdir.tmpdir)
assert os.environ.get("HOME") == tmphome
# Does use HOME only during run. testdir.monkeypatch.setenv("CUSTOMENV", "42")
assert os.environ.get("HOME") != tmphome
testdir._env_run_update["CUSTOMENV"] = "42"
p1 = testdir.makepyfile( p1 = testdir.makepyfile(
""" """