Skip to content

Commit c3b7832

Browse files
author
Wu Jianxiao
committed
Add test for pydracli crash
1 parent 75b0e33 commit c3b7832

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

pydra/conftest.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import shutil
22
import os
33
import pytest
4+
import typing as ty
5+
from click.testing import CliRunner, Result as CliResult
46

57
os.environ["NO_ET"] = "true"
68

@@ -41,17 +43,39 @@ def pytest_generate_tests(metafunc):
4143
metafunc.parametrize("any_worker", available_workers)
4244

4345

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+
4458
# For debugging in IDE's don't catch raised exceptions and let the IDE
4559
# 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
4774

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
5175

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
5579

5680

5781
# Example VSCode launch configuration for debugging unittests

pydra/scripts/tests/test_crash.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pydra.scripts.cli import crash
2+
from pydra.utils.general import default_run_cache_root
3+
from pydra.tasks.testing import Divide
4+
from traceback import format_exception
5+
import typing as ty
6+
7+
8+
# @pytest.mark.xfail(reason="Need to fix a couple of things after syntax changes")
9+
def test_crash_cli(cli_runner):
10+
result = cli_runner(
11+
crash,
12+
[
13+
f"{default_run_cache_root}/{Divide(x=15, y=0)._checksum}/_error.pklz",
14+
"--rerun",
15+
"--debugger",
16+
"pdb",
17+
],
18+
)
19+
assert result.exit_code == 0, show_cli_trace(result)
20+
21+
22+
def show_cli_trace(result: ty.Any) -> str:
23+
"Used in testing to show traceback of CLI output"
24+
return "".join(format_exception(*result.exc_info))

0 commit comments

Comments
 (0)