From 14bf4974b5a48e504026cb94905749fedd556d38 Mon Sep 17 00:00:00 2001 From: HolyMagician03-UMich Date: Sun, 7 Apr 2024 10:20:34 -0400 Subject: [PATCH] adopted suggested changes, made testcase --- changelog/11777.bugfix.rst | 1 - changelog/11777.improvement.rst | 1 + src/_pytest/terminal.py | 11 ++---- testing/test_terminal.py | 69 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 8 deletions(-) delete mode 100644 changelog/11777.bugfix.rst create mode 100644 changelog/11777.improvement.rst diff --git a/changelog/11777.bugfix.rst b/changelog/11777.bugfix.rst deleted file mode 100644 index d59b153d1..000000000 --- a/changelog/11777.bugfix.rst +++ /dev/null @@ -1 +0,0 @@ -Added support for verbosity setting in the short test summary info, so that when -vv is enabled, longer text will be printed out. diff --git a/changelog/11777.improvement.rst b/changelog/11777.improvement.rst new file mode 100644 index 000000000..f7fc42b67 --- /dev/null +++ b/changelog/11777.improvement.rst @@ -0,0 +1 @@ +Text is no longer truncated in the ``short test summary info`` section when ``-vv`` is given. \ No newline at end of file diff --git a/src/_pytest/terminal.py b/src/_pytest/terminal.py index f81dd003f..724d5c54d 100644 --- a/src/_pytest/terminal.py +++ b/src/_pytest/terminal.py @@ -1408,14 +1408,11 @@ def _get_line_with_reprcrash_message( except AttributeError: pass else: - if not running_on_ci(): - if isinstance(config, Config) and config.option.verbose >= 2: - available_width = 500 - else: - available_width = tw.fullwidth - line_width - msg = _format_trimmed(" - {}", msg, available_width) - else: + if running_on_ci() or config.option.verbose >= 2: msg = f" - {msg}" + else: + available_width = tw.fullwidth - line_width + msg = _format_trimmed(" - {}", msg, available_width) if msg is not None: line += msg diff --git a/testing/test_terminal.py b/testing/test_terminal.py index f49425109..9c17708cc 100644 --- a/testing/test_terminal.py +++ b/testing/test_terminal.py @@ -2443,6 +2443,75 @@ def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None: check("🉐🉐🉐🉐🉐\n2nd line", 80, "FAILED nodeid::🉐::withunicode - 🉐🉐🉐🉐🉐") +def test_short_summary_with_verbose(monkeypatch: MonkeyPatch) -> None: + mocked_verbose_word = "FAILED" + + mocked_pos = "some::nodeid" + + def mock_running_on_ci(): + return False + + def mock_get_pos(*args): + return mocked_pos + + monkeypatch.setattr(_pytest.terminal, "_get_node_id_with_markup", mock_get_pos) + monkeypatch.setattr(_pytest.terminal, "running_on_ci", mock_running_on_ci) + + class Namespace: + def __init__(self, **kwargs): + self.__dict__.update(kwargs) + + class config: + def __init__(self): + object.__setattr__(self, "option", Namespace(verbose=2)) + + class rep: + def _get_verbose_word(self, *args): + return mocked_verbose_word + + class longrepr: + class reprcrash: + pass + + def check(msg, width, expected): + class DummyTerminalWriter: + fullwidth = width + + def markup(self, word: str, **markup: str): + return word + + __tracebackhide__ = True + if msg: + rep.longrepr.reprcrash.message = msg # type: ignore + actual = _get_line_with_reprcrash_message( + config(), # type: ignore[arg-type] + rep(), # type: ignore[arg-type] + DummyTerminalWriter(), # type: ignore[arg-type] + {}, + ) + + assert actual == expected + + # AttributeError with message + check(None, 80, "FAILED some::nodeid") + + check("msg", 80, "FAILED some::nodeid - msg") + check("msg", 3, "FAILED some::nodeid - msg") + + check("some longer msg", 10, "FAILED some::nodeid - some longer msg") + + check("some\nmessage", 25, "FAILED some::nodeid - some\nmessage") + check("some\nmessage", 80, "FAILED some::nodeid - some\nmessage") + + # Test unicode safety. + check("🉐🉐🉐🉐🉐\n2nd line", 29, "FAILED some::nodeid - 🉐🉐🉐🉐🉐\n2nd line") + + # NOTE: constructed, not sure if this is supported. + mocked_pos = "nodeid::🉐::withunicode" + check("🉐🉐🉐🉐🉐\n2nd line", 29, "FAILED nodeid::🉐::withunicode - 🉐🉐🉐🉐🉐\n2nd line") + check("🉐🉐🉐🉐🉐\n2nd line", 80, "FAILED nodeid::🉐::withunicode - 🉐🉐🉐🉐🉐\n2nd line") + + @pytest.mark.parametrize( "seconds, expected", [