diff --git a/AUTHORS b/AUTHORS index ad4c2093892..47e0ad7a5e8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -366,6 +366,7 @@ Nico Vidal Nikesh Chavhan Nikolay Kondratyev Nipunn Koorapati +niukanen1 Oleg Pidsadnyi Oleg Sushchenko Oleksandr Zavertniev diff --git a/changelog/15021.bugfix.rst b/changelog/15021.bugfix.rst new file mode 100644 index 00000000000..f60d35dfb16 --- /dev/null +++ b/changelog/15021.bugfix.rst @@ -0,0 +1 @@ +Preserved blank lines between paragraphs in command-line option help text. diff --git a/src/_pytest/config/argparsing.py b/src/_pytest/config/argparsing.py index d435715785d..dc66aa10185 100644 --- a/src/_pytest/config/argparsing.py +++ b/src/_pytest/config/argparsing.py @@ -574,7 +574,7 @@ def _split_lines(self, text: str, width: int) -> list[str]: """ lines = [] for line in text.splitlines(): - lines.extend(textwrap.wrap(line.strip(), width)) + lines.extend(textwrap.wrap(line.strip(), width) or [""]) return lines diff --git a/testing/test_parseopt.py b/testing/test_parseopt.py index f58754aae27..6cda1c9626d 100644 --- a/testing/test_parseopt.py +++ b/testing/test_parseopt.py @@ -286,6 +286,31 @@ def test_drop_short_help0(self, parser: parseopt.Parser) -> None: help = parser.optparser.format_help() assert "--func-args, --doit foo" in help + def test_help_preserves_blank_lines(self, parser: parseopt.Parser) -> None: + parser.addoption( + "--paragraph-help", + action="store_true", + help="first paragraph\n\nsecond paragraph", + ) + + lines = parser.optparser.format_help().splitlines() + first_line = next( + i for i, line in enumerate(lines) if "first paragraph" in line + ) + + assert lines[first_line + 1].strip() == "" + assert lines[first_line + 2].strip() == "second paragraph" + + def test_help_line_wrapping_with_blank_lines(self) -> None: + formatter = parseopt.DropShorterLongHelpFormatter("prog") + + assert formatter._split_lines("first paragraph\n\nsecond paragraph", 80) == [ + "first paragraph", + "", + "second paragraph", + ] + assert formatter._split_lines("one two three", 7) == ["one two", "three"] + # testing would be more helpful with all help generated def test_drop_short_help1(self, parser: parseopt.Parser) -> None: group = parser.getgroup("general")