1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
import inspect
import sys
from pathlib import Path
import pytest
from rich.console import Console
import cyclopts
from cyclopts import Group, Parameter
def pytest_ignore_collect(collection_path):
for minor in range(8, 20):
if sys.version_info < (3, minor) and collection_path.stem.startswith(f"test_py3{minor}_"):
return True
@pytest.fixture(autouse=True)
def chdir_to_tmp_path(tmp_path, monkeypatch):
"""Automatically change current directory to tmp_path"""
monkeypatch.chdir(tmp_path)
@pytest.fixture
def app():
return cyclopts.App()
@pytest.fixture
def console():
return Console(width=70, force_terminal=True, highlight=False, color_system=None, legacy_windows=False)
@pytest.fixture
def default_function_groups():
return (Parameter(), Group("Arguments"), Group("Parameters"))
@pytest.fixture
def assert_parse_args(app):
def inner(f, cmd: str, *args, **kwargs):
signature = inspect.signature(f)
expected_bind = signature.bind(*args, **kwargs)
actual_command, actual_bind, _ = app.parse_args(cmd, print_error=False, exit_on_error=False)
assert actual_command == f
assert actual_bind == expected_bind
return inner
@pytest.fixture
def assert_parse_args_config(app):
def inner(config: dict, f, cmd: str, *args, **kwargs):
signature = inspect.signature(f)
expected_bind = signature.bind(*args, **kwargs)
actual_command, actual_bind, _ = app.parse_args(cmd, print_error=False, exit_on_error=False, **config)
assert actual_command == f
assert actual_bind == expected_bind
return inner
@pytest.fixture
def assert_parse_args_partial(app):
def inner(f, cmd: str, *args, **kwargs):
signature = inspect.signature(f)
expected_bind = signature.bind_partial(*args, **kwargs)
actual_command, actual_bind, _ = app.parse_args(cmd, print_error=False, exit_on_error=False)
assert actual_command == f
assert actual_bind == expected_bind
return inner
@pytest.fixture
def convert():
"""Function that performs a conversion for a given type/cmd pair.
Goes through the whole app stack.
"""
def inner(type_, cmd):
app = cyclopts.App()
if isinstance(cmd, Path):
cmd = cmd.as_posix()
@app.default
def target(arg1: type_): # pyright: ignore
return arg1
return app(cmd, exit_on_error=False)
return inner
|