Skip to content

Commit 1c008e4

Browse files
committed
pytester: respect monkeypatch fixture for env updates
Fixes #6213.
1 parent 64d8910 commit 1c008e4

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

changelog/6213.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester: the ``testdir`` fixture respects environment settings from the ``monkeypatch`` fixture for inner runs.

src/_pytest/pytester.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ def inline_run(self, *args, plugins=(), no_reraise_ctrlc=False):
855855
try:
856856
# Do not load user config (during runs only).
857857
mp_run = MonkeyPatch()
858-
for k, v in self._env_run_update.items():
858+
for k, v in self._get_env_run_update().items():
859859
mp_run.setenv(k, v)
860860
finalizers.append(mp_run.undo)
861861

@@ -1091,7 +1091,7 @@ def popen(
10911091
env["PYTHONPATH"] = os.pathsep.join(
10921092
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
10931093
)
1094-
env.update(self._env_run_update)
1094+
env.update(self._get_env_run_update())
10951095
kw["env"] = env
10961096

10971097
if stdin is Testdir.CLOSE_STDIN:
@@ -1263,13 +1263,19 @@ def spawn(self, cmd, expect_timeout=10.0):
12631263

12641264
# Do not load user config.
12651265
env = os.environ.copy()
1266-
env.update(self._env_run_update)
1266+
env.update(self._get_env_run_update())
12671267

12681268
child = pexpect.spawn(cmd, logfile=logfile, env=env)
12691269
self.request.addfinalizer(logfile.close)
12701270
child.timeout = expect_timeout
12711271
return child
12721272

1273+
def _get_env_run_update(self):
1274+
"""Get updates for environment not overridden via monkeypatch fixture."""
1275+
outer_mp = self.request.getfixturevalue("monkeypatch")
1276+
outer = {item[1] for item in outer_mp._setitem if item[0] is os.environ}
1277+
return {k: v for k, v in self._env_run_update.items() if k not in outer}
1278+
12731279

12741280
def getdecoded(out):
12751281
try:

testing/test_pytester.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -542,17 +542,27 @@ def test_no_matching(function):
542542
func(bad_pattern) # bad pattern does not match any line: passes
543543

544544

545-
def test_pytester_addopts(request, monkeypatch):
545+
def test_pytester_addopts_before_testdir(request, monkeypatch):
546+
orig = os.environ.get("PYTEST_ADDOPTS", None)
546547
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
547-
548548
testdir = request.getfixturevalue("testdir")
549+
assert "PYTEST_ADDOPTS" not in os.environ
550+
testdir.finalize()
551+
assert os.environ.get("PYTEST_ADDOPTS") == orig
549552

550-
try:
551-
assert "PYTEST_ADDOPTS" not in os.environ
552-
finally:
553-
testdir.finalize()
554553

555-
assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
554+
@pytest.mark.parametrize("method", ("setenv", "delenv"))
555+
def test_testdir_respects_monkeypatch(method, testdir, monkeypatch):
556+
assert monkeypatch is not testdir.monkeypatch
557+
tmphome = str(testdir.tmpdir)
558+
assert testdir._env_run_update["HOME"] == tmphome
559+
assert testdir._get_env_run_update()["HOME"] == tmphome
560+
if method == "setenv":
561+
monkeypatch.setenv("HOME", "anotherhome")
562+
else:
563+
assert method == "delenv"
564+
monkeypatch.delenv("HOME", raising=False)
565+
assert "HOME" not in testdir._get_env_run_update()
556566

557567

558568
def test_run_stdin(testdir):

0 commit comments

Comments
 (0)