fix(config): keep missing env refs unresolved

This commit is contained in:
Evan Luo 2026-03-19 15:51:29 +00:00 committed by chengyongru
parent 11b60c84df
commit f371072d3f
3 changed files with 9 additions and 7 deletions

View File

@ -19,12 +19,15 @@ def resolve_env_vars(value: str) -> str:
Returns:
String with all {env:VAR} references replaced by their values.
Missing env vars resolve to empty string.
Missing env vars are left unchanged.
"""
def replacer(match: re.Match[str]) -> str:
var_name = match.group(1)
return os.environ.get(var_name, "")
env_value = os.environ.get(var_name)
if env_value is None:
return match.group(0)
return env_value
return _REF_PATTERN.sub(replacer, value)

View File

@ -216,7 +216,7 @@ def test_save_keeps_intentional_in_memory_override_of_env_ref(tmp_path, monkeypa
assert saved["providers"]["openai"]["apiKey"] == "sk-manual-override"
def test_missing_env_ref_resolves_empty_at_runtime_but_persists_placeholder(tmp_path) -> None:
def test_missing_env_ref_stays_unresolved_and_persists_placeholder(tmp_path) -> None:
config_path = tmp_path / "config.json"
config_path.write_text(
json.dumps(
@ -232,8 +232,7 @@ def test_missing_env_ref_resolves_empty_at_runtime_but_persists_placeholder(tmp_
)
config = load_config(config_path)
assert config.providers.openai.api_key == ""
assert config.get_provider_name("openai/gpt-4.1") is None
assert config.providers.openai.api_key == "{env:MISSING_OPENAI_KEY}"
save_config(config, config_path)
saved = json.loads(config_path.read_text(encoding="utf-8"))

View File

@ -21,8 +21,8 @@ class TestResolveEnvVars:
monkeypatch.setenv("HOST", "example.com")
assert resolve_env_vars("{env:USER}@{env:HOST}") == "alice@example.com"
def test_unresolved_var_becomes_empty(self) -> None:
assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == ""
def test_unresolved_var_kept_unchanged(self) -> None:
assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == "{env:NONEXISTENT_VAR_XYZ}"
def test_empty_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("EMPTY_VAR", "")