File: conftest.py

package info (click to toggle)
python-asyncclick 8.3.0.5%2Basync-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,664 kB
  • sloc: python: 14,154; makefile: 12; sh: 10
file content (39 lines) | stat: -rw-r--r-- 788 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from threading import Thread

import anyio
import pytest

from asyncclick.testing import CliRunner


class SyncCliRunner(CliRunner):
    def invoke(self, *a, _sync=False, **k):
        fn = super().invoke
        if _sync:
            return fn(*a, **k)

        # anyio now protects against nested calls, so we use a thread
        result = None

        def f():
            nonlocal result, fn

            async def r():
                return await fn(*a, **k)

            result = anyio.run(r)  ## , backend="trio")

        t = Thread(target=f, name="TEST")
        t.start()
        t.join()
        return result


@pytest.fixture(scope="function")
def runner(request):
    return SyncCliRunner()


@pytest.fixture(scope="function")
def arunner(request):
    return CliRunner()