Skip to content

Commit 2a3918d

Browse files
committed
_get_env_run_update, update test_testdir_respects_monkeypatch
1 parent 0e4f4c2 commit 2a3918d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/_pytest/pytester.py

Lines changed: 7 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,17 @@ 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+
outer = {item[1] for item in self.monkeypatch._setitem if item[0] is os.environ}
1275+
return {k: v for k, v in self._env_run_update.items() if k not in outer}
1276+
12731277

12741278
def getdecoded(out):
12751279
try:

testing/test_pytester.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,18 @@ def test_pytester_addopts_before_testdir(request, monkeypatch):
551551
assert os.environ.get("PYTEST_ADDOPTS") == orig
552552

553553

554-
def test_testdir_respects_monkeypatch(testdir, monkeypatch):
554+
@pytest.mark.parametrize("method", ("setenv", "delenv"))
555+
def test_testdir_respects_monkeypatch(method, testdir, monkeypatch):
555556
assert monkeypatch is 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)