|
1 | 1 | import shutil
|
2 | 2 | import os
|
3 | 3 | import pytest
|
| 4 | +import typing as ty |
| 5 | +from click.testing import CliRunner, Result as CliResult |
4 | 6 |
|
5 | 7 | os.environ["NO_ET"] = "true"
|
6 | 8 |
|
@@ -41,17 +43,39 @@ def pytest_generate_tests(metafunc):
|
41 | 43 | metafunc.parametrize("any_worker", available_workers)
|
42 | 44 |
|
43 | 45 |
|
| 46 | +@pytest.fixture |
| 47 | +def cli_runner(catch_cli_exceptions: bool) -> ty.Callable[..., ty.Any]: |
| 48 | + def invoke( |
| 49 | + *args: ty.Any, catch_exceptions: bool = catch_cli_exceptions, **kwargs: ty.Any |
| 50 | + ) -> CliResult: |
| 51 | + runner = CliRunner() |
| 52 | + result = runner.invoke(*args, catch_exceptions=catch_exceptions, **kwargs) # type: ignore[misc] |
| 53 | + return result |
| 54 | + |
| 55 | + return invoke |
| 56 | + |
| 57 | + |
44 | 58 | # For debugging in IDE's don't catch raised exceptions and let the IDE
|
45 | 59 | # break at it
|
46 |
| -if os.getenv("_PYTEST_RAISE", "0") != "0": # pragma: no cover |
| 60 | +if os.getenv("_PYTEST_RAISE", "0") != "0": |
| 61 | + |
| 62 | + @pytest.hookimpl(tryfirst=True) |
| 63 | + def pytest_exception_interact(call: pytest.CallInfo[ty.Any]) -> None: |
| 64 | + if call.excinfo is not None: |
| 65 | + raise call.excinfo.value |
| 66 | + |
| 67 | + @pytest.hookimpl(tryfirst=True) |
| 68 | + def pytest_internalerror(excinfo: pytest.ExceptionInfo[BaseException]) -> None: |
| 69 | + raise excinfo.value |
| 70 | + |
| 71 | + CATCH_CLI_EXCEPTIONS = False |
| 72 | +else: |
| 73 | + CATCH_CLI_EXCEPTIONS = True |
47 | 74 |
|
48 |
| - @pytest.hookimpl(tryfirst=True) # pragma: no cover |
49 |
| - def pytest_exception_interact(call): # pragma: no cover |
50 |
| - raise call.excinfo.value # pragma: no cover |
51 | 75 |
|
52 |
| - @pytest.hookimpl(tryfirst=True) # pragma: no cover |
53 |
| - def pytest_internalerror(excinfo): # pragma: no cover |
54 |
| - raise excinfo.value # pragma: no cover |
| 76 | +@pytest.fixture |
| 77 | +def catch_cli_exceptions() -> bool: |
| 78 | + return CATCH_CLI_EXCEPTIONS |
55 | 79 |
|
56 | 80 |
|
57 | 81 | # Example VSCode launch configuration for debugging unittests
|
|
0 commit comments