Skip to content

Commit 2228ccb

Browse files
committed
pytester: reset log output in _match_lines (#70)
This is necessary for when using e.g. `no_fnmatch_line` after it. Factor it out into `_fail`. (cherry picked from commit aade7ed) Ref: #5914 (comment)
1 parent 7e5ad31 commit 2228ccb

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

changelog/5914.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytester: fix ``no_fnmatch_line`` when used after positive matching.

src/_pytest/pytester.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,8 +1438,10 @@ def _match_lines(self, lines2, match_func, match_nickname):
14381438
self._log("{:>{width}}".format("and:", width=wnick), repr(nextline))
14391439
extralines.append(nextline)
14401440
else:
1441-
self._log("remains unmatched: {!r}".format(line))
1442-
pytest.fail(self._log_text.lstrip())
1441+
msg = "remains unmatched: {!r}".format(line)
1442+
self._log(msg)
1443+
self._fail(msg)
1444+
self._log_output = []
14431445

14441446
def no_fnmatch_line(self, pat):
14451447
"""Ensure captured lines do not match the given pattern, using ``fnmatch.fnmatch``.
@@ -1465,18 +1467,20 @@ def _no_match_line(self, pat, match_func, match_nickname):
14651467
__tracebackhide__ = True
14661468
nomatch_printed = False
14671469
wnick = len(match_nickname) + 1
1468-
try:
1469-
for line in self.lines:
1470-
if match_func(line, pat):
1471-
self._log("%s:" % match_nickname, repr(pat))
1472-
self._log("{:>{width}}".format("with:", width=wnick), repr(line))
1473-
pytest.fail(self._log_text.lstrip())
1474-
else:
1475-
if not nomatch_printed:
1476-
self._log(
1477-
"{:>{width}}".format("nomatch:", width=wnick), repr(pat)
1478-
)
1479-
nomatch_printed = True
1480-
self._log("{:>{width}}".format("and:", width=wnick), repr(line))
1481-
finally:
1482-
self._log_output = []
1470+
for line in self.lines:
1471+
if match_func(line, pat):
1472+
msg = "{}: {!r}".format(match_nickname, pat)
1473+
self._log(msg)
1474+
self._log("{:>{width}}".format("with:", width=wnick), repr(line))
1475+
self._fail(msg)
1476+
else:
1477+
if not nomatch_printed:
1478+
self._log("{:>{width}}".format("nomatch:", width=wnick), repr(pat))
1479+
nomatch_printed = True
1480+
self._log("{:>{width}}".format("and:", width=wnick), repr(line))
1481+
self._log_output = []
1482+
1483+
def _fail(self, msg):
1484+
log_text = self._log_text
1485+
self._log_output = []
1486+
pytest.fail(log_text)

testing/test_pytester.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ def test_no_matching(function):
530530
]
531531
else:
532532
assert obtained == [
533-
"nomatch: '{}'".format(good_pattern),
533+
" nomatch: '{}'".format(good_pattern),
534534
" and: 'cachedir: .pytest_cache'",
535535
" and: 'collecting ... collected 1 item'",
536536
" and: ''",
@@ -542,6 +542,14 @@ def test_no_matching(function):
542542
func(bad_pattern) # bad pattern does not match any line: passes
543543

544544

545+
def test_no_matching_after_match():
546+
lm = LineMatcher(["1", "2", "3"])
547+
lm.fnmatch_lines(["1", "3"])
548+
with pytest.raises(pytest.fail.Exception) as e:
549+
lm.no_fnmatch_line("*")
550+
assert str(e.value).splitlines() == ["fnmatch: '*'", " with: '1'"]
551+
552+
545553
def test_pytester_addopts(request, monkeypatch):
546554
monkeypatch.setenv("PYTEST_ADDOPTS", "--orig-unused")
547555

0 commit comments

Comments
 (0)