| 1 |
import subprocess |
| 2 |
import warnings |
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
import cyclopts.core |
| 8 |
from cyclopts import App |
| 9 |
from cyclopts.core import _log_framework_warning |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture(autouse=True) |
| 13 |
def clear_cache(): |
| 14 |
|
| 15 |
_log_framework_warning.cache_clear() |
| 16 |
|
| 17 |
yield |
| 18 |
|
| 19 |
|
| 20 |
_log_framework_warning.cache_clear() |
| 21 |
|
| 22 |
|
| 23 |
def test_app_iter(app): |
| 24 |
"""Like a dictionary, __iter__ of an App should yield keys (command names).""" |
| 25 |
|
| 26 |
@app.command |
| 27 |
def foo(): |
| 28 |
pass |
| 29 |
|
| 30 |
@app.command |
| 31 |
def bar(): |
| 32 |
pass |
| 33 |
|
| 34 |
actual = list(app) |
| 35 |
assert actual == ["--help", "-h", "--version", "foo", "bar"] |
| 36 |
|
| 37 |
|
| 38 |
def test_app_iter_with_meta(app): |
| 39 |
@app.command |
| 40 |
def foo(): |
| 41 |
pass |
| 42 |
|
| 43 |
@app.command |
| 44 |
def bar(): |
| 45 |
pass |
| 46 |
|
| 47 |
@app.meta.command |
| 48 |
def fizz(): |
| 49 |
pass |
| 50 |
|
| 51 |
actual = list(app) |
| 52 |
assert actual == ["--help", "-h", "--version", "foo", "bar"] |
| 53 |
|
| 54 |
actual = list(app.meta) |
| 55 |
assert actual == ["--help", "-h", "--version", "fizz", "foo", "bar"] |
| 56 |
|
| 57 |
|
| 58 |
def test_app_update(): |
| 59 |
app1 = App() |
| 60 |
app2 = App() |
| 61 |
|
| 62 |
@app1.command |
| 63 |
def foo(): |
| 64 |
pass |
| 65 |
|
| 66 |
@app2.command |
| 67 |
def bar(): |
| 68 |
pass |
| 69 |
|
| 70 |
app1.update(app2) |
| 71 |
|
| 72 |
assert list(app1) == ["--help", "-h", "--version", "foo", "bar"] |
| 73 |
|
| 74 |
|
| 75 |
def test_log_framework_warning_unknown(): |
| 76 |
|
| 77 |
with warnings.catch_warnings(): |
| 78 |
warnings.simplefilter("error") |
| 79 |
_log_framework_warning(cyclopts.core.TestFramework.UNKNOWN) |
| 80 |
|
| 81 |
|
| 82 |
def test_log_framework_warning_pytest(app): |
| 83 |
|
| 84 |
with pytest.warns(UserWarning) as warning_records: |
| 85 |
try: |
| 86 |
app(exit_on_error=False) |
| 87 |
except Exception: |
| 88 |
pass |
| 89 |
|
| 90 |
assert len(warning_records) == 1 |
| 91 |
warning_msg = str(warning_records[0].message) |
| 92 |
assert ( |
| 93 |
warning_msg |
| 94 |
== 'Cyclopts application invoked without tokens under unit-test framework "pytest". Did you mean "app([])"?' |
| 95 |
) |
| 96 |
|
| 97 |
|
| 98 |
@pytest.mark.skip(reason="code-coverage injects pytest into subprocess. Otherwise works.") |
| 99 |
def test_log_framework_warning_pytest_subprocess(): |
| 100 |
current_dir = Path(__file__).parent |
| 101 |
|
| 102 |
|
| 103 |
script_path = current_dir / "empty_app.py" |
| 104 |
|
| 105 |
|
| 106 |
result = subprocess.run( |
| 107 |
["python", str(script_path)], |
| 108 |
capture_output=True, |
| 109 |
text=True, |
| 110 |
) |
| 111 |
|
| 112 |
assert "Cyclopts application invoked without tokens under unit-test framework" not in result.stderr |