Skip to content

Commit

Permalink
Merge pull request #6219 from blueyed/testdir-use-monkeypatch
Browse files Browse the repository at this point in the history
pytester: remove special handling of env during inner runs
  • Loading branch information
blueyed authored Nov 22, 2019
2 parents 2fa0518 + b0ebcfb commit a4408eb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 29 deletions.
1 change: 1 addition & 0 deletions changelog/6213.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytester: the ``testdir`` fixture respects environment settings from the ``monkeypatch`` fixture for inner runs.
16 changes: 3 additions & 13 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ def __init__(self, request, tmpdir_factory):

# Environment (updates) for inner runs.
tmphome = str(self.tmpdir)
self._env_run_update = {"HOME": tmphome, "USERPROFILE": tmphome}
mp.setenv("HOME", tmphome)
mp.setenv("USERPROFILE", tmphome)

def __repr__(self):
return "<Testdir {!r}>".format(self.tmpdir)
Expand Down Expand Up @@ -853,12 +854,6 @@ def inline_run(self, *args, plugins=(), no_reraise_ctrlc=False):
plugins = list(plugins)
finalizers = []
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
# inline should be reverted after the test run completes to avoid
# clashing with later inline tests run within the same pytest test,
Expand Down Expand Up @@ -1091,7 +1086,6 @@ def popen(
env["PYTHONPATH"] = os.pathsep.join(
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
)
env.update(self._env_run_update)
kw["env"] = env

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

# Do not load user config.
env = os.environ.copy()
env.update(self._env_run_update)

child = pexpect.spawn(cmd, logfile=logfile, env=env)
child = pexpect.spawn(cmd, logfile=logfile)
self.request.addfinalizer(logfile.close)
child.timeout = expect_timeout
return child
Expand Down
2 changes: 1 addition & 1 deletion testing/test_pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def pdb_env(request):
if "testdir" in request.fixturenames:
# Disable pdb++ with inner tests.
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):
Expand Down
24 changes: 9 additions & 15 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,17 +550,15 @@ def test_no_matching_after_match():
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")

testdir = request.getfixturevalue("testdir")

try:
assert "PYTEST_ADDOPTS" not in os.environ
finally:
testdir.finalize()

assert os.environ["PYTEST_ADDOPTS"] == "--orig-unused"
assert "PYTEST_ADDOPTS" not in os.environ
testdir.finalize()
assert os.environ.get("PYTEST_ADDOPTS") == "--orig-unused"
monkeypatch.undo()
assert os.environ.get("PYTEST_ADDOPTS") == orig


def test_run_stdin(testdir):
Expand Down Expand Up @@ -640,14 +638,10 @@ def test_popen_default_stdin_stderr_and_stdin_None(testdir):


def test_spawn_uses_tmphome(testdir):
import os

tmphome = str(testdir.tmpdir)
assert os.environ.get("HOME") == tmphome

# Does use HOME only during run.
assert os.environ.get("HOME") != tmphome

testdir._env_run_update["CUSTOMENV"] = "42"
testdir.monkeypatch.setenv("CUSTOMENV", "42")

p1 = testdir.makepyfile(
"""
Expand Down

0 comments on commit a4408eb

Please sign in to comment.