-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy patherror.py
More file actions
116 lines (89 loc) · 3.48 KB
/
error.py
File metadata and controls
116 lines (89 loc) · 3.48 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from __future__ import annotations
import os
import string
from abc import ABC
from abc import abstractmethod
from typing import Sequence
from typing import Tuple
from _pytest._code.code import ReprFileLocation
from _pytest._io import TerminalWriter
class CppFailureError(Exception):
"""
Should be raised by test Facades when a test fails.
"""
def __init__(self, failures: Sequence[CppTestFailure]) -> None:
self.failures = list(failures)
Markup = Tuple[str, ...]
class CppTestFailure(ABC):
"""
Represents a failure in a C++ test. Each framework
must implement the abstract functions to build the final exception
message that will be displayed in the terminal.
"""
@abstractmethod
def get_lines(self) -> list[tuple[str, Markup]]:
"""
Returns list of (line, markup) that will be displayed to the user,
where markup can be a sequence of color codes from
TerminalWriter._esctable:
'black', 'red', 'green', 'yellow',
'blue', 'purple', 'cyan', 'white',
'bold', 'light', 'blink', 'invert'
"""
@abstractmethod
def get_file_reference(self) -> tuple[str, int]:
"""
Return tuple of filename, linenum of the failure.
"""
class CppFailureRepr(object):
"""
"repr" object for pytest that knows how to print a CppFailure instance
into both terminal and files.
"""
failure_sep = "---"
def __init__(self, failures: Sequence[CppTestFailure]) -> None:
self.failures = list(failures)
def __str__(self) -> str:
reprs = []
for failure in self.failures:
pure_lines = "\n".join(x[0] for x in failure.get_lines())
repr_loc = self._get_repr_file_location(failure)
reprs.append("%s\n%s" % (pure_lines, repr_loc))
return self.failure_sep.join(reprs)
def _get_repr_file_location(self, failure: CppTestFailure) -> ReprFileLocation:
filename, linenum = failure.get_file_reference()
return ReprFileLocation(filename, linenum, "C++ failure")
def toterminal(self, tw: TerminalWriter) -> None:
for index, failure in enumerate(self.failures):
filename, linenum = failure.get_file_reference()
code_lines = get_code_context_around_line(filename, linenum)
for line in code_lines:
tw.line(line, white=True, bold=True) # pragma: no cover
indent = get_left_whitespace(code_lines[-1]) if code_lines else ""
for line, markup in failure.get_lines():
markup_params = {m: True for m in markup}
tw.line(indent + line, **markup_params)
location = self._get_repr_file_location(failure)
location.toterminal(tw)
if index != len(self.failures) - 1:
tw.line(self.failure_sep, cyan=True)
def get_code_context_around_line(filename: str, linenum: int) -> list[str]:
"""
return code context lines, with the last line being the line at
linenum.
"""
if os.path.isfile(filename):
index = linenum - 1
with open(filename) as f:
index_above = index - 2
index_above = index_above if index_above >= 0 else 0
return [x.rstrip() for x in f.readlines()[index_above : index + 1]]
return []
def get_left_whitespace(line: str) -> str:
result = ""
for c in line:
if c in string.whitespace:
result += c
else:
break
return result