Skip to content

Commit 3c31967

Browse files
committed
Replace (old) raises test helper with expect_raises
Fixes #350 Change-Id: I154ffeecad925622167593b6a5a45458eaf95641
1 parent 291d230 commit 3c31967

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

test/test_cmd.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from mako.cmd import cmdline
66
from .util.assertions import eq_
7-
from .util.assertions import raises
7+
from .util.assertions import expect_raises
8+
from .util.assertions import expect_raises_message
89
from .util.fixtures import template_base
910
from .util.fixtures import TemplateTest
1011

@@ -30,7 +31,7 @@ def test_stdin_syntax_err(self):
3031
"sys.stdin", mock.Mock(read=mock.Mock(return_value="${x"))
3132
):
3233
with self._capture_output_fixture("stderr") as stderr:
33-
with raises(SystemExit):
34+
with expect_raises(SystemExit):
3435
cmdline(["--var", "x=5", "-"])
3536

3637
assert (
@@ -43,7 +44,7 @@ def test_stdin_rt_err(self):
4344
"sys.stdin", mock.Mock(read=mock.Mock(return_value="${q}"))
4445
):
4546
with self._capture_output_fixture("stderr") as stderr:
46-
with raises(SystemExit):
47+
with expect_raises(SystemExit):
4748
cmdline(["--var", "x=5", "-"])
4849

4950
assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0]
@@ -59,7 +60,7 @@ def test_file_success(self):
5960

6061
def test_file_syntax_err(self):
6162
with self._capture_output_fixture("stderr") as stderr:
62-
with raises(SystemExit):
63+
with expect_raises(SystemExit):
6364
cmdline(
6465
[
6566
"--var",
@@ -73,7 +74,7 @@ def test_file_syntax_err(self):
7374

7475
def test_file_rt_err(self):
7576
with self._capture_output_fixture("stderr") as stderr:
76-
with raises(SystemExit):
77+
with expect_raises(SystemExit):
7778
cmdline(
7879
[
7980
"--var",
@@ -86,5 +87,7 @@ def test_file_rt_err(self):
8687
assert "Traceback" in stderr.write.mock_calls[0][1][0]
8788

8889
def test_file_notfound(self):
89-
with raises(SystemExit, "error: can't find fake.lalala"):
90+
with expect_raises_message(
91+
SystemExit, "error: can't find fake.lalala"
92+
):
9093
cmdline(["--var", "x=5", "fake.lalala"])

test/util/assertions.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@ def eq_(a, b, msg=None):
88
assert a == b, msg or "%r != %r" % (a, b)
99

1010

11-
@contextlib.contextmanager
12-
def raises(except_cls, message=None):
13-
try:
14-
yield
15-
success = False
16-
except except_cls as e:
17-
if message:
18-
assert re.search(message, str(e), re.UNICODE), "%r !~ %s" % (
19-
message,
20-
e,
21-
)
22-
print(str(e).encode("utf-8"))
23-
success = True
24-
25-
# assert outside the block so it works for AssertionError too !
26-
assert success, "Callable did not raise an exception"
27-
28-
2911
def _assert_proper_exception_context(exception):
3012
"""assert that any exception we're catching does not have a __context__
3113
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):
152134
assert success, "Callable did not raise an exception"
153135

154136

155-
def expect_raises(except_cls, check_context=True):
137+
def expect_raises(except_cls, check_context=False):
138+
return _expect_raises(except_cls, check_context=check_context)
139+
140+
141+
def expect_raises_message(except_cls, msg, check_context=False):
142+
return _expect_raises(except_cls, msg=msg, check_context=check_context)
143+
144+
145+
def expect_raises_with_proper_context(except_cls, check_context=True):
156146
return _expect_raises(except_cls, check_context=check_context)
157147

158148

159-
def expect_raises_message(except_cls, msg, check_context=True):
149+
def expect_raises_message_with_proper_context(
150+
except_cls, msg, check_context=True
151+
):
160152
return _expect_raises(except_cls, msg=msg, check_context=check_context)

0 commit comments

Comments
 (0)