| 1 |
import asyncio |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
|
| 5 |
def test_version_print_console_from_init(app, console): |
| 6 |
app.console = console |
| 7 |
|
| 8 |
with console.capture() as capture: |
| 9 |
app.version_print() |
| 10 |
|
| 11 |
assert "0.0.0\n" == capture.get() |
| 12 |
|
| 13 |
|
| 14 |
def test_version_print_console_from_method(app, console): |
| 15 |
with console.capture() as capture: |
| 16 |
app.version_print(console) |
| 17 |
|
| 18 |
assert "0.0.0\n" == capture.get() |
| 19 |
|
| 20 |
|
| 21 |
def test_version_print_console_none(app, console): |
| 22 |
app.version = None |
| 23 |
with console.capture() as capture: |
| 24 |
app.version_print(console) |
| 25 |
|
| 26 |
assert "0.0.0\n" == capture.get() |
| 27 |
|
| 28 |
|
| 29 |
def test_version_print_custom_string(app, console): |
| 30 |
"""The asterisks also test to make sure the proper help_format is being used.""" |
| 31 |
app.version = "**foo**" |
| 32 |
|
| 33 |
with console.capture() as capture: |
| 34 |
app.version_print(console) |
| 35 |
|
| 36 |
assert "foo\n" == capture.get() |
| 37 |
|
| 38 |
|
| 39 |
def test_version_print_custom_callable(app, console): |
| 40 |
def my_version(): |
| 41 |
return "**foo**" |
| 42 |
|
| 43 |
app.version = my_version |
| 44 |
|
| 45 |
with console.capture() as capture: |
| 46 |
app.version_print(console) |
| 47 |
|
| 48 |
assert "foo\n" == capture.get() |
| 49 |
|
| 50 |
|
| 51 |
def test_version_print_help_format_fallback(app, console): |
| 52 |
"""If no explicit version_format is provided, we should fallback to help_format.""" |
| 53 |
app.help_format = "rich" |
| 54 |
app.version = "[red]foo[/red]" |
| 55 |
|
| 56 |
with console.capture() as capture: |
| 57 |
app.version_print(console) |
| 58 |
|
| 59 |
assert "foo\n" == capture.get() |
| 60 |
|
| 61 |
|
| 62 |
def test_version_print_help_format_override(app, console): |
| 63 |
"""If version_format is provided, help_format should not be used for version.""" |
| 64 |
app.help_format = "plain" |
| 65 |
app.version_format = "rich" |
| 66 |
app.version = "[red]foo[/red]" |
| 67 |
|
| 68 |
with console.capture() as capture: |
| 69 |
app.version_print(console) |
| 70 |
|
| 71 |
assert "foo\n" == capture.get() |
| 72 |
|
| 73 |
|
| 74 |
def test_version_print_custom_async_callable(app, console): |
| 75 |
"""Test that async callables work for version via command-line.""" |
| 76 |
|
| 77 |
async def my_async_version(): |
| 78 |
return "**async-foo**" |
| 79 |
|
| 80 |
app.version = my_async_version |
| 81 |
|
| 82 |
|
| 83 |
with console.capture() as capture: |
| 84 |
app(["--version"], console=console) |
| 85 |
|
| 86 |
assert "async-foo\n" == capture.get() |
| 87 |
|
| 88 |
|
| 89 |
def test_version_print_async_in_async_context(app, console): |
| 90 |
"""Test that async version callables work when called from within an async context.""" |
| 91 |
|
| 92 |
async def my_async_version(): |
| 93 |
|
| 94 |
try: |
| 95 |
asyncio.get_running_loop() |
| 96 |
except RuntimeError: |
| 97 |
return "ERROR: Not in async context" |
| 98 |
return "**async-version-from-async-context**" |
| 99 |
|
| 100 |
app.version = my_async_version |
| 101 |
|
| 102 |
async def run_test(): |
| 103 |
with console.capture() as capture: |
| 104 |
|
| 105 |
await app.run_async(["--version"], console=console) |
| 106 |
return capture.get() |
| 107 |
|
| 108 |
result = asyncio.run(run_test()) |
| 109 |
assert "async-version-from-async-context\n" == result |
| 110 |
|
| 111 |
|
| 112 |
def test_version_print_sync_callable_end_to_end(app, console): |
| 113 |
"""Test that vanilla sync version callables works end-to-end.""" |
| 114 |
|
| 115 |
def my_sync_version(): |
| 116 |
return "**sync-foo**" |
| 117 |
|
| 118 |
app.version = my_sync_version |
| 119 |
|
| 120 |
with console.capture() as capture: |
| 121 |
app(["--version"], console=console) |
| 122 |
|
| 123 |
assert "sync-foo\n" == capture.get() |
| 124 |
|
| 125 |
|
| 126 |
def test_help_and_version_flags_together(app, console): |
| 127 |
"""Test that help flag takes priority when both --help and --version are provided. |
| 128 |
|
| 129 |
Regression test for bug where having both flags caused NameError on Console forward reference. |
| 130 |
""" |
| 131 |
|
| 132 |
@app.command |
| 133 |
def files( |
| 134 |
input_file: Path, |
| 135 |
output_file: Path | None = None, |
| 136 |
): |
| 137 |
"""Work with files.""" |
| 138 |
pass |
| 139 |
|
| 140 |
with console.capture() as capture: |
| 141 |
app(["files", "-h", "--version"], console=console) |
| 142 |
|
| 143 |
output = capture.get() |
| 144 |
assert "Work with files" in output |
| 145 |
assert "INPUT-FILE" in output |
| 146 |
|
| 147 |
|
| 148 |
def test_version_and_help_flags_together(console): |
| 149 |
"""Test that help flag takes priority when --version --help are provided (version first). |
| 150 |
|
| 151 |
Regression test for issue #645 where having multiple --version/--help flags caused |
| 152 |
NameError: name 'Console' is not defined. |
| 153 |
""" |
| 154 |
import pytest |
| 155 |
|
| 156 |
from cyclopts import App |
| 157 |
|
| 158 |
|
| 159 |
app = App(name="myapp", version="1.0.0") |
| 160 |
|
| 161 |
@app.command |
| 162 |
def files( |
| 163 |
input_file: Path, |
| 164 |
output_file: Path | None = None, |
| 165 |
): |
| 166 |
"""Work with files.""" |
| 167 |
pass |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
with console.capture() as capture: |
| 172 |
with pytest.raises(SystemExit) as exc_info: |
| 173 |
app(["files", "--version", "--help", "--help"], console=console) |
| 174 |
assert exc_info.value.code == 0 |
| 175 |
|
| 176 |
output = capture.get() |
| 177 |
assert "Work with files" in output |
| 178 |
assert "INPUT-FILE" in output |
| 179 |
|
| 180 |
|
| 181 |
with console.capture() as capture: |
| 182 |
with pytest.raises(SystemExit) as exc_info: |
| 183 |
app(["files", "--help", "--version", "--version"], console=console) |
| 184 |
assert exc_info.value.code == 0 |
| 185 |
|
| 186 |
output = capture.get() |
| 187 |
assert "Work with files" in output |
| 188 |
assert "INPUT-FILE" in output |
| 189 |
|
| 190 |
|
| 191 |
def test_subcommand_version(console): |
| 192 |
"""Test that subcommands display their own version, not the parent's version. |
| 193 |
|
| 194 |
Regression test for issue #627 where subcommand --version showed parent version. |
| 195 |
""" |
| 196 |
from cyclopts import App |
| 197 |
|
| 198 |
|
| 199 |
root_app = App(version_flags=[], result_action="return_value") |
| 200 |
|
| 201 |
|
| 202 |
subcommand = App( |
| 203 |
name="subcommand", |
| 204 |
version="1.2.3", |
| 205 |
version_flags=["--version"], |
| 206 |
) |
| 207 |
root_app.command(subcommand) |
| 208 |
|
| 209 |
@subcommand.default |
| 210 |
def subcommand_default(): |
| 211 |
"""Subcommand default function.""" |
| 212 |
return "executed" |
| 213 |
|
| 214 |
|
| 215 |
with console.capture() as capture: |
| 216 |
root_app(["subcommand", "--version"], console=console) |
| 217 |
|
| 218 |
assert "1.2.3\n" == capture.get() |
| 219 |
|
| 220 |
|
| 221 |
def test_subcommand_version_with_meta_app(console): |
| 222 |
"""Test that subcommand version works correctly with meta apps. |
| 223 |
|
| 224 |
Regression test for issue #627 with meta app wrapper. |
| 225 |
""" |
| 226 |
from typing import Annotated |
| 227 |
|
| 228 |
from cyclopts import App, Parameter |
| 229 |
|
| 230 |
|
| 231 |
root_app = App(version_flags=[], result_action="return_value") |
| 232 |
|
| 233 |
|
| 234 |
subcommand = App( |
| 235 |
name="subcommand", |
| 236 |
version="1.2.3", |
| 237 |
version_flags=["-v", "--version"], |
| 238 |
) |
| 239 |
root_app.command(subcommand) |
| 240 |
|
| 241 |
@subcommand.default |
| 242 |
def subcommand_default(): |
| 243 |
"""Subcommand default function.""" |
| 244 |
return "executed" |
| 245 |
|
| 246 |
@root_app.meta.default |
| 247 |
def meta( |
| 248 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 249 |
verbose: Annotated[bool, Parameter(alias="-V")] = False, |
| 250 |
): |
| 251 |
return root_app(tokens) |
| 252 |
|
| 253 |
|
| 254 |
with console.capture() as capture: |
| 255 |
root_app.meta(["subcommand", "--version"], console=console) |
| 256 |
|
| 257 |
assert "1.2.3\n" == capture.get() |
| 258 |
|
| 259 |
|
| 260 |
with console.capture() as capture: |
| 261 |
root_app.meta(["-V", "subcommand", "-v"], console=console) |
| 262 |
|
| 263 |
assert "1.2.3\n" == capture.get() |
| 264 |
|
| 265 |
|
| 266 |
def test_subcommand_inherits_parent_version(console): |
| 267 |
"""Test that subcommand inherits parent's version when not explicitly set. |
| 268 |
|
| 269 |
When a subcommand doesn't have an explicit version, it should inherit |
| 270 |
the parent's version if the parent has one set. |
| 271 |
""" |
| 272 |
from cyclopts import App |
| 273 |
|
| 274 |
|
| 275 |
root_app = App(version="2.0.0", result_action="return_value") |
| 276 |
|
| 277 |
|
| 278 |
subcommand = App(name="subcommand") |
| 279 |
root_app.command(subcommand) |
| 280 |
|
| 281 |
@subcommand.default |
| 282 |
def subcommand_default(): |
| 283 |
"""Subcommand default function.""" |
| 284 |
return "executed" |
| 285 |
|
| 286 |
|
| 287 |
with console.capture() as capture: |
| 288 |
root_app(["subcommand", "--version"], console=console) |
| 289 |
|
| 290 |
assert "2.0.0\n" == capture.get() |
| 291 |
|
| 292 |
|
| 293 |
def test_subcommand_explicit_version_overrides_parent(console): |
| 294 |
"""Test that subcommand's explicit version overrides parent's version.""" |
| 295 |
from cyclopts import App |
| 296 |
|
| 297 |
|
| 298 |
root_app = App(version="2.0.0", result_action="return_value") |
| 299 |
|
| 300 |
|
| 301 |
subcommand = App(name="subcommand", version="3.0.0") |
| 302 |
root_app.command(subcommand) |
| 303 |
|
| 304 |
@subcommand.default |
| 305 |
def subcommand_default(): |
| 306 |
"""Subcommand default function.""" |
| 307 |
return "executed" |
| 308 |
|
| 309 |
|
| 310 |
with console.capture() as capture: |
| 311 |
root_app(["subcommand", "--version"], console=console) |
| 312 |
|
| 313 |
assert "3.0.0\n" == capture.get() |
| 314 |
|
| 315 |
|
| 316 |
def test_function_command_inherits_parent_version(console): |
| 317 |
"""Test that function commands inherit parent's version.""" |
| 318 |
from cyclopts import App |
| 319 |
|
| 320 |
root_app = App(version="4.0.0", result_action="return_value") |
| 321 |
|
| 322 |
@root_app.command |
| 323 |
def my_command(): |
| 324 |
"""A command function.""" |
| 325 |
return "executed" |
| 326 |
|
| 327 |
|
| 328 |
with console.capture() as capture: |
| 329 |
root_app(["my-command", "--version"], console=console) |
| 330 |
|
| 331 |
assert "4.0.0\n" == capture.get() |