Skip to content
Myr

🐛 Fix newline after header in help text, and add more tests for the behaviour of `rich_markup_mode` (#964)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sofie Van Landeghem · committed by GitHub · parent 202f55a · Browse files · 2 files+251 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Diff stats

2 files+251 added−0 removed

  • tests:+248 added−0 removed
  • other:+3 added−0 removed

2 files+251 added−0 removed

modified:tests/test_rich_markup_mode.py+248 added
1from typing import List23import pytest4import typer5import typer.completion6from typer.testing import CliRunner7
32 unmodified lines
40 assert "Hello World" in result.stdout4142 result = runner.invoke(app, ["--help"])43 assert any(c in result.stdout for c in rounded)444546@pytest.mark.parametrize(47 "mode,lines",48 [49 pytest.param(50 "markdown",51 ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""],52 marks=pytest.mark.xfail,53 ),54 pytest.param(55 "rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]56 ),57 pytest.param(58 None, ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]59 ),60 ],61)62def test_markup_mode_newline_pr815(mode: str, lines: List[str]):63 app = typer.Typer(rich_markup_mode=mode)6465 @app.command()66 def main(arg: str):67 """First line6869 Line 17071 Line 27273 Line 374 """75 print(f"Hello {arg}")7677 assert app.rich_markup_mode == mode7879 result = runner.invoke(app, ["World"])80 assert "Hello World" in result.stdout8182 result = runner.invoke(app, ["--help"])83 result_lines = [line.strip() for line in result.stdout.split("\n")]84 if mode:85 assert any(c in result.stdout for c in rounded)86 help_start = result_lines.index("First line")87 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]88 assert help_start != -189 assert result_lines[help_start:arg_start] == lines909192@pytest.mark.parametrize(93 "mode,lines",94 [95 pytest.param("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]),96 pytest.param("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),97 pytest.param(None, ["First line", "", "Line 1 Line 2 Line 3", ""]),98 ],99)100def test_markup_mode_newline_issue447(mode: str, lines: List[str]):101 app = typer.Typer(rich_markup_mode=mode)102103 @app.command()104 def main(arg: str):105 """First line106107 Line 1108 Line 2109 Line 3110 """111 print(f"Hello {arg}")112113 assert app.rich_markup_mode == mode114115 result = runner.invoke(app, ["World"])116 assert "Hello World" in result.stdout117118 result = runner.invoke(app, ["--help"])119 result_lines = [line.strip() for line in result.stdout.split("\n")]120 if mode:121 assert any(c in result.stdout for c in rounded)122 help_start = result_lines.index("First line")123 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]124 assert help_start != -1125 assert result_lines[help_start:arg_start] == lines126127128@pytest.mark.parametrize(129 "mode,lines",130 [131 pytest.param(132 "markdown",133 [134 "This header is long",135 "",136 "Line 1",137 "",138 "Line 2 continues here",139 "",140 "Line 3",141 "",142 ],143 marks=pytest.mark.xfail,144 ),145 pytest.param(146 "rich",147 [148 "This header is long",149 "",150 "Line 1",151 "",152 "Line 2",153 "continues here",154 "",155 "Line 3",156 "",157 ],158 ),159 pytest.param(160 None,161 [162 "This header is long",163 "",164 "Line 1",165 "",166 "Line 2 continues here",167 "",168 "Line 3",169 "",170 ],171 ),172 ],173)174def test_markup_mode_newline_mixed(mode: str, lines: List[str]):175 app = typer.Typer(rich_markup_mode=mode)176177 @app.command()178 def main(arg: str):179 """This header180 is long181182 Line 1183184 Line 2185 continues here186187 Line 3188 """189 print(f"Hello {arg}")190191 assert app.rich_markup_mode == mode192193 result = runner.invoke(app, ["World"])194 assert "Hello World" in result.stdout195196 result = runner.invoke(app, ["--help"])197 result_lines = [line.strip() for line in result.stdout.split("\n")]198 if mode:199 assert any(c in result.stdout for c in rounded)200 help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0]201 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]202 assert help_start != -1203 assert result_lines[help_start:arg_start] == lines204205206@pytest.mark.parametrize(207 "mode,lines",208 [209 pytest.param(210 "markdown",211 ["First line", "", "• 1", "• 2", "• 3", ""],212 marks=pytest.mark.xfail,213 ),214 pytest.param("rich", ["First line", "", "- 1", "- 2", "- 3", ""]),215 pytest.param(None, ["First line", "", "- 1 - 2 - 3", ""]),216 ],217)218def test_markup_mode_bullets_single_newline(mode: str, lines: List[str]):219 app = typer.Typer(rich_markup_mode=mode)220221 @app.command()222 def main(arg: str):223 """First line224225 - 1226 - 2227 - 3228 """229 print(f"Hello {arg}")230231 assert app.rich_markup_mode == mode232233 result = runner.invoke(app, ["World"])234 assert "Hello World" in result.stdout235236 result = runner.invoke(app, ["--help"])237 result_lines = [line.strip() for line in result.stdout.split("\n")]238 if mode:239 assert any(c in result.stdout for c in rounded)240 help_start = result_lines.index("First line")241 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]242 assert help_start != -1243 assert result_lines[help_start:arg_start] == lines244245246@pytest.mark.parametrize(247 "mode,lines",248 [249 pytest.param(250 "markdown",251 ["First line", "", "• 1", "• 2", "• 3", ""],252 marks=pytest.mark.xfail,253 ),254 (255 "rich",256 ["First line", "", "- 1", "", "- 2", "", "- 3", ""],257 ),258 (None, ["First line", "", "- 1", "", "- 2", "", "- 3", ""]),259 ],260)261def test_markup_mode_bullets_double_newline(mode: str, lines: List[str]):262 app = typer.Typer(rich_markup_mode=mode)263264 @app.command()265 def main(arg: str):266 """First line267268 - 1269270 - 2271272 - 3273 """274 print(f"Hello {arg}")275276 assert app.rich_markup_mode == mode277278 result = runner.invoke(app, ["World"])279 assert "Hello World" in result.stdout280281 result = runner.invoke(app, ["--help"])282 result_lines = [line.strip() for line in result.stdout.split("\n")]283 if mode:284 assert any(c in result.stdout for c in rounded)285 help_start = result_lines.index("First line")286 arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]287 assert help_start != -1288 assert result_lines[help_start:arg_start] == lines
modified:typer/rich_utils.py+3 added
198 unmodified lines
199 style=STYLE_HELPTEXT_FIRST_LINE,200 markup_mode=markup_mode,201 )202203 # Add a newline inbetween the header and the remaining paragraphs204 yield Text("")205206 # Get remaining lines, remove single line breaks and format as dim207 remaining_paragraphs = help_text.split("\n\n")[1:]208 if remaining_paragraphs:209 if markup_mode != MARKUP_MODE_RICH:
526 unmodified lines