Debian Codegraph @

tests/test_fuzzy_command_matching.py

raw symbols SCIP
1 """Tests for fuzzy command matching (backward compatibility feature).
2
3 Should probably be removed in v5.
4 """
5
6 import pytest
7
8 from cyclopts import App
9 from cyclopts.exceptions import UnknownCommandError
10
11
12 @pytest.mark.parametrize(
13 "func_name,expected_result,test_inputs",
14 [
15 (
16 "myCommand",
17 "myCommand executed",
18 ["my-command", "mycommand", "my_command", "MyCommand", "MYCOMMAND"],
19 ),
20 (
21 "my_command",
22 "my_command executed",
23 ["my-command", "mycommand", "my_command", "MY-COMMAND"],
24 ),
25 (
26 "HTTPServer",
27 "HTTPServer executed",
28 ["http-server", "httpserver", "http_server", "HTTPSERVER"],
29 ),
30 ],
31 )
32 def test_fuzzy_command_matching_basic(app, func_name, expected_result, test_inputs):
33 """Fuzzy matching allows commands to be invoked with various styles."""
34
35 def command_func():
36 return expected_result
37
38 command_func.__name__ = func_name
39 app.command(command_func)
40
41 for input_cmd in test_inputs:
42 assert app([input_cmd]) == expected_result
43
44
45 def test_fuzzy_command_matching_no_false_positives(app):
46 """Fuzzy matching doesn't match unrelated commands."""
47
48 @app.command
49 def my_command():
50 return "my_command"
51
52 @app.command
53 def other_command():
54 return "other_command"
55
56 # These should NOT match
57 with pytest.raises(UnknownCommandError, match="myother"):
58 app.parse_args(["myother"], exit_on_error=False)
59
60 with pytest.raises(UnknownCommandError, match="commandmy"):
61 app.parse_args(["commandmy"], exit_on_error=False)
62
63
64 def test_fuzzy_command_matching_exact_takes_precedence(app):
65 """Exact matches are preferred over fuzzy matches."""
66
67 @app.command(name="my-command")
68 def cmd1():
69 return "cmd1"
70
71 @app.command(name="mycommand")
72 def cmd2():
73 return "cmd2"
74
75 # Exact match wins
76 assert app(["my-command"]) == "cmd1"
77 assert app(["mycommand"]) == "cmd2"
78
79
80 def test_fuzzy_command_matching_ambiguous_error(app):
81 """Ambiguous fuzzy matches raise clear errors."""
82
83 @app.command(name="my-command")
84 def cmd1():
85 return "cmd1"
86
87 @app.command(name="mycommand")
88 def cmd2():
89 return "cmd2"
90
91 # Both normalize to 'mycommand', so this is ambiguous
92 with pytest.raises(ValueError, match="Ambiguous command 'my_command'.*my-command.*mycommand"):
93 app.parse_args(["my_command"], exit_on_error=False)
94
95
96 @pytest.mark.parametrize(
97 "input_cmd,user_id,expected",
98 [
99 ("get-user-data", "123", "User 123"),
100 ("getuserdata", "456", "User 456"),
101 ("get_user_data", "789", "User 789"),
102 ("GetUserData", "999", "User 999"),
103 ],
104 )
105 def test_fuzzy_command_matching_with_arguments(app, input_cmd, user_id, expected):
106 """Fuzzy matching works with commands that have arguments."""
107
108 @app.command
109 def getUserData(user_id: int): # noqa: N802
110 return f"User {user_id}"
111
112 assert app([input_cmd, user_id]) == expected
113
114
115 @pytest.mark.parametrize(
116 "command_name,input_variants,expected",
117 [
118 (
119 "buildProject",
120 ["buildproject", "build_project", "build-project"],
121 "building",
122 ),
123 (
124 "testProject",
125 ["testproject", "testProject", "test-project"],
126 "testing",
127 ),
128 (
129 "deployProduction",
130 ["deployproduction", "deploy_production", "deploy-production"],
131 "deploying",
132 ),
133 ],
134 )
135 def test_fuzzy_command_matching_multiple_commands(app, command_name, input_variants, expected):
136 """Fuzzy matching works with multiple commands."""
137
138 def command_func():
139 return expected
140
141 command_func.__name__ = command_name
142 app.command(command_func)
143
144 for input_cmd in input_variants:
145 assert app([input_cmd]) == expected
146
147
148 @pytest.mark.parametrize(
149 "command_name,input_variants",
150 [
151 ("cmd1", ["cmd1", "CMD1", "Cmd1"]),
152 ("cmd2", ["cmd2", "CMD2", "Cmd2"]),
153 ],
154 )
155 def test_fuzzy_command_matching_preserves_cmd_digit_behavior(app, command_name, input_variants):
156 """Fuzzy matching doesn't break cmd1, cmd2, etc (no lowercase-digit split)."""
157
158 def command_func():
159 return command_name
160
161 command_func.__name__ = command_name
162 app.command(command_func)
163
164 for input_cmd in input_variants:
165 assert app([input_cmd]) == command_name
166
167
168 @pytest.mark.parametrize(
169 "subcommand,input_variants,expected",
170 [
171 ("createTable", ["createtable", "create_table", "create-table"], "table created"),
172 ("dropTable", ["droptable", "drop_table", "drop-table"], "table dropped"),
173 ],
174 )
175 def test_fuzzy_command_matching_nested_commands(app, subcommand, input_variants, expected):
176 """Fuzzy matching works with nested commands."""
177 sub_app = App()
178
179 def command_func():
180 return expected
181
182 command_func.__name__ = subcommand
183 sub_app.command(command_func)
184
185 app.command(sub_app, name="database")
186
187 for input_cmd in input_variants:
188 assert app(["database", input_cmd]) == expected
189
190
191 def test_fuzzy_matching_excludes_flags_issue_718(app):
192 """Fuzzy matching should NOT match bare words to flags like --version or --help.
193
194 Regression test for issue #718: Running 'app version' (without --) should NOT
195 match the '--version' flag via fuzzy matching.
196 """
197
198 @app.command
199 def hello():
200 return "hello"
201
202 # "version" should NOT match "--version" via fuzzy matching
203 with pytest.raises(UnknownCommandError, match="version"):
204 app.parse_args(["version"], exit_on_error=False)
205
206 # "help" should NOT match "--help" via fuzzy matching
207 with pytest.raises(UnknownCommandError, match="help"):
208 app.parse_args(["help"], exit_on_error=False)
209
210 # But actual flags should still work
211 # Note: --help and --version cause the app to exit, so we check parse_commands instead
212 command_chain, apps, unused = app.parse_commands(["--version"])
213 assert "--version" in command_chain
214
215 command_chain, apps, unused = app.parse_commands(["--help"])
216 assert "--help" in command_chain
217
218
219 def test_fuzzy_matching_excludes_short_flags(app):
220 """Fuzzy matching should also exclude short flags like -v and -h."""
221
222 @app.command
223 def hello():
224 return "hello"
225
226 # "v" should NOT match "-v" via fuzzy matching
227 with pytest.raises(UnknownCommandError, match="v"):
228 app.parse_args(["v"], exit_on_error=False)
229
230 # "h" should NOT match "-h" via fuzzy matching
231 with pytest.raises(UnknownCommandError, match="h"):
232 app.parse_args(["h"], exit_on_error=False)