Skip to content

Commit 87b4574

Browse files
committed
make ipython_dir, cluster module-scoped
instead of session-scoped less leak across tests, a bit more time setting up / tearing down clusters and catch tempdir cleanup errors we shouldn't really do this, but it's preventing pytest from reporting errors, so warn about them instead of raising
1 parent 6b183b0 commit 87b4574

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

ipyparallel/tests/conftest.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import sys
6+
from contextlib import contextmanager
67
from subprocess import check_call
78
from subprocess import check_output
89
from tempfile import TemporaryDirectory
@@ -21,16 +22,33 @@
2122
from . import teardown
2223

2324

24-
@pytest.fixture(autouse=True, scope="session")
25-
def ipython_dir():
26-
with TemporaryDirectory(suffix="dotipython") as td:
27-
with mock.patch.dict(os.environ, {"IPYTHONDIR": td}):
28-
assert IPython.paths.get_ipython_dir() == td
29-
pd = ProfileDir.create_profile_dir_by_name(td, name="default")
30-
# configure fast heartbeats for quicker tests with small numbers of local engines
31-
with open(os.path.join(pd.location, "ipcontroller_config.py"), "w") as f:
32-
f.write("c.HeartMonitor.period = 200")
25+
@contextmanager
26+
def temporary_ipython_dir():
27+
# FIXME: cleanup has issues on Windows
28+
# this is *probably* a real bug of holding open files,
29+
# but it is preventing feedback about test failures
30+
td_obj = TemporaryDirectory(suffix=".ipython")
31+
td = td_obj.name
32+
33+
with mock.patch.dict(os.environ, {"IPYTHONDIR": td}):
34+
assert IPython.paths.get_ipython_dir() == td
35+
pd = ProfileDir.create_profile_dir_by_name(td, name="default")
36+
# configure fast heartbeats for quicker tests with small numbers of local engines
37+
with open(os.path.join(pd.location, "ipcontroller_config.py"), "w") as f:
38+
f.write("c.HeartMonitor.period = 200")
39+
try:
3340
yield td
41+
finally:
42+
try:
43+
td_obj.cleanup()
44+
except Exception as e:
45+
print(f"Failed to cleanup {td}: {e}", file=sys.stderr)
46+
47+
48+
@pytest.fixture(autouse=True, scope="module")
49+
def ipython_dir(request):
50+
with temporary_ipython_dir() as ipython_dir:
51+
yield ipython_dir
3452

3553

3654
def pytest_collection_modifyitems(items):
@@ -46,7 +64,7 @@ def pytest_collection_modifyitems(items):
4664
assert not inspect.isasyncgenfunction(item.obj)
4765

4866

49-
@pytest.fixture(scope="session")
67+
@pytest.fixture(scope="module")
5068
def cluster(request, ipython_dir):
5169
"""Setup IPython parallel cluster"""
5270
setup()
@@ -56,12 +74,13 @@ def cluster(request, ipython_dir):
5674
teardown()
5775

5876

59-
@pytest.fixture(scope='session')
77+
@pytest.fixture(scope='module')
6078
def ipython(ipython_dir):
6179
config = default_config()
6280
config.TerminalInteractiveShell.simple_prompt = True
6381
shell = TerminalInteractiveShell.instance(config=config)
64-
return shell
82+
yield shell
83+
TerminalInteractiveShell.clear_instance()
6584

6685

6786
@pytest.fixture()

0 commit comments

Comments
 (0)