Debian Codegraph @

tests/test_issue_643_flag_change_bug.py

raw symbols SCIP
1 """Test for issue #643: version_flags and help_flags setter bug.
2
3 When changing version_flags or help_flags, the _delete_commands() call
4 happens AFTER setting the new value, so it tries to delete commands matching
5 the NEW flags instead of the OLD flags. This leaves orphaned commands from
6 the old flags in the app.
7 """
8
9 from cyclopts import App
10
11
12 def test_version_flags_change_deletes_old_commands(console):
13 """Test that changing version_flags properly deletes old version commands.
14
15 Bug: Currently fails because old commands remain in app.
16 """
17 app = App(name="test", version="1.0.0", result_action="return_value")
18
19 # Initial state: default version flags are ["--version"]
20 assert "--version" in app
21
22 # Change version_flags to a different set
23 app.version_flags = ["--ver", "-V"]
24
25 # The OLD commands should be deleted
26 assert "--version" not in app, "Old version flag should be deleted"
27
28 # The NEW commands should exist
29 assert "--ver" in app, "New version flag should be created"
30 assert "-V" in app, "New version flag should be created"
31
32
33 def test_help_flags_change_deletes_old_commands(console):
34 """Test that changing help_flags properly deletes old help commands.
35
36 Bug: Currently fails because old commands remain in app.
37 """
38 app = App(name="test", result_action="return_value")
39
40 # Initial state: default help flags are ["--help", "-h"]
41 assert "--help" in app
42 assert "-h" in app
43
44 # Change help_flags to a different set
45 app.help_flags = ["--ayuda"] # Spanish for "help"
46
47 # The OLD commands should be deleted
48 assert "--help" not in app, "Old help flag should be deleted"
49 assert "-h" not in app, "Old help flag should be deleted"
50
51 # The NEW commands should exist
52 assert "--ayuda" in app, "New help flag should be created"
53
54
55 def test_version_flags_change_overlapping_flags(console):
56 """Test changing version_flags when there's overlap between old and new."""
57 app = App(name="test", version="1.0.0", result_action="return_value")
58
59 # Set initial custom version flags
60 app.version_flags = ["--version", "-v", "--ver"]
61
62 assert "--version" in app
63 assert "-v" in app
64 assert "--ver" in app
65
66 # Change to a set that overlaps with one old flag
67 app.version_flags = ["--version", "-V"] # Keep --version, change -v to -V, remove --ver
68
69 # Should keep the overlapping flag
70 assert "--version" in app, "Overlapping flag should remain"
71
72 # Should add new flag
73 assert "-V" in app, "New flag should be created"
74
75 # Should remove old flags that aren't in new set
76 assert "-v" not in app, "Old flag not in new set should be deleted"
77 assert "--ver" not in app, "Old flag not in new set should be deleted"
78
79
80 def test_help_flags_change_overlapping_flags(console):
81 """Test changing help_flags when there's overlap between old and new."""
82 app = App(name="test", result_action="return_value")
83
84 # Set initial custom help flags
85 app.help_flags = ["--help", "-h", "--ayuda"]
86
87 assert "--help" in app
88 assert "-h" in app
89 assert "--ayuda" in app
90
91 # Change to a set that overlaps with one old flag
92 app.help_flags = ["--help", "-?"] # Keep --help, change -h to -?, remove --ayuda
93
94 # Should keep the overlapping flag
95 assert "--help" in app, "Overlapping flag should remain"
96
97 # Should add new flag
98 assert "-?" in app, "New flag should be created"
99
100 # Should remove old flags that aren't in new set
101 assert "-h" not in app, "Old flag not in new set should be deleted"
102 assert "--ayuda" not in app, "Old flag not in new set should be deleted"
103
104
105 def test_version_flags_empty_to_nonempty(console):
106 """Test changing from empty version_flags to non-empty."""
107 app = App(name="test", version="1.0.0", version_flags=[], result_action="return_value")
108
109 # Initially no version flags
110 assert "--version" not in app
111
112 # Set version flags
113 app.version_flags = ["--version"]
114
115 # Should now have version flag
116 assert "--version" in app
117
118
119 def test_version_flags_nonempty_to_empty(console):
120 """Test changing from non-empty version_flags to empty."""
121 app = App(name="test", version="1.0.0", result_action="return_value")
122
123 # Initially has default version flags
124 assert "--version" in app
125
126 # Remove all version flags
127 app.version_flags = []
128
129 # Should no longer have version flag
130 assert "--version" not in app
131
132
133 def test_help_flags_empty_to_nonempty(console):
134 """Test changing from empty help_flags to non-empty."""
135 app = App(name="test", help_flags=[], result_action="return_value")
136
137 # Initially no help flags
138 assert "--help" not in app
139
140 # Set help flags
141 app.help_flags = ["--help"]
142
143 # Should now have help flag
144 assert "--help" in app
145
146
147 def test_help_flags_nonempty_to_empty(console):
148 """Test changing from non-empty help_flags to empty."""
149 app = App(name="test", result_action="return_value")
150
151 # Initially has default help flags
152 assert "--help" in app
153 assert "-h" in app
154
155 # Remove all help flags
156 app.help_flags = []
157
158 # Should no longer have help flags
159 assert "--help" not in app
160 assert "-h" not in app
161
162
163 def test_version_flags_functional_after_change(tmp_path, console):
164 """Test that version command works correctly after changing flags."""
165 app = App(name="test", version="1.0.0", result_action="return_value")
166
167 # Change version flags
168 app.version_flags = ["--ver"]
169
170 # Test that new flag works
171 with console.capture() as capture:
172 app(["--ver"], console=console)
173
174 assert "1.0.0" in capture.get()
175
176
177 def test_help_flags_functional_after_change(tmp_path, console):
178 """Test that help command works correctly after changing flags."""
179 app = App(name="test", help="Test app", result_action="return_value")
180
181 # Change help flags
182 app.help_flags = ["--ayuda"]
183
184 # Test that new flag works
185 with console.capture() as capture:
186 app(["--ayuda"], console=console)
187
188 assert "Test app" in capture.get()
189
190
191 def test_multiple_flag_changes(console):
192 """Test changing flags multiple times in sequence."""
193 app = App(name="test", version="1.0.0", result_action="return_value")
194
195 # First change
196 app.version_flags = ["--ver1"]
197 assert "--ver1" in app
198 assert "--version" not in app
199
200 # Second change
201 app.version_flags = ["--ver2"]
202 assert "--ver2" in app
203 assert "--ver1" not in app
204 assert "--version" not in app
205
206 # Third change
207 app.version_flags = ["--ver3"]
208 assert "--ver3" in app
209 assert "--ver2" not in app
210 assert "--ver1" not in app
211 assert "--version" not in app