Skip to content
Myr

🐛 Ensure that hidden commands are not shown when Rich markup is disabled (#1812)

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 c2d97d1 · Browse files · 6 files+103 added−54 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-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>

Diff stats

6 files+103 added−54 removed

  • tests:+101 added−54 removed
  • other:+2 added−0 removed

6 files+103 added−54 removed

deleted:tests/assets/corner_cases.py-19 removed
1import typer23app = typer.Typer()456@app.command(context_settings={"auto_envvar_prefix": "TEST"})7def main(8 name: str = typer.Option("John", hidden=True),9 lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),10 age: int = typer.Option(lambda: 42, show_default=True),11):12 """13 Say hello.14 """15 print(f"Hello {name} {lastname}, it seems you have {age}")161718if __name__ == "__main__":19 app()
added:tests/assets/hidden_commands.py+30 added
1import typer23app = typer.Typer()456@app.command()7def visible():8 """Visible command."""91011@app.command(hidden=True)12def hidden_decorated():13 """Hidden via @app.command(hidden=True)."""141516def hidden_var():17 """Hidden via app.command(name, hidden=True)(fn)."""181920app.command("hidden-var", hidden=True)(hidden_var)2122hidden_subgroup = typer.Typer(hidden=True)232425@hidden_subgroup.command()26def sub():27 """Hidden subgroup command."""282930app.add_typer(hidden_subgroup, name="hidden-subgroup")
added:tests/assets/hidden_option.py+19 added
1import typer23app = typer.Typer()456@app.command(context_settings={"auto_envvar_prefix": "TEST"})7def main(8 name: str = typer.Option("John", hidden=True),9 lastname: str = typer.Option("Doe", "/lastname", show_default="Mr. Doe"),10 age: int = typer.Option(lambda: 42, show_default=True),11):12 """13 Say hello.14 """15 print(f"Hello {name} {lastname}, it seems you have {age}")161718if __name__ == "__main__":19 app()
deleted:tests/test_corner_cases.py-35 removed
1import pytest2import typer.core3from typer.testing import CliRunner45from tests.assets import corner_cases as mod67runner = CliRunner()8910def test_hidden_option():11 result = runner.invoke(mod.app, ["--help"])12 assert result.exit_code == 013 assert "Say hello" in result.output14 assert "--name" not in result.output15 assert "/lastname" in result.output16 assert "TEST_LASTNAME" in result.output17 assert "(dynamic)" in result.output181920def test_hidden_option_no_rich(monkeypatch: pytest.MonkeyPatch):21 monkeypatch.setattr(typer.core, "HAS_RICH", False)2223 result = runner.invoke(mod.app, ["--help"])24 assert result.exit_code == 025 assert "Say hello" in result.output26 assert "--name" not in result.output27 assert "/lastname" in result.output28 assert "TEST_LASTNAME" in result.output29 assert "(dynamic)" in result.output303132def test_coverage_call():33 result = runner.invoke(mod.app)34 assert result.exit_code == 035 assert "Hello John Doe, it seems you have 42" in result.output
added:tests/test_hidden.py+52 added
1import pytest2import typer.core3from typer.testing import CliRunner45from tests.assets import hidden_commands as mod_command6from tests.assets import hidden_option as mod_option7from tests.utils import needs_rich89runner = CliRunner()101112@pytest.mark.parametrize(13 "use_rich",14 [15 pytest.param(False),16 pytest.param(True, marks=needs_rich),17 ],18)19def test_hidden_option(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:20 monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)2122 result = runner.invoke(mod_option.app, ["--help"])23 assert result.exit_code == 024 assert "Say hello" in result.output25 assert "--name" not in result.output26 assert "/lastname" in result.output27 assert "TEST_LASTNAME" in result.output28 assert "(dynamic)" in result.output293031def test_coverage_call() -> None:32 result = runner.invoke(mod_option.app)33 assert result.exit_code == 034 assert "Hello John Doe, it seems you have 42" in result.output353637@pytest.mark.parametrize(38 "use_rich",39 [40 pytest.param(False),41 pytest.param(True, marks=needs_rich),42 ],43)44def test_hidden_commands(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:45 monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)4647 result = runner.invoke(mod_command.app, ["--help"])48 assert result.exit_code == 049 assert "visible" in result.output50 assert "hidden-decorated" not in result.output51 assert "hidden-var" not in result.output52 assert "hidden-subgroup" not in result.output
modified:typer/core.py+2 added
1049 unmodified lines
1050 ) -> None:1051 commands = []1052 for subcommand in self.list_commands(ctx):1053 cmd = self.get_command(ctx, subcommand)1054 if cmd is None or cmd.hidden:1055 continue10561057 commands.append((subcommand, cmd))10581059 # allow for 3 times the default spacing
158 unmodified lines