mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-04-25 12:26:00 +00:00
fix(config): keep missing env refs unresolved
This commit is contained in:
parent
11b60c84df
commit
f371072d3f
@ -19,12 +19,15 @@ def resolve_env_vars(value: str) -> str:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
String with all {env:VAR} references replaced by their values.
|
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:
|
def replacer(match: re.Match[str]) -> str:
|
||||||
var_name = match.group(1)
|
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)
|
return _REF_PATTERN.sub(replacer, value)
|
||||||
|
|
||||||
|
|||||||
@ -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"
|
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 = tmp_path / "config.json"
|
||||||
config_path.write_text(
|
config_path.write_text(
|
||||||
json.dumps(
|
json.dumps(
|
||||||
@ -232,8 +232,7 @@ def test_missing_env_ref_resolves_empty_at_runtime_but_persists_placeholder(tmp_
|
|||||||
)
|
)
|
||||||
|
|
||||||
config = load_config(config_path)
|
config = load_config(config_path)
|
||||||
assert config.providers.openai.api_key == ""
|
assert config.providers.openai.api_key == "{env:MISSING_OPENAI_KEY}"
|
||||||
assert config.get_provider_name("openai/gpt-4.1") is None
|
|
||||||
|
|
||||||
save_config(config, config_path)
|
save_config(config, config_path)
|
||||||
saved = json.loads(config_path.read_text(encoding="utf-8"))
|
saved = json.loads(config_path.read_text(encoding="utf-8"))
|
||||||
|
|||||||
@ -21,8 +21,8 @@ class TestResolveEnvVars:
|
|||||||
monkeypatch.setenv("HOST", "example.com")
|
monkeypatch.setenv("HOST", "example.com")
|
||||||
assert resolve_env_vars("{env:USER}@{env:HOST}") == "alice@example.com"
|
assert resolve_env_vars("{env:USER}@{env:HOST}") == "alice@example.com"
|
||||||
|
|
||||||
def test_unresolved_var_becomes_empty(self) -> None:
|
def test_unresolved_var_kept_unchanged(self) -> None:
|
||||||
assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == ""
|
assert resolve_env_vars("{env:NONEXISTENT_VAR_XYZ}") == "{env:NONEXISTENT_VAR_XYZ}"
|
||||||
|
|
||||||
def test_empty_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
def test_empty_env_var(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||||
monkeypatch.setenv("EMPTY_VAR", "")
|
monkeypatch.setenv("EMPTY_VAR", "")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user