Wrap reason in verbose output

This commit is contained in:
Brian Larsen 2023-04-30 19:49:50 -05:00
parent 762bb61562
commit 64ed241f5f
2 changed files with 32 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import datetime
import inspect import inspect
import platform import platform
import sys import sys
import textwrap
import warnings import warnings
from collections import Counter from collections import Counter
from functools import partial from functools import partial
@ -426,6 +427,28 @@ class TerminalReporter:
self._tw.line() self._tw.line()
self.currentfspath = None self.currentfspath = None
def wrap_write(
self,
content: str,
*,
flush: bool = False,
margin: int = 8,
line_sep: str = "\n",
**markup: bool,
) -> None:
"""Wrap message with margin for progress info."""
width_of_current_line = self._tw.width_of_current_line
wrapped = line_sep.join(
textwrap.wrap(
" " * width_of_current_line + content,
width=self._screen_width - margin,
drop_whitespace=True,
replace_whitespace=False,
),
)
wrapped = wrapped[width_of_current_line:]
self._tw.write(wrapped, flush=flush, **markup)
def write(self, content: str, *, flush: bool = False, **markup: bool) -> None: def write(self, content: str, *, flush: bool = False, **markup: bool) -> None:
self._tw.write(content, flush=flush, **markup) self._tw.write(content, flush=flush, **markup)
@ -572,7 +595,7 @@ class TerminalReporter:
formatted_reason = f" ({reason})" formatted_reason = f" ({reason})"
if reason and formatted_reason is not None: if reason and formatted_reason is not None:
self._tw.write(formatted_reason) self.wrap_write(formatted_reason)
if self._show_progress_info: if self._show_progress_info:
self._write_progress_information_filling_space() self._write_progress_information_filling_space()
else: else:

View File

@ -387,13 +387,13 @@ class TestTerminal:
pytest.xfail("It's 🕙 o'clock") pytest.xfail("It's 🕙 o'clock")
@pytest.mark.skip( @pytest.mark.skip(
reason="cannot do foobar because baz is missing due to I don't know what" reason="1 cannot do foobar because baz is missing due to I don't know what"
) )
def test_long_skip(): def test_long_skip():
pass pass
@pytest.mark.xfail( @pytest.mark.xfail(
reason="cannot do foobar because baz is missing due to I don't know what" reason="2 cannot do foobar because baz is missing due to I don't know what"
) )
def test_long_xfail(): def test_long_xfail():
print(1 / 0) print(1 / 0)
@ -417,8 +417,8 @@ class TestTerminal:
result.stdout.fnmatch_lines( result.stdout.fnmatch_lines(
common_output common_output
+ [ + [
"test_verbose_skip_reason.py::test_long_skip SKIPPED (cannot *...) *", "test_verbose_skip_reason.py::test_long_skip SKIPPED (1 cannot *...) *",
"test_verbose_skip_reason.py::test_long_xfail XFAIL (cannot *...) *", "test_verbose_skip_reason.py::test_long_xfail XFAIL (2 cannot *...) *",
] ]
) )
@ -428,12 +428,14 @@ class TestTerminal:
+ [ + [
( (
"test_verbose_skip_reason.py::test_long_skip SKIPPED" "test_verbose_skip_reason.py::test_long_skip SKIPPED"
" (cannot do foobar because baz is missing due to I don't know what) *" " (1 cannot do foobar"
), ),
"because baz is missing due to I don't know what) *",
( (
"test_verbose_skip_reason.py::test_long_xfail XFAIL" "test_verbose_skip_reason.py::test_long_xfail XFAIL"
" (cannot do foobar because baz is missing due to I don't know what) *" " (2 cannot do foobar"
), ),
"because baz is missing due to I don't know what) *",
] ]
) )