|
9 | 9 | redirect_stderr, redirect_stdout) |
10 | 10 | from typing import Any, Callable, Iterable, Iterator, Optional, Tuple, Type |
11 | 11 |
|
12 | | -import pytest |
13 | | - |
14 | 12 | from git_machete import cli, utils |
15 | 13 | from git_machete.utils import MacheteException, PopenResult |
16 | 14 |
|
@@ -61,27 +59,41 @@ def launch_command(*cmd_and_args: str) -> str: |
61 | 59 | return output |
62 | 60 |
|
63 | 61 |
|
| 62 | +def strip_trailing_spaces(text: str) -> str: |
| 63 | + return re.sub(" +$", "", text, flags=re.MULTILINE) |
| 64 | + |
| 65 | + |
64 | 66 | def assert_success(cmd_and_args: Iterable[str], expected_result: str) -> None: |
65 | 67 | if expected_result.startswith("\n"): |
66 | 68 | # removeprefix is only available since Python 3.9 |
67 | 69 | expected_result = expected_result[1:] |
68 | 70 | expected_result = textwrap.dedent(expected_result) |
69 | | - actual_result = re.sub(" +$", "", textwrap.dedent(launch_command(*cmd_and_args)), flags=re.MULTILINE) |
| 71 | + actual_result = strip_trailing_spaces(textwrap.dedent(launch_command(*cmd_and_args))) |
70 | 72 | assert actual_result == expected_result |
71 | 73 |
|
72 | 74 |
|
73 | | -def assert_failure(cmd_and_args: Iterable[str], expected_message: str, expected_type: Type[BaseException] = MacheteException) -> None: |
| 75 | +def assert_failure(cmd_and_args: Iterable[str], expected_message: str, expected_type: Type[BaseException] = MacheteException, |
| 76 | + expected_output: Optional[str] = None) -> None: |
74 | 77 | if expected_message.startswith("\n"): |
75 | 78 | # removeprefix is only available since Python 3.9 |
76 | 79 | expected_message = expected_message[1:] |
77 | 80 | expected_message = textwrap.dedent(expected_message) |
78 | 81 |
|
79 | | - with pytest.raises(expected_type) as ei: |
80 | | - launch_command(*cmd_and_args) |
81 | | - error_message = ei.value.msg # type: ignore[attr-defined] |
82 | | - error_message = re.sub(" +$", "", error_message, flags=re.MULTILINE) |
| 82 | + if expected_output is not None: |
| 83 | + if expected_output.startswith("\n"): |
| 84 | + expected_output = expected_output[1:] |
| 85 | + expected_output = textwrap.dedent(expected_output) |
| 86 | + |
| 87 | + output, e = launch_command_capturing_output_and_exception(*cmd_and_args) |
| 88 | + assert e is not None, f"Expected {expected_type.__name__} but no exception was raised" |
| 89 | + assert isinstance(e, expected_type), f"Expected {expected_type.__name__} but got {type(e).__name__}: {e}" |
| 90 | + error_message = strip_trailing_spaces(e.msg) # type: ignore[attr-defined] |
83 | 91 | assert error_message == expected_message |
84 | 92 |
|
| 93 | + if expected_output is not None: |
| 94 | + actual_output = strip_trailing_spaces(textwrap.dedent(output or "")) |
| 95 | + assert actual_output == expected_output |
| 96 | + |
85 | 97 |
|
86 | 98 | def read_branch_layout_file() -> str: |
87 | 99 | with open(".git/machete") as def_file: |
|
0 commit comments