1
- import sys
2
- import pytest
3
- import re
4
-
5
-
6
- @pytest .fixture (params = ["Python>=36" , "async_generator" ])
7
- def async_yield_implementation (request ):
8
- if request .param == "Python>=36" :
9
-
10
- def patch_code (code ):
11
- # Convert code to use Python>=3.6 builtin async generator
12
- code = re .sub (r"(?m)^\s*@async_generator\n" , r"" , code )
13
- code = re .sub (r"await yield_" , r"yield" , code )
14
- return code
15
-
16
- return patch_code
17
- else :
18
- return lambda x : x
19
-
20
-
21
- def test_single_async_yield_fixture (testdir , async_yield_implementation ):
1
+ def test_single_async_yield_fixture (testdir ):
22
2
testdir .makepyfile (
23
- async_yield_implementation (
24
- """
3
+ """
25
4
import pytest
26
5
import trio
27
- from async_generator import async_generator, yield_
28
6
29
7
events = []
30
8
31
9
@pytest.fixture
32
- @async_generator
33
10
async def fix1():
34
11
events.append('fix1 setup')
35
12
await trio.sleep(0)
36
13
37
- await yield_( 'fix1')
14
+ yield 'fix1'
38
15
39
16
await trio.sleep(0)
40
17
events.append('fix1 teardown')
@@ -53,43 +30,38 @@ def test_after():
53
30
'fix1 teardown',
54
31
]
55
32
"""
56
- )
57
33
)
58
34
59
35
result = testdir .runpytest ()
60
36
61
37
result .assert_outcomes (passed = 3 )
62
38
63
39
64
- def test_nested_async_yield_fixture (testdir , async_yield_implementation ):
40
+ def test_nested_async_yield_fixture (testdir ):
65
41
66
42
testdir .makepyfile (
67
- async_yield_implementation (
68
- """
43
+ """
69
44
import pytest
70
45
import trio
71
- from async_generator import async_generator, yield_
72
46
73
47
events = []
74
48
75
49
@pytest.fixture
76
- @async_generator
77
50
async def fix2():
78
51
events.append('fix2 setup')
79
52
await trio.sleep(0)
80
53
81
- await yield_( 'fix2')
54
+ yield 'fix2'
82
55
83
56
await trio.sleep(0)
84
57
events.append('fix2 teardown')
85
58
86
59
@pytest.fixture
87
- @async_generator
88
60
async def fix1(fix2):
89
61
events.append('fix1 setup')
90
62
await trio.sleep(0)
91
63
92
- await yield_( 'fix1')
64
+ yield 'fix1'
93
65
94
66
await trio.sleep(0)
95
67
events.append('fix1 teardown')
@@ -113,32 +85,28 @@ def test_after():
113
85
'fix2 teardown',
114
86
]
115
87
"""
116
- )
117
88
)
118
89
119
90
result = testdir .runpytest ()
120
91
121
92
result .assert_outcomes (passed = 3 )
122
93
123
94
124
- def test_async_yield_fixture_within_sync_fixture (testdir , async_yield_implementation ):
95
+ def test_async_yield_fixture_within_sync_fixture (testdir ):
125
96
126
97
testdir .makepyfile (
127
- async_yield_implementation (
128
- """
98
+ """
129
99
import pytest
130
100
import trio
131
- from async_generator import async_generator, yield_
132
101
133
102
events = []
134
103
135
104
@pytest.fixture
136
- @async_generator
137
105
async def fix2():
138
106
events.append('fix2 setup')
139
107
await trio.sleep(0)
140
108
141
- await yield_( 'fix2')
109
+ yield 'fix2'
142
110
143
111
await trio.sleep(0)
144
112
events.append('fix2 teardown')
@@ -163,34 +131,28 @@ def test_after():
163
131
'fix2 teardown',
164
132
]
165
133
"""
166
- )
167
134
)
168
135
169
136
result = testdir .runpytest ()
170
137
171
138
result .assert_outcomes (passed = 3 )
172
139
173
140
174
- def test_async_yield_fixture_within_sync_yield_fixture (
175
- testdir , async_yield_implementation
176
- ):
141
+ def test_async_yield_fixture_within_sync_yield_fixture (testdir ):
177
142
178
143
testdir .makepyfile (
179
- async_yield_implementation (
180
- """
144
+ """
181
145
import pytest
182
146
import trio
183
- from async_generator import async_generator, yield_
184
147
185
148
events = []
186
149
187
150
@pytest.fixture
188
- @async_generator
189
151
async def fix2():
190
152
events.append('fix2 setup')
191
153
await trio.sleep(0)
192
154
193
- await yield_( 'fix2')
155
+ yield 'fix2'
194
156
195
157
await trio.sleep(0)
196
158
events.append('fix2 teardown')
@@ -220,36 +182,31 @@ def test_after():
220
182
'fix2 teardown',
221
183
]
222
184
"""
223
- )
224
185
)
225
186
226
187
result = testdir .runpytest ()
227
188
228
189
result .assert_outcomes (passed = 3 )
229
190
230
191
231
- def test_async_yield_fixture_with_multiple_yields (testdir , async_yield_implementation ):
192
+ def test_async_yield_fixture_with_multiple_yields (testdir ):
232
193
233
194
testdir .makepyfile (
234
- async_yield_implementation (
235
- """
195
+ """
236
196
import pytest
237
197
import trio
238
- from async_generator import async_generator, yield_
239
198
240
199
@pytest.fixture
241
- @async_generator
242
200
async def fix1():
243
201
await trio.sleep(0)
244
- await yield_( 'good')
202
+ yield 'good'
245
203
await trio.sleep(0)
246
- await yield_( 'bad')
204
+ yield 'bad'
247
205
248
206
@pytest.mark.trio
249
207
async def test_actual_test(fix1):
250
208
pass
251
209
"""
252
- )
253
210
)
254
211
255
212
result = testdir .runpytest ()
@@ -259,14 +216,12 @@ async def test_actual_test(fix1):
259
216
result .assert_outcomes (failed = 1 )
260
217
261
218
262
- def test_async_yield_fixture_with_nursery (testdir , async_yield_implementation ):
219
+ def test_async_yield_fixture_with_nursery (testdir ):
263
220
264
221
testdir .makepyfile (
265
- async_yield_implementation (
266
- """
222
+ """
267
223
import pytest
268
224
import trio
269
- from async_generator import async_generator, yield_
270
225
271
226
272
227
async def handle_client(stream):
@@ -276,11 +231,10 @@ async def handle_client(stream):
276
231
277
232
278
233
@pytest.fixture
279
- @async_generator
280
234
async def server():
281
235
async with trio.open_nursery() as nursery:
282
236
listeners = await nursery.start(trio.serve_tcp, handle_client, 0)
283
- await yield_( listeners[0])
237
+ yield listeners[0]
284
238
nursery.cancel_scope.cancel()
285
239
286
240
@@ -291,42 +245,35 @@ async def test_actual_test(server):
291
245
rep = await stream.receive_some(4)
292
246
assert rep == b'ping'
293
247
"""
294
- )
295
248
)
296
249
297
250
result = testdir .runpytest ()
298
251
299
252
result .assert_outcomes (passed = 1 )
300
253
301
254
302
- def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (
303
- testdir , async_yield_implementation
304
- ):
255
+ def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (testdir ):
305
256
306
257
testdir .makepyfile (
307
- async_yield_implementation (
308
- """
258
+ """
309
259
import pytest
310
260
import trio
311
- from async_generator import async_generator, yield_
312
261
313
262
setup_events = set()
314
263
teardown_events = set()
315
264
316
265
@pytest.fixture
317
- @async_generator
318
266
async def good_fixture():
319
267
async with trio.open_nursery() as nursery:
320
268
setup_events.add('good_fixture setup')
321
- await yield_( None)
269
+ yield None
322
270
teardown_events.add('good_fixture teardown')
323
271
324
272
@pytest.fixture
325
- @async_generator
326
273
async def bad_fixture():
327
274
async with trio.open_nursery() as nursery:
328
275
setup_events.add('bad_fixture setup')
329
- await yield_( None)
276
+ yield None
330
277
teardown_events.add('bad_fixture teardown')
331
278
raise RuntimeError('Crash during fixture teardown')
332
279
@@ -348,7 +295,6 @@ def test_after():
348
295
'good_fixture teardown',
349
296
}
350
297
"""
351
- )
352
298
)
353
299
354
300
result = testdir .runpytest ()
0 commit comments