Coverage for tests / test_suggest_commands.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-11-18 11:12 +0000

1import typer 1abcdefghij

2from typer.testing import CliRunner 1abcdefghij

3 

4runner = CliRunner() 1abcdefghij

5 

6 

7def test_typo_suggestion_enabled(): 1abcdefghij

8 """Test that typo suggestions work when enabled""" 

9 app = typer.Typer() 1abcdefghij

10 

11 @app.command() 1abcdefghij

12 def create(): # pragma: no cover 1abcdefghij

13 typer.echo("Creating...") 

14 

15 @app.command() 1abcdefghij

16 def delete(): # pragma: no cover 1abcdefghij

17 typer.echo("Deleting...") 

18 

19 result = runner.invoke(app, ["crate"]) 1abcdefghij

20 assert result.exit_code != 0 1abcdefghij

21 assert "No such command" in result.output 1abcdefghij

22 assert "Did you mean 'create'?" in result.output 1abcdefghij

23 

24 

25def test_typo_suggestion_multiple_matches(): 1abcdefghij

26 """Test that multiple suggestions are shown when there are multiple close matches""" 

27 app = typer.Typer() 1abcdefghij

28 

29 @app.command() 1abcdefghij

30 def create(): # pragma: no cover 1abcdefghij

31 typer.echo("Creating...") 

32 

33 @app.command() 1abcdefghij

34 def createnew(): # pragma: no cover 1abcdefghij

35 typer.echo("Creating new...") 

36 

37 result = runner.invoke(app, ["crate"]) 1abcdefghij

38 assert result.exit_code != 0 1abcdefghij

39 assert "No such command" in result.output 1abcdefghij

40 assert "Did you mean" in result.output 1abcdefghij

41 assert "create" in result.output and "createnew" in result.output 1abcdefghij

42 

43 

44def test_typo_suggestion_no_matches(): 1abcdefghij

45 """Test that no suggestions are shown when there are no close matches""" 

46 app = typer.Typer() 1abcdefghij

47 

48 @app.command() 1abcdefghij

49 def create(): # pragma: no cover 1abcdefghij

50 typer.echo("Creating...") 

51 

52 @app.command() 1abcdefghij

53 def delete(): # pragma: no cover 1abcdefghij

54 typer.echo("Deleting...") 

55 

56 result = runner.invoke(app, ["xyz"]) 1abcdefghij

57 assert result.exit_code != 0 1abcdefghij

58 assert "No such command" in result.output 1abcdefghij

59 assert "Did you mean" not in result.output 1abcdefghij

60 

61 

62def test_typo_suggestion_exact_match_works(): 1abcdefghij

63 """Test that exact matches still work normally""" 

64 app = typer.Typer() 1abcdefghij

65 

66 @app.command() 1abcdefghij

67 def create(): 1abcdefghij

68 typer.echo("Creating...") 1abcdefghij

69 

70 @app.command() 1abcdefghij

71 def delete(): 1abcdefghij

72 typer.echo("Deleting...") 1abcdefghij

73 

74 result = runner.invoke(app, ["create"]) 1abcdefghij

75 assert result.exit_code == 0 1abcdefghij

76 assert "Creating..." in result.output 1abcdefghij

77 

78 result = runner.invoke(app, ["delete"]) 1abcdefghij

79 assert result.exit_code == 0 1abcdefghij

80 assert "Deleting..." in result.output 1abcdefghij

81 

82 

83def test_typo_suggestion_disabled(): 1abcdefghij

84 """Test that typo suggestions can be explicitly disabled""" 

85 app = typer.Typer(suggest_commands=False) 1abcdefghij

86 

87 @app.command() 1abcdefghij

88 def create(): # pragma: no cover 1abcdefghij

89 typer.echo("Creating...") 

90 

91 @app.command() 1abcdefghij

92 def delete(): # pragma: no cover 1abcdefghij

93 typer.echo("Deleting...") 

94 

95 result = runner.invoke(app, ["crate"]) 1abcdefghij

96 assert result.exit_code != 0 1abcdefghij

97 assert "No such command" in result.output 1abcdefghij

98 assert "Did you mean" not in result.output 1abcdefghij