11
11
pytest .skip ("Terminal API tests time out on Windows." , allow_module_level = True )
12
12
13
13
14
+ # Kill all running terminals after each test to avoid cross-test issues
15
+ # with still running terminals.
16
+ @pytest .fixture
17
+ def kill_all (serverapp ):
18
+ async def _ ():
19
+ await serverapp .web_app .settings ["terminal_manager" ].kill_all ()
20
+ return _
21
+
22
+
14
23
@pytest .fixture
15
24
def terminal_path (tmp_path ):
16
25
subdir = tmp_path .joinpath ('terminal_path' )
@@ -21,7 +30,7 @@ def terminal_path(tmp_path):
21
30
shutil .rmtree (str (subdir ), ignore_errors = True )
22
31
23
32
24
- async def test_terminal_create (fetch ):
33
+ async def test_terminal_create (fetch , kill_all ):
25
34
await fetch (
26
35
'api' , 'terminals' ,
27
36
method = 'POST' ,
@@ -37,9 +46,10 @@ async def test_terminal_create(fetch):
37
46
data = json .loads (resp_list .body .decode ())
38
47
39
48
assert len (data ) == 1
49
+ await kill_all ()
40
50
41
51
42
- async def test_terminal_create_with_kwargs (fetch , ws_fetch , terminal_path ):
52
+ async def test_terminal_create_with_kwargs (fetch , ws_fetch , terminal_path , kill_all ):
43
53
resp_create = await fetch (
44
54
'api' , 'terminals' ,
45
55
method = 'POST' ,
@@ -59,9 +69,15 @@ async def test_terminal_create_with_kwargs(fetch, ws_fetch, terminal_path):
59
69
data = json .loads (resp_get .body .decode ())
60
70
61
71
assert data ['name' ] == term_name
72
+ await kill_all ()
62
73
63
74
64
- async def test_terminal_create_with_cwd (fetch , ws_fetch , terminal_path ):
75
+ async def test_terminal_create_with_cwd (
76
+ fetch ,
77
+ ws_fetch ,
78
+ terminal_path ,
79
+ kill_all
80
+ ):
65
81
resp = await fetch (
66
82
'api' , 'terminals' ,
67
83
method = 'POST' ,
@@ -75,21 +91,19 @@ async def test_terminal_create_with_cwd(fetch, ws_fetch, terminal_path):
75
91
ws = await ws_fetch (
76
92
'terminals' , 'websocket' , term_name
77
93
)
94
+ await ws .write_message (json .dumps (['stdin' , 'pwd\r ' ]))
78
95
79
- ws .write_message (json .dumps (['stdin' , 'pwd\r \n ' ]))
80
-
81
- message_stdout = ''
96
+ messages = ""
82
97
while True :
83
98
try :
84
- message = await asyncio .wait_for (ws .read_message (), timeout = 1.0 )
99
+ response = await asyncio .wait_for (ws .read_message (), timeout = 1.0 )
85
100
except asyncio .TimeoutError :
86
- break
87
-
88
- message = json .loads (message )
101
+ return messages
89
102
90
- if message [0 ] == 'stdout' :
91
- message_stdout += message [1 ]
103
+ response = json .loads (response )
104
+ if response [0 ] == "stdout" :
105
+ messages += response [1 ]
92
106
93
107
ws .close ()
94
-
95
- assert str ( terminal_path ) in message_stdout
108
+ assert str ( terminal_path ) in messages
109
+ await kill_all ()
0 commit comments