diff options
author | Michael Bourke <michael@iter8ve.com> | 2022-01-06 18:16:20 +1100 |
---|---|---|
committer | Michael Bourke <michael@iter8ve.com> | 2022-01-06 07:19:31 +0000 |
commit | 3c3196754b55b48c044e59a5beacb9a13bc25114 (patch) | |
tree | 1a1e52936d4f3245747cb7cb3d1436e535edfeb1 | |
parent | 291d230d8a9b25218705cbe4e547f379d5b4312d (diff) | |
download | mako-3c3196754b55b48c044e59a5beacb9a13bc25114.tar.gz |
Replace (old) raises test helper with expect_raises
Fixes #350
Change-Id: I154ffeecad925622167593b6a5a45458eaf95641
-rw-r--r-- | test/test_cmd.py | 15 | ||||
-rw-r--r-- | test/util/assertions.py | 32 |
2 files changed, 21 insertions, 26 deletions
diff --git a/test/test_cmd.py b/test/test_cmd.py index 8fc155a..2347452 100644 --- a/test/test_cmd.py +++ b/test/test_cmd.py @@ -4,7 +4,8 @@ from unittest import mock from mako.cmd import cmdline from .util.assertions import eq_ -from .util.assertions import raises +from .util.assertions import expect_raises +from .util.assertions import expect_raises_message from .util.fixtures import template_base from .util.fixtures import TemplateTest @@ -30,7 +31,7 @@ class CmdTest(TemplateTest): "sys.stdin", mock.Mock(read=mock.Mock(return_value="${x")) ): with self._capture_output_fixture("stderr") as stderr: - with raises(SystemExit): + with expect_raises(SystemExit): cmdline(["--var", "x=5", "-"]) assert ( @@ -43,7 +44,7 @@ class CmdTest(TemplateTest): "sys.stdin", mock.Mock(read=mock.Mock(return_value="${q}")) ): with self._capture_output_fixture("stderr") as stderr: - with raises(SystemExit): + with expect_raises(SystemExit): cmdline(["--var", "x=5", "-"]) assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0] @@ -59,7 +60,7 @@ class CmdTest(TemplateTest): def test_file_syntax_err(self): with self._capture_output_fixture("stderr") as stderr: - with raises(SystemExit): + with expect_raises(SystemExit): cmdline( [ "--var", @@ -73,7 +74,7 @@ class CmdTest(TemplateTest): def test_file_rt_err(self): with self._capture_output_fixture("stderr") as stderr: - with raises(SystemExit): + with expect_raises(SystemExit): cmdline( [ "--var", @@ -86,5 +87,7 @@ class CmdTest(TemplateTest): assert "Traceback" in stderr.write.mock_calls[0][1][0] def test_file_notfound(self): - with raises(SystemExit, "error: can't find fake.lalala"): + with expect_raises_message( + SystemExit, "error: can't find fake.lalala" + ): cmdline(["--var", "x=5", "fake.lalala"]) diff --git a/test/util/assertions.py b/test/util/assertions.py index a6c5d4e..8be154c 100644 --- a/test/util/assertions.py +++ b/test/util/assertions.py @@ -8,24 +8,6 @@ def eq_(a, b, msg=None): assert a == b, msg or "%r != %r" % (a, b) -@contextlib.contextmanager -def raises(except_cls, message=None): - try: - yield - success = False - except except_cls as e: - if message: - assert re.search(message, str(e), re.UNICODE), "%r !~ %s" % ( - message, - e, - ) - print(str(e).encode("utf-8")) - success = True - - # assert outside the block so it works for AssertionError too ! - assert success, "Callable did not raise an exception" - - def _assert_proper_exception_context(exception): """assert that any exception we're catching does not have a __context__ without a __cause__, and that __suppress_context__ is never set. @@ -152,9 +134,19 @@ def _expect_raises(except_cls, msg=None, check_context=False, cause_cls=None): assert success, "Callable did not raise an exception" -def expect_raises(except_cls, check_context=True): +def expect_raises(except_cls, check_context=False): + return _expect_raises(except_cls, check_context=check_context) + + +def expect_raises_message(except_cls, msg, check_context=False): + return _expect_raises(except_cls, msg=msg, check_context=check_context) + + +def expect_raises_with_proper_context(except_cls, check_context=True): return _expect_raises(except_cls, check_context=check_context) -def expect_raises_message(except_cls, msg, check_context=True): +def expect_raises_message_with_proper_context( + except_cls, msg, check_context=True +): return _expect_raises(except_cls, msg=msg, check_context=check_context) |