-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path_test_utils.py
More file actions
62 lines (47 loc) · 1.74 KB
/
Copy path_test_utils.py
File metadata and controls
62 lines (47 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Another sad little utility module."""
import traceback
import attr
from testtools.matchers import Equals, Mismatch
@attr.s
class ReraisedTracebackMismatch(object):
expected_tb = attr.ib()
got_tb = attr.ib()
def describe(self):
return (
"The reference traceback:\n"
+ "".join(self.expected_tb)
+ "\nshould match the tail end of the received traceback:\n"
+ "".join(self.got_tb)
+ "\nbut it doesn't."
)
@attr.s
class MatchesException(object):
expected = attr.ib()
def match(self, other):
expected_type = type(self.expected)
if type(other) is not expected_type:
return Mismatch("{} is not a {}".format(other, expected_type))
if other.args != self.expected.args:
return Mismatch(
"{} has different arguments: {}.".format(other.args, self.expected.args)
)
@attr.s
class MatchesReraisedExcInfo(object):
expected = attr.ib()
def match(self, actual):
valcheck = Equals(self.expected.args).match(actual.args)
if valcheck is not None:
return valcheck
typecheck = Equals(type(self.expected)).match(type(actual))
if typecheck is not None:
return typecheck
expected = list(
traceback.TracebackException.from_exception(self.expected).format()
)
new = list(traceback.TracebackException.from_exception(actual).format())
tail_equals = lambda a, b: a == b[-len(a) :]
if not tail_equals(expected[1:], new[1:]):
return ReraisedTracebackMismatch(expected_tb=expected, got_tb=new)
def raise_(e):
"""Raise an exception instance. Exists so you can raise in a lambda."""
raise e