| 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 |
|
| 20 |
assert "--version" in app |
| 21 |
|
| 22 |
|
| 23 |
app.version_flags = ["--ver", "-V"] |
| 24 |
|
| 25 |
|
| 26 |
assert "--version" not in app, "Old version flag should be deleted" |
| 27 |
|
| 28 |
|
| 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 |
|
| 41 |
assert "--help" in app |
| 42 |
assert "-h" in app |
| 43 |
|
| 44 |
|
| 45 |
app.help_flags = ["--ayuda"] |
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 67 |
app.version_flags = ["--version", "-V"] |
| 68 |
|
| 69 |
|
| 70 |
assert "--version" in app, "Overlapping flag should remain" |
| 71 |
|
| 72 |
|
| 73 |
assert "-V" in app, "New flag should be created" |
| 74 |
|
| 75 |
|
| 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 |
|
| 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 |
|
| 92 |
app.help_flags = ["--help", "-?"] |
| 93 |
|
| 94 |
|
| 95 |
assert "--help" in app, "Overlapping flag should remain" |
| 96 |
|
| 97 |
|
| 98 |
assert "-?" in app, "New flag should be created" |
| 99 |
|
| 100 |
|
| 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 |
|
| 110 |
assert "--version" not in app |
| 111 |
|
| 112 |
|
| 113 |
app.version_flags = ["--version"] |
| 114 |
|
| 115 |
|
| 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 |
|
| 124 |
assert "--version" in app |
| 125 |
|
| 126 |
|
| 127 |
app.version_flags = [] |
| 128 |
|
| 129 |
|
| 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 |
|
| 138 |
assert "--help" not in app |
| 139 |
|
| 140 |
|
| 141 |
app.help_flags = ["--help"] |
| 142 |
|
| 143 |
|
| 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 |
|
| 152 |
assert "--help" in app |
| 153 |
assert "-h" in app |
| 154 |
|
| 155 |
|
| 156 |
app.help_flags = [] |
| 157 |
|
| 158 |
|
| 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 |
|
| 168 |
app.version_flags = ["--ver"] |
| 169 |
|
| 170 |
|
| 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 |
|
| 182 |
app.help_flags = ["--ayuda"] |
| 183 |
|
| 184 |
|
| 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 |
|
| 196 |
app.version_flags = ["--ver1"] |
| 197 |
assert "--ver1" in app |
| 198 |
assert "--version" not in app |
| 199 |
|
| 200 |
|
| 201 |
app.version_flags = ["--ver2"] |
| 202 |
assert "--ver2" in app |
| 203 |
assert "--ver1" not in app |
| 204 |
assert "--version" not in app |
| 205 |
|
| 206 |
|
| 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 |