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