| 1 |
from textwrap import dedent |
| 2 |
from typing import Annotated |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from cyclopts import App, Parameter |
| 7 |
from cyclopts.config import Toml |
| 8 |
|
| 9 |
|
| 10 |
def test_config_toml(tmp_path): |
| 11 |
fn = tmp_path / "test.toml" |
| 12 |
fn.write_text( |
| 13 |
dedent( |
| 14 |
"""\ |
| 15 |
[foo] |
| 16 |
key1 = "foo1" |
| 17 |
key2 = "foo2" |
| 18 |
|
| 19 |
[foo.function1] |
| 20 |
key1 = "bar1" |
| 21 |
key2 = "bar2" |
| 22 |
""" |
| 23 |
) |
| 24 |
) |
| 25 |
config = Toml(fn) |
| 26 |
assert config.config == { |
| 27 |
"foo": { |
| 28 |
"key1": "foo1", |
| 29 |
"key2": "foo2", |
| 30 |
"function1": { |
| 31 |
"key1": "bar1", |
| 32 |
"key2": "bar2", |
| 33 |
}, |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture |
| 39 |
def config_path(tmp_path): |
| 40 |
"""Path to JSON configuration file in tmp_path""" |
| 41 |
return tmp_path / "config.toml" |
| 42 |
|
| 43 |
|
| 44 |
def test_duplicate_config_toml_with_meta(config_path): |
| 45 |
config_path.write_text( |
| 46 |
dedent( |
| 47 |
"""\ |
| 48 |
[this-test] |
| 49 |
name = "Alice" |
| 50 |
""" |
| 51 |
) |
| 52 |
) |
| 53 |
app = App( |
| 54 |
config=( |
| 55 |
Toml("config.toml", root_keys=("this-test",)), |
| 56 |
Toml("config.toml", root_keys=("this-test",)), |
| 57 |
), |
| 58 |
result_action="return_value", |
| 59 |
) |
| 60 |
|
| 61 |
@app.meta.default |
| 62 |
def meta( |
| 63 |
*tokens: Annotated[str, Parameter(show=False, allow_leading_hyphen=True)], |
| 64 |
): |
| 65 |
return app(tokens, exit_on_error=False) |
| 66 |
|
| 67 |
@app.default |
| 68 |
def main(name: str): |
| 69 |
return name |
| 70 |
|
| 71 |
assert app.meta([], exit_on_error=False) == "Alice" |