| 1 | from typing import Annotated |
| 2 | |
| 3 | import pytest |
| 4 | |
| 5 | from cyclopts import MissingArgumentError, Parameter |
| 6 | |
| 7 | |
| 8 | def test_env_var_unset_use_signature_default(app, assert_parse_args, monkeypatch): |
| 9 | @app.default |
| 10 | def foo(bar: Annotated[int, Parameter(env_var="BAR")] = 123): |
| 11 | pass |
| 12 | |
| 13 | monkeypatch.delenv("BAR", raising=False) |
| 14 | assert_parse_args(foo, "") |
| 15 | |
| 16 | |
| 17 | def test_env_var_set_use_env_var(app, assert_parse_args, monkeypatch): |
| 18 | @app.default |
| 19 | def foo(bar: Annotated[int, Parameter(env_var="BAR")] = 123): |
| 20 | pass |
| 21 | |
| 22 | monkeypatch.setenv("BAR", "456") |
| 23 | assert_parse_args(foo, "", 456) |
| 24 | |
| 25 | |
| 26 | def test_env_var_set_use_env_var_no_default(app, assert_parse_args, monkeypatch): |
| 27 | @app.default |
| 28 | def foo(bar: Annotated[int, Parameter(env_var="BAR")]): |
| 29 | pass |
| 30 | |
| 31 | monkeypatch.setenv("BAR", "456") |
| 32 | assert_parse_args(foo, "", 456) |
| 33 | |
| 34 | monkeypatch.delenv("BAR") |
| 35 | with pytest.raises(MissingArgumentError): |
| 36 | app.parse_args([], exit_on_error=False) |
| 37 | |
| 38 | |
| 39 | def test_env_var_list_set_use_env_var(app, assert_parse_args, monkeypatch): |
| 40 | @app.default |
| 41 | def foo(bar: Annotated[int, Parameter(env_var=["BAR", "BAZ"])] = 123): |
| 42 | pass |
| 43 | |
| 44 | monkeypatch.setenv("BAR", "456") |
| 45 | |
| 46 | assert_parse_args(foo, [], 456) |
| 47 | |
| 48 | |
| 49 | def test_env_var_unset_list_use_signature_default(app, assert_parse_args, monkeypatch): |
| 50 | @app.default |
| 51 | def foo(bar: Annotated[int, Parameter(env_var=["BAR", "BAZ"])] = 123): |
| 52 | pass |
| 53 | |
| 54 | monkeypatch.delenv("BAR", raising=False) |
| 55 | monkeypatch.delenv("BAZ", raising=False) |
| 56 | |
| 57 | assert_parse_args(foo, []) |