Skip to content

Commit

Permalink
Merge pull request #1747 from nicoddemus/line-match-stringio
Browse files Browse the repository at this point in the history
Log LineMatcher output in a stream instead of stderr
  • Loading branch information
The-Compiler authored Jul 21, 2016
2 parents eaa4ee3 + 4c11240 commit 832ada1
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ class LineMatcher:

def __init__(self, lines):
self.lines = lines
self._log_output = []

def str(self):
"""Return the entire original text."""
Expand All @@ -1072,10 +1073,11 @@ def fnmatch_lines_random(self, lines2):
for line in lines2:
for x in self.lines:
if line == x or fnmatch(x, line):
print_("matched: ", repr(line))
self._log("matched: ", repr(line))
break
else:
raise ValueError("line %r not found in output" % line)
self._log("line %r not found in output" % line)
raise ValueError(self._log_text)

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

def _log(self, *args):
self._log_output.append(' '.join((str(x) for x in args)))

@property
def _log_text(self):
return '\n'.join(self._log_output)

def fnmatch_lines(self, lines2):
"""Search the text for matching lines.
Expand All @@ -1096,8 +1105,6 @@ def fnmatch_lines(self, lines2):
stdout.
"""
def show(arg1, arg2):
py.builtin.print_(arg1, arg2, file=sys.stderr)
lines2 = self._getlines(lines2)
lines1 = self.lines[:]
nextline = None
Expand All @@ -1108,17 +1115,18 @@ def show(arg1, arg2):
while lines1:
nextline = lines1.pop(0)
if line == nextline:
show("exact match:", repr(line))
self._log("exact match:", repr(line))
break
elif fnmatch(nextline, line):
show("fnmatch:", repr(line))
show(" with:", repr(nextline))
self._log("fnmatch:", repr(line))
self._log(" with:", repr(nextline))
break
else:
if not nomatchprinted:
show("nomatch:", repr(line))
self._log("nomatch:", repr(line))
nomatchprinted = True
show(" and:", repr(nextline))
self._log(" and:", repr(nextline))
extralines.append(nextline)
else:
pytest.fail("remains unmatched: %r, see stderr" % (line,))
self._log("remains unmatched: %r" % (line,))
pytest.fail(self._log_text)

0 comments on commit 832ada1

Please sign in to comment.