Skip to content

Commit bf8c790

Browse files
committed
Generators or async generators post shotdown items are not allowed
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 16f453c commit bf8c790

File tree

1 file changed

+44
-34
lines changed
  • launch_testing/launch_testing/pytest

1 file changed

+44
-34
lines changed

launch_testing/launch_testing/pytest/plugin.py

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -326,40 +326,50 @@ def enumerate_reversed(sequence):
326326
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
327327
def pytest_pyfunc_call(pyfuncitem):
328328
"""Run launch_testing test coroutines and functions in an event loop."""
329-
if is_launch_test(pyfuncitem):
330-
func = pyfuncitem.obj
331-
shutdown_test = is_shutdown_test(pyfuncitem)
332-
fixture = get_launch_test_fixture(pyfuncitem)
333-
scope = fixture._pytestfixturefunction.scope
334-
event_loop = pyfuncitem.funcargs['event_loop']
335-
ls = pyfuncitem.funcargs['launch_service']
336-
on_shutdown = functools.partial(
337-
finalize_launch_service, ls, eprefix=f'When running test {func.__name__}')
338-
before_test = on_shutdown if shutdown_test else None
339-
if inspect.iscoroutinefunction(func):
340-
pyfuncitem.obj = wrap_coroutine(func, event_loop, before_test)
341-
elif inspect.isgeneratorfunction(func):
342-
if scope != 'function':
343-
shutdown_item = pyfuncitem._launch_testing_shutdown_item
344-
pyfuncitem.obj, shutdown_item.obj = (
345-
wrap_generator(func, event_loop, on_shutdown)
346-
)
347-
shutdown_item._fixtureinfo = shutdown_item.session._fixturemanager.getfixtureinfo(
348-
shutdown_item, shutdown_item.obj, shutdown_item.cls, funcargs=True)
349-
else:
350-
pyfuncitem.obj = wrap_generator_fscope(func, event_loop, on_shutdown)
351-
elif inspect.isasyncgenfunction(func):
352-
if scope != 'function':
353-
shutdown_item = pyfuncitem._launch_testing_shutdown_item
354-
pyfuncitem.obj, shutdown_item.obj = (
355-
wrap_asyncgen(func, event_loop, on_shutdown)
356-
)
357-
shutdown_item._fixtureinfo = shutdown_item.session._fixturemanager.getfixtureinfo(
358-
shutdown_item, shutdown_item.obj, shutdown_item.cls, funcargs=True)
359-
else:
360-
pyfuncitem.obj = wrap_asyncgen_fscope(func, event_loop, on_shutdown)
361-
elif not getattr(pyfuncitem.obj, '_launch_testing_wrapped', False):
362-
pyfuncitem.obj = wrap_func(func, event_loop, before_test)
329+
if not is_launch_test(pyfuncitem):
330+
yield
331+
return
332+
333+
func = pyfuncitem.obj
334+
if has_shutdown_kwarg(pyfuncitem) and need_shutdown_test_item(func):
335+
skip(
336+
'generator or asyncgenerator based launch test items cannot be marked with'
337+
' shutdown=True'
338+
)
339+
yield
340+
return
341+
shutdown_test = is_shutdown_test(pyfuncitem)
342+
fixture = get_launch_test_fixture(pyfuncitem)
343+
scope = fixture._pytestfixturefunction.scope
344+
event_loop = pyfuncitem.funcargs['event_loop']
345+
ls = pyfuncitem.funcargs['launch_service']
346+
on_shutdown = functools.partial(
347+
finalize_launch_service, ls, eprefix=f'When running test {func.__name__}')
348+
before_test = on_shutdown if shutdown_test else None
349+
if inspect.iscoroutinefunction(func):
350+
pyfuncitem.obj = wrap_coroutine(func, event_loop, before_test)
351+
elif inspect.isgeneratorfunction(func):
352+
if scope != 'function':
353+
shutdown_item = pyfuncitem._launch_testing_shutdown_item
354+
pyfuncitem.obj, shutdown_item.obj = (
355+
wrap_generator(func, event_loop, on_shutdown)
356+
)
357+
shutdown_item._fixtureinfo = shutdown_item.session._fixturemanager.getfixtureinfo(
358+
shutdown_item, shutdown_item.obj, shutdown_item.cls, funcargs=True)
359+
else:
360+
pyfuncitem.obj = wrap_generator_fscope(func, event_loop, on_shutdown)
361+
elif inspect.isasyncgenfunction(func):
362+
if scope != 'function':
363+
shutdown_item = pyfuncitem._launch_testing_shutdown_item
364+
pyfuncitem.obj, shutdown_item.obj = (
365+
wrap_asyncgen(func, event_loop, on_shutdown)
366+
)
367+
shutdown_item._fixtureinfo = shutdown_item.session._fixturemanager.getfixtureinfo(
368+
shutdown_item, shutdown_item.obj, shutdown_item.cls, funcargs=True)
369+
else:
370+
pyfuncitem.obj = wrap_asyncgen_fscope(func, event_loop, on_shutdown)
371+
elif not getattr(pyfuncitem.obj, '_launch_testing_wrapped', False):
372+
pyfuncitem.obj = wrap_func(func, event_loop, before_test)
363373
yield
364374

365375

0 commit comments

Comments
 (0)