Skip to content
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
30 changes: 30 additions & 0 deletions tests/assets/hidden_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import typer

app = typer.Typer()


@app.command()
def visible():
"""Visible command."""


@app.command(hidden=True)
def hidden_decorated():
"""Hidden via @app.command(hidden=True)."""


def hidden_var():
"""Hidden via app.command(name, hidden=True)(fn)."""


app.command("hidden-var", hidden=True)(hidden_var)

hidden_subgroup = typer.Typer(hidden=True)


@hidden_subgroup.command()
def sub():
"""Hidden subgroup command."""


app.add_typer(hidden_subgroup, name="hidden-subgroup")
File renamed without changes.
35 changes: 0 additions & 35 deletions tests/test_corner_cases.py

This file was deleted.

52 changes: 52 additions & 0 deletions tests/test_hidden.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest
import typer.core
from typer.testing import CliRunner

from tests.assets import hidden_commands as mod_command
from tests.assets import hidden_option as mod_option
from tests.utils import needs_rich

runner = CliRunner()


@pytest.mark.parametrize(
"use_rich",
[
pytest.param(False),
pytest.param(True, marks=needs_rich),
],
)
def test_hidden_option(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:
monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)

result = runner.invoke(mod_option.app, ["--help"])
assert result.exit_code == 0
assert "Say hello" in result.output
assert "--name" not in result.output
assert "/lastname" in result.output
assert "TEST_LASTNAME" in result.output
assert "(dynamic)" in result.output


def test_coverage_call() -> None:
result = runner.invoke(mod_option.app)
assert result.exit_code == 0
assert "Hello John Doe, it seems you have 42" in result.output


@pytest.mark.parametrize(
"use_rich",
[
pytest.param(False),
pytest.param(True, marks=needs_rich),
],
)
def test_hidden_commands(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:
monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)

result = runner.invoke(mod_command.app, ["--help"])
assert result.exit_code == 0
assert "visible" in result.output
assert "hidden-decorated" not in result.output
assert "hidden-var" not in result.output
assert "hidden-subgroup" not in result.output
2 changes: 2 additions & 0 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,8 @@ def format_commands(
commands = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
if cmd is None or cmd.hidden:
continue

commands.append((subcommand, cmd))

Expand Down
Loading