Coverage for tests / test_suggest_commands.py: 100%
42 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-03-26 21:46 +0000
1import typer 1abcdefg
2from typer.testing import CliRunner 1abcdefg
4runner = CliRunner() 1abcdefg
7def test_typo_suggestion_enabled(): 1abcdefg
8 """Test that typo suggestions work when enabled"""
9 app = typer.Typer() 1abcdefg
11 @app.command() 1abcdefg
12 def create(): # pragma: no cover 1abcdefg
13 typer.echo("Creating...")
15 @app.command() 1abcdefg
16 def delete(): # pragma: no cover 1abcdefg
17 typer.echo("Deleting...")
19 result = runner.invoke(app, ["crate"]) 1abcdefg
20 assert result.exit_code != 0 1abcdefg
21 assert "No such command" in result.output 1abcdefg
22 assert "Did you mean 'create'?" in result.output 1abcdefg
25def test_typo_suggestion_multiple_matches(): 1abcdefg
26 """Test that multiple suggestions are shown when there are multiple close matches"""
27 app = typer.Typer() 1abcdefg
29 @app.command() 1abcdefg
30 def create(): # pragma: no cover 1abcdefg
31 typer.echo("Creating...")
33 @app.command() 1abcdefg
34 def createnew(): # pragma: no cover 1abcdefg
35 typer.echo("Creating new...")
37 result = runner.invoke(app, ["crate"]) 1abcdefg
38 assert result.exit_code != 0 1abcdefg
39 assert "No such command" in result.output 1abcdefg
40 assert "Did you mean" in result.output 1abcdefg
41 assert "create" in result.output and "createnew" in result.output 1abcdefg
44def test_typo_suggestion_no_matches(): 1abcdefg
45 """Test that no suggestions are shown when there are no close matches"""
46 app = typer.Typer() 1abcdefg
48 @app.command() 1abcdefg
49 def create(): # pragma: no cover 1abcdefg
50 typer.echo("Creating...")
52 @app.command() 1abcdefg
53 def delete(): # pragma: no cover 1abcdefg
54 typer.echo("Deleting...")
56 result = runner.invoke(app, ["xyz"]) 1abcdefg
57 assert result.exit_code != 0 1abcdefg
58 assert "No such command" in result.output 1abcdefg
59 assert "Did you mean" not in result.output 1abcdefg
62def test_typo_suggestion_exact_match_works(): 1abcdefg
63 """Test that exact matches still work normally"""
64 app = typer.Typer() 1abcdefg
66 @app.command() 1abcdefg
67 def create(): 1abcdefg
68 typer.echo("Creating...") 1abcdefg
70 @app.command() 1abcdefg
71 def delete(): 1abcdefg
72 typer.echo("Deleting...") 1abcdefg
74 result = runner.invoke(app, ["create"]) 1abcdefg
75 assert result.exit_code == 0 1abcdefg
76 assert "Creating..." in result.output 1abcdefg
78 result = runner.invoke(app, ["delete"]) 1abcdefg
79 assert result.exit_code == 0 1abcdefg
80 assert "Deleting..." in result.output 1abcdefg
83def test_typo_suggestion_disabled(): 1abcdefg
84 """Test that typo suggestions can be explicitly disabled"""
85 app = typer.Typer(suggest_commands=False) 1abcdefg
87 @app.command() 1abcdefg
88 def create(): # pragma: no cover 1abcdefg
89 typer.echo("Creating...")
91 @app.command() 1abcdefg
92 def delete(): # pragma: no cover 1abcdefg
93 typer.echo("Deleting...")
95 result = runner.invoke(app, ["crate"]) 1abcdefg
96 assert result.exit_code != 0 1abcdefg
97 assert "No such command" in result.output 1abcdefg
98 assert "Did you mean" not in result.output 1abcdefg