Skip to content

Commit e6d54f7

Browse files
committed
pytester: remove special handling of env during inner runs
Closes #6213.
1 parent 64d8910 commit e6d54f7

File tree

4 files changed

+14
-29
lines changed

4 files changed

+14
-29
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: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ def __init__(self, request, tmpdir_factory):
547547

548548
# Environment (updates) for inner runs.
549549
tmphome = str(self.tmpdir)
550-
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome}
550+
mp.setenv("HOME", tmphome)
551+
mp.setenv("USERPROFILE", tmphome)
551552

552553
def __repr__(self):
553554
return "<Testdir {!r}>".format(self.tmpdir)
@@ -853,12 +854,6 @@ def inline_run(self, *args, plugins=(), no_reraise_ctrlc=False):
853854
plugins = list(plugins)
854855
finalizers = []
855856
try:
856-
# Do not load user config (during runs only).
857-
mp_run = MonkeyPatch()
858-
for k, v in self._env_run_update.items():
859-
mp_run.setenv(k, v)
860-
finalizers.append(mp_run.undo)
861-
862857
# Any sys.module or sys.path changes done while running pytest
863858
# inline should be reverted after the test run completes to avoid
864859
# clashing with later inline tests run within the same pytest test,
@@ -1091,7 +1086,6 @@ def popen(
10911086
env["PYTHONPATH"] = os.pathsep.join(
10921087
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
10931088
)
1094-
env.update(self._env_run_update)
10951089
kw["env"] = env
10961090

10971091
if stdin is Testdir.CLOSE_STDIN:
@@ -1261,11 +1255,7 @@ def spawn(self, cmd, expect_timeout=10.0):
12611255
pytest.skip("pexpect.spawn not available")
12621256
logfile = self.tmpdir.join("spawn.out").open("wb")
12631257

1264-
# Do not load user config.
1265-
env = os.environ.copy()
1266-
env.update(self._env_run_update)
1267-
1268-
child = pexpect.spawn(cmd, logfile=logfile, env=env)
1258+
child = pexpect.spawn(cmd, logfile=logfile)
12691259
self.request.addfinalizer(logfile.close)
12701260
child.timeout = expect_timeout
12711261
return child

testing/test_pdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def pdb_env(request):
2222
if "testdir" in request.fixturenames:
2323
# Disable pdb++ with inner tests.
2424
testdir = request.getfixturevalue("testdir")
25-
testdir._env_run_update["PDBPP_HIJACK_PDB"] = "0"
25+
testdir.monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")
2626

2727

2828
def runpdb_and_get_report(testdir, source):

testing/test_pytester.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -542,17 +542,15 @@ 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-
550-
try:
551-
assert "PYTEST_ADDOPTS" not in os.environ
552-
finally:
553-
testdir.finalize()
554-
555-
assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
549+
assert "PYTEST_ADDOPTS" not in os.environ
550+
testdir.finalize()
551+
assert os.environ.get("PYTEST_ADDOPTS") == "--orig-unused"
552+
monkeypatch.undo()
553+
assert os.environ.get("PYTEST_ADDOPTS") == orig
556554

557555

558556
def test_run_stdin(testdir):
@@ -632,14 +630,10 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir):
632630

633631

634632
def test_spawn_uses_tmphome(testdir):
635-
import os
636-
637633
tmphome = str(testdir.tmpdir)
634+
assert os.environ.get("HOME") == tmphome
638635

639-
# Does use HOME only during run.
640-
assert os.environ.get("HOME") != tmphome
641-
642-
testdir._env_run_update["CUSTOMENV"] = "42"
636+
testdir.monkeypatch.setenv("CUSTOMENV", "42")
643637

644638
p1 = testdir.makepyfile(
645639
"""

0 commit comments

Comments
 (0)