| 1 | """Test that all config classes support the source parameter.""" |
| 2 | |
| 3 | import json |
| 4 | import os |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from cyclopts import App |
| 9 | from cyclopts.config import Dict, Env, Json, Toml, Yaml |
| 10 | from cyclopts.exceptions import MissingArgumentError |
| 11 | |
| 12 | |
| 13 | def test_json_default_source(tmp_path): |
| 14 | """Test that Json uses file path as default source.""" |
| 15 | config_file = tmp_path / "config.json" |
| 16 | config_file.write_text(json.dumps({"name": "Alice"})) |
| 17 | |
| 18 | config = Json(config_file) |
| 19 | assert config.source == str(config_file.absolute()) |
| 20 | |
| 21 | |
| 22 | def test_json_custom_source(tmp_path): |
| 23 | """Test that Json accepts custom source parameter.""" |
| 24 | config_file = tmp_path / "config.json" |
| 25 | config_file.write_text(json.dumps({"name": "Alice"})) |
| 26 | |
| 27 | config = Json(config_file, source="my-custom-source") |
| 28 | assert config.source == "my-custom-source" |
| 29 | |
| 30 | |
| 31 | def test_json_custom_source_in_error(tmp_path): |
| 32 | """Test that custom source appears in error messages.""" |
| 33 | config_file = tmp_path / "config.json" |
| 34 | config_file.write_text(json.dumps({"age": 30})) |
| 35 | |
| 36 | app = App(config=Json(config_file, source="api-config"), result_action="return_value") |
| 37 | |
| 38 | @app.default |
| 39 | def main(name: str, age: int): |
| 40 | return f"{name} is {age} years old." |
| 41 | |
| 42 | with pytest.raises(MissingArgumentError): |
| 43 | app([], exit_on_error=False) |
| 44 | |
| 45 | |
| 46 | def test_json_source_setter(tmp_path): |
| 47 | """Test that Json.source can be modified via setter.""" |
| 48 | config_file = tmp_path / "config.json" |
| 49 | config_file.write_text(json.dumps({"name": "Alice"})) |
| 50 | |
| 51 | config = Json(config_file) |
| 52 | original_source = config.source |
| 53 | |
| 54 | config.source = "updated-source" |
| 55 | assert config.source == "updated-source" |
| 56 | |
| 57 | config.source = original_source |
| 58 | assert config.source == original_source |
| 59 | |
| 60 | |
| 61 | def test_toml_default_source(tmp_path): |
| 62 | """Test that Toml uses file path as default source.""" |
| 63 | config_file = tmp_path / "config.toml" |
| 64 | config_file.write_text('[main]\nname = "Alice"') |
| 65 | |
| 66 | config = Toml(config_file) |
| 67 | assert config.source == str(config_file.absolute()) |
| 68 | |
| 69 | |
| 70 | def test_toml_custom_source(tmp_path): |
| 71 | """Test that Toml accepts custom source parameter.""" |
| 72 | config_file = tmp_path / "config.toml" |
| 73 | config_file.write_text('[main]\nname = "Alice"') |
| 74 | |
| 75 | config = Toml(config_file, source="my-toml-source") |
| 76 | assert config.source == "my-toml-source" |
| 77 | |
| 78 | |
| 79 | def test_yaml_default_source(tmp_path): |
| 80 | """Test that Yaml uses file path as default source.""" |
| 81 | config_file = tmp_path / "config.yaml" |
| 82 | config_file.write_text("main:\n name: Alice") |
| 83 | |
| 84 | config = Yaml(config_file) |
| 85 | assert config.source == str(config_file.absolute()) |
| 86 | |
| 87 | |
| 88 | def test_yaml_custom_source(tmp_path): |
| 89 | """Test that Yaml accepts custom source parameter.""" |
| 90 | config_file = tmp_path / "config.yaml" |
| 91 | config_file.write_text("main:\n name: Alice") |
| 92 | |
| 93 | config = Yaml(config_file, source="my-yaml-source") |
| 94 | assert config.source == "my-yaml-source" |
| 95 | |
| 96 | |
| 97 | def test_env_default_source(): |
| 98 | """Test that Env uses 'env' as default source.""" |
| 99 | config = Env("TEST_") |
| 100 | assert config.source == "env" |
| 101 | |
| 102 | |
| 103 | def test_env_custom_source(): |
| 104 | """Test that Env accepts custom source parameter.""" |
| 105 | config = Env("TEST_", source="environment-variables") |
| 106 | assert config.source == "environment-variables" |
| 107 | |
| 108 | |
| 109 | def test_env_custom_source_in_tokens(): |
| 110 | """Test that Env custom source is used when creating tokens.""" |
| 111 | app = App(config=Env("TEST_", source="custom-env"), result_action="return_value") |
| 112 | |
| 113 | @app.default |
| 114 | def main(name: str = "default"): |
| 115 | return name |
| 116 | |
| 117 | os.environ["TEST_NAME"] = "Alice" |
| 118 | try: |
| 119 | result = app([]) |
| 120 | assert result == "Alice" |
| 121 | finally: |
| 122 | del os.environ["TEST_NAME"] |
| 123 | |
| 124 | |
| 125 | def test_dict_default_source(): |
| 126 | """Test that Dict uses 'dict' as default source.""" |
| 127 | config = Dict({"name": "Alice"}) |
| 128 | assert config.source == "dict" |
| 129 | |
| 130 | |
| 131 | def test_dict_custom_source(): |
| 132 | """Test that Dict accepts custom source parameter.""" |
| 133 | config = Dict({"name": "Alice"}, source="api-response") |
| 134 | assert config.source == "api-response" |
| 135 | |
| 136 | |
| 137 | def test_multiple_configs_with_custom_sources(tmp_path): |
| 138 | """Test using multiple configs with custom sources.""" |
| 139 | json_file = tmp_path / "config.json" |
| 140 | json_file.write_text(json.dumps({"name": "Alice"})) |
| 141 | |
| 142 | app = App( |
| 143 | config=[ |
| 144 | Json(json_file, source="json-config"), |
| 145 | Dict({"age": 30}, source="dict-config"), |
| 146 | Env("TEST_", source="env-config"), |
| 147 | ], |
| 148 | result_action="return_value", |
| 149 | ) |
| 150 | |
| 151 | @app.default |
| 152 | def main(name: str, age: int, city: str = "Unknown"): |
| 153 | return f"{name}, {age}, {city}" |
| 154 | |
| 155 | os.environ["TEST_CITY"] = "Springfield" |
| 156 | try: |
| 157 | result = app([]) |
| 158 | assert result == "Alice, 30, Springfield" |
| 159 | finally: |
| 160 | del os.environ["TEST_CITY"] |
| 161 | |
| 162 | |
| 163 | def test_source_preserved_across_app_calls(tmp_path): |
| 164 | """Test that custom source is preserved across multiple app calls.""" |
| 165 | config_file = tmp_path / "config.json" |
| 166 | config_file.write_text(json.dumps({"value": 42})) |
| 167 | |
| 168 | config = Json(config_file, source="persistent-source") |
| 169 | app = App(config=config, result_action="return_value") |
| 170 | |
| 171 | @app.default |
| 172 | def main(value: int): |
| 173 | return value |
| 174 | |
| 175 | result1 = app([]) |
| 176 | assert result1 == 42 |
| 177 | assert config.source == "persistent-source" |
| 178 | |
| 179 | result2 = app([]) |
| 180 | assert result2 == 42 |
| 181 | assert config.source == "persistent-source" |