Coverage for tests/test_suggest_commands.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-19 20:29 +0000

1import typer 1abcdefghi

2from typer.testing import CliRunner 1abcdefghi

3 

4runner = CliRunner() 1abcdefghi

5 

6 

7def test_typo_suggestion_enabled(): 1abcdefghi

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

9 app = typer.Typer() 1abcdefghi

10 

11 @app.command() 1abcdefghi

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

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

14 

15 @app.command() 1abcdefghi

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

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

18 

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

20 assert result.exit_code != 0 1abcdefghi

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

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

23 

24 

25def test_typo_suggestion_multiple_matches(): 1abcdefghi

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

27 app = typer.Typer() 1abcdefghi

28 

29 @app.command() 1abcdefghi

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

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

32 

33 @app.command() 1abcdefghi

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

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

36 

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

38 assert result.exit_code != 0 1abcdefghi

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

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

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

42 

43 

44def test_typo_suggestion_no_matches(): 1abcdefghi

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

46 app = typer.Typer() 1abcdefghi

47 

48 @app.command() 1abcdefghi

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

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

51 

52 @app.command() 1abcdefghi

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

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

55 

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

57 assert result.exit_code != 0 1abcdefghi

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

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

60 

61 

62def test_typo_suggestion_exact_match_works(): 1abcdefghi

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

64 app = typer.Typer() 1abcdefghi

65 

66 @app.command() 1abcdefghi

67 def create(): 1abcdefghi

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

69 

70 @app.command() 1abcdefghi

71 def delete(): 1abcdefghi

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

73 

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

75 assert result.exit_code == 0 1abcdefghi

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

77 

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

79 assert result.exit_code == 0 1abcdefghi

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

81 

82 

83def test_typo_suggestion_disabled(): 1abcdefghi

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

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

86 

87 @app.command() 1abcdefghi

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

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

90 

91 @app.command() 1abcdefghi

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

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

94 

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

96 assert result.exit_code != 0 1abcdefghi

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

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