🐛 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
Provenance
- Commit
9172486- Tree
8d9ac30- Author
Sofie Van Landeghem <svlandeg@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c2d97d1
6 files changed+103 added−54 removed
deleted:tests/assets/corner_cases.py-19 removed
1-import typer2-3-app = typer.Typer()4-5-6-@app.command(context_settings={"auto_envvar_prefix": "TEST"})7-def 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}")16-17-18-if __name__ == "__main__":19- app()added:tests/assets/hidden_commands.py+30 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command()7+def visible():8+ """Visible command."""9+10+11+@app.command(hidden=True)12+def hidden_decorated():13+ """Hidden via @app.command(hidden=True)."""14+15+16+def hidden_var():17+ """Hidden via app.command(name, hidden=True)(fn)."""18+19+20+app.command("hidden-var", hidden=True)(hidden_var)21+22+hidden_subgroup = typer.Typer(hidden=True)23+24+25+@hidden_subgroup.command()26+def sub():27+ """Hidden subgroup command."""28+29+30+app.add_typer(hidden_subgroup, name="hidden-subgroup")added:tests/assets/hidden_option.py+19 added
1+import typer2+3+app = typer.Typer()4+5+6+@app.command(context_settings={"auto_envvar_prefix": "TEST"})7+def 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}")16+17+18+if __name__ == "__main__":19+ app()deleted:tests/test_corner_cases.py-35 removed
1-import pytest2-import typer.core3-from typer.testing import CliRunner4-5-from tests.assets import corner_cases as mod6-7-runner = CliRunner()8-9-10-def 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.output18-19-20-def test_hidden_option_no_rich(monkeypatch: pytest.MonkeyPatch):21- monkeypatch.setattr(typer.core, "HAS_RICH", False)22-23- 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.output30-31-32-def 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.outputadded:tests/test_hidden.py+52 added
1+import pytest2+import typer.core3+from typer.testing import CliRunner4+5+from tests.assets import hidden_commands as mod_command6+from tests.assets import hidden_option as mod_option7+from tests.utils import needs_rich8+9+runner = CliRunner()10+11+12+@pytest.mark.parametrize(13+ "use_rich",14+ [15+ pytest.param(False),16+ pytest.param(True, marks=needs_rich),17+ ],18+)19+def test_hidden_option(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:20+ monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)21+22+ 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.output29+30+31+def 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.output35+36+37+@pytest.mark.parametrize(38+ "use_rich",39+ [40+ pytest.param(False),41+ pytest.param(True, marks=needs_rich),42+ ],43+)44+def test_hidden_commands(monkeypatch: pytest.MonkeyPatch, use_rich: bool) -> None:45+ monkeypatch.setattr(typer.core, "HAS_RICH", use_rich)46+47+ 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.outputmodified:typer/core.py+2 added
1049 unmodified lines1050 ) -> 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+ continue1056 1057 commands.append((subcommand, cmd))1058 1059 # allow for 3 times the default spacing158 unmodified lines