3
3
import logging
4
4
import os
5
5
import sys
6
+ from contextlib import contextmanager
6
7
from subprocess import check_call
7
8
from subprocess import check_output
8
9
from tempfile import TemporaryDirectory
21
22
from . import teardown
22
23
23
24
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 :
33
40
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
34
52
35
53
36
54
def pytest_collection_modifyitems (items ):
@@ -46,7 +64,7 @@ def pytest_collection_modifyitems(items):
46
64
assert not inspect .isasyncgenfunction (item .obj )
47
65
48
66
49
- @pytest .fixture (scope = "session " )
67
+ @pytest .fixture (scope = "module " )
50
68
def cluster (request , ipython_dir ):
51
69
"""Setup IPython parallel cluster"""
52
70
setup ()
@@ -56,12 +74,13 @@ def cluster(request, ipython_dir):
56
74
teardown ()
57
75
58
76
59
- @pytest .fixture (scope = 'session ' )
77
+ @pytest .fixture (scope = 'module ' )
60
78
def ipython (ipython_dir ):
61
79
config = default_config ()
62
80
config .TerminalInteractiveShell .simple_prompt = True
63
81
shell = TerminalInteractiveShell .instance (config = config )
64
- return shell
82
+ yield shell
83
+ TerminalInteractiveShell .clear_instance ()
65
84
66
85
67
86
@pytest .fixture ()
0 commit comments