8000 🐛 Fix newline after header in help text, and add more tests for the behaviour of `rich_markup_mode` by svlandeg · Pull Request #964 · fastapi/typer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact 8000 its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletions tests/test_rich_markup_mode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import List

import pytest
import typer
import typer.completion
from typer.testing import CliRunner
Expand Down Expand Up @@ -38,3 +41,248 @@ def main(arg: str):

result = runner.invoke(app, ["--help"])
assert any(c in result.stdout for c in rounded)


@pytest.mark.parametrize(
"mode,lines",
[
pytest.param(
"markdown",
["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""],
marks=pytest.mark.xfail,
),
pytest.param(
"rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
),
pytest.param(
None, ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]
),
],
)
def test_markup_mode_newline_pr815(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

Line 1

Line 2

Line 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
if mode:
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
pytest.param("markdown", ["First line", "", "Line 1 Line 2 Line 3", ""]),
pytest.param("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),
pytest.param(None, ["First line", "", "Line 1 Line 2 Line 3", ""]),
],
)
def test_markup_mode_newline_issue447(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

Line 1
Line 2
Line 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
if mode:
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
pytest.param(
"markdown",
[
"This header is long",
"",
"Line 1",
"",
"Line 2 continues here",
"",
"Line 3",
"",
],
marks=pytest.mark.xfail,
),
pytest.param(
"rich",
[
"This header is long",
"",
"Line 1",
"",
"Line 2",
"continues here",
"",
"Line 3",
"",
],
),
pytest.param(
None,
[
"This header is long",
"",
"Line 1",
"",
"Line 2 continues here",
"",
"Line 3",
"",
],
),
],
)
def test_markup_mode_newline_mixed(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""This header
is long

Line 1

Line 2
continues here

Line 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
if mode:
assert any(c in result.stdout for c in rounded)
help_start = [i for i, row in enumerate(result_lines) if "This header" in row][0]
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
pytest.param(
"markdown",
["First line", "", "• 1", "• 2", "• 3", ""],
marks=pytest.mark.xfail,
),
pytest.param("rich", ["First line", "", "- 1", "- 2", "- 3", ""]),
pytest.param(None, ["First line", "", "- 1 - 2 - 3", ""]),
],
)
def test_markup_mode_bullets_single_newline(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

- 1
- 2
- 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
if mode:
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
pytest.param(
"markdown",
["First line", "", "• 1", "• 2", "• 3", ""],
marks=pytest.mark.xfail,
),
(
"rich",
["First line", "", "- 1", "", "- 2", "", "- 3", ""],
),
(None, ["First line", "", "- 1", "", "- 2", "", "- 3", ""]),
],
)
def test_markup_mode_bullets_double_newline(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

- 1

- 2

- 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
if mode:
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines
3 changes: 3 additions & 0 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ def _get_help_text(
markup_mode=markup_mode,
)

# Add a newline inbetween the header and the remaining paragraphs
yield Text("")

# Get remaining lines, remove single line breaks and format as dim
remaining_paragraphs = help_text.split("\n\n")[1:]
if remaining_paragraphs:
Expand Down
Loading
0