Skip to content

Commit 2d5b8c5

Browse files
committed
Make the Result available on an error
1 parent 901e6bd commit 2d5b8c5

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

manageprojects/test_utils/click_cli_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class ForceRichTerminalWidth(MassContextManager):
3030
)
3131

3232

33+
class ClickInvokeCliException(Exception):
34+
def __init__(self, result: Result):
35+
self.result = result
36+
37+
3338
def invoke_click(cli, *args, expected_stderr='', expected_exit_code=0, strip=True, **kwargs):
3439
assert isinstance(cli, click.Command)
3540

@@ -40,7 +45,7 @@ def invoke_click(cli, *args, expected_stderr='', expected_exit_code=0, strip=Tru
4045
result: Result = runner.invoke(cli=cli, args=args, **kwargs, color=False)
4146

4247
if result.exception:
43-
raise result.exception
48+
raise ClickInvokeCliException(result) from result.exception
4449

4550
stdout = result.stdout
4651
stderr = result.stderr
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from unittest.mock import patch
2+
3+
from click.testing import Result
4+
5+
from manageprojects.cli import cli_app
6+
from manageprojects.cli.cli_app import cli
7+
from manageprojects.test_utils.click_cli_utils import ClickInvokeCliException, invoke_click
8+
from manageprojects.tests.base import BaseTestCase
9+
10+
11+
class CliTestCase(BaseTestCase):
12+
def test_invoke_click(self):
13+
with patch.object(cli_app, 'verbose_check_call', side_effect=RuntimeError('Bam!')):
14+
with self.assertRaises(ClickInvokeCliException) as cm:
15+
invoke_click(cli, 'install')
16+
17+
self.assertIsInstance(cm.exception, ClickInvokeCliException)
18+
self.assertIsInstance(cm.exception.result, Result)
19+
self.assertIsInstance(cm.exception.result.exception, RuntimeError)
20+
self.assertEqual(cm.exception.result.exception.args, ('Bam!',))

0 commit comments

Comments
 (0)