Skip to content

Commit 832ada1

Browse files
authored
Merge pull request #1747 from nicoddemus/line-match-stringio
Log LineMatcher output in a stream instead of stderr
2 parents eaa4ee3 + 4c11240 commit 832ada1

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

_pytest/pytester.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ class LineMatcher:
10491049

10501050
def __init__(self, lines):
10511051
self.lines = lines
1052+
self._log_output = []
10521053

10531054
def str(self):
10541055
"""Return the entire original text."""
@@ -1072,10 +1073,11 @@ def fnmatch_lines_random(self, lines2):
10721073
for line in lines2:
10731074
for x in self.lines:
10741075
if line == x or fnmatch(x, line):
1075-
print_("matched: ", repr(line))
1076+
self._log("matched: ", repr(line))
10761077
break
10771078
else:
1078-
raise ValueError("line %r not found in output" % line)
1079+
self._log("line %r not found in output" % line)
1080+
raise ValueError(self._log_text)
10791081

10801082
def get_lines_after(self, fnline):
10811083
"""Return all lines following the given line in the text.
@@ -1087,6 +1089,13 @@ def get_lines_after(self, fnline):
10871089
return self.lines[i+1:]
10881090
raise ValueError("line %r not found in output" % fnline)
10891091

1092+
def _log(self, *args):
1093+
self._log_output.append(' '.join((str(x) for x in args)))
1094+
1095+
@property
1096+
def _log_text(self):
1097+
return '\n'.join(self._log_output)
1098+
10901099
def fnmatch_lines(self, lines2):
10911100
"""Search the text for matching lines.
10921101
@@ -1096,8 +1105,6 @@ def fnmatch_lines(self, lines2):
10961105
stdout.
10971106
10981107
"""
1099-
def show(arg1, arg2):
1100-
py.builtin.print_(arg1, arg2, file=sys.stderr)
11011108
lines2 = self._getlines(lines2)
11021109
lines1 = self.lines[:]
11031110
nextline = None
@@ -1108,17 +1115,18 @@ def show(arg1, arg2):
11081115
while lines1:
11091116
nextline = lines1.pop(0)
11101117
if line == nextline:
1111-
show("exact match:", repr(line))
1118+
self._log("exact match:", repr(line))
11121119
break
11131120
elif fnmatch(nextline, line):
1114-
show("fnmatch:", repr(line))
1115-
show(" with:", repr(nextline))
1121+
self._log("fnmatch:", repr(line))
1122+
self._log(" with:", repr(nextline))
11161123
break
11171124
else:
11181125
if not nomatchprinted:
1119-
show("nomatch:", repr(line))
1126+
self._log("nomatch:", repr(line))
11201127
nomatchprinted = True
1121-
show(" and:", repr(nextline))
1128+
self._log(" and:", repr(nextline))
11221129
extralines.append(nextline)
11231130
else:
1124-
pytest.fail("remains unmatched: %r, see stderr" % (line,))
1131+
self._log("remains unmatched: %r" % (line,))
1132+
pytest.fail(self._log_text)

0 commit comments

Comments
 (0)