Co-Authored-By: Vincent Guilloux <vincent.guilloux@mistral.ai>
Co-Authored-By: Clément Siriex <clement.sirieix@mistral.ai>
Co-Authored-By: Kim-Adeline Miguel <kimadeline.miguel@mistral.ai>
This commit is contained in:
Mathias Gesbert 2026-01-30 14:08:38 +01:00 committed by Mathias Gesbert
parent bd3497b1c0
commit 9809cfc831
32 changed files with 622 additions and 305 deletions

View file

@ -0,0 +1,61 @@
from __future__ import annotations
from pathlib import Path
from vibe.core.config import load_dotenv_values
def _write_env_file(path: Path, content: str) -> None:
path.write_text(content, encoding="utf-8")
def test_skips_missing_file(tmp_path: Path) -> None:
environ = {"EXISTING": "1"}
missing_path = tmp_path / "missing.env"
load_dotenv_values(env_path=missing_path, environ=environ)
assert environ == {"EXISTING": "1"}
def test_sets_and_overrides_values(tmp_path: Path) -> None:
env_path = tmp_path / ".env"
_write_env_file(
env_path,
"\n".join([
"MISTRAL_API_KEY=new-key",
"HTTPS_PROXY=https://local-proxy:8080",
"OTHER=from-env",
"NEW_KEY=added",
"FOO=replace",
])
+ "\n",
)
environ = {
"MISTRAL_API_KEY": "old-key",
"HTTPS_PROXY": "old-https",
"OTHER": "keep",
"FOO": "keep",
}
load_dotenv_values(env_path=env_path, environ=environ)
assert environ["MISTRAL_API_KEY"] == "new-key"
assert environ["HTTPS_PROXY"] == "https://local-proxy:8080"
assert environ["OTHER"] == "from-env"
assert environ["NEW_KEY"] == "added"
assert environ["FOO"] == "replace"
def test_ignores_empty_values(tmp_path: Path) -> None:
env_path = tmp_path / ".env"
_write_env_file(
env_path, "\n".join(["EMPTY=", "MISTRAL_API_KEY=", "NO_VALUE"]) + "\n"
)
environ: dict[str, str] = {}
load_dotenv_values(env_path=env_path, environ=environ)
assert "EMPTY" not in environ
assert "MISTRAL_API_KEY" not in environ
assert "NO_VALUE" not in environ

View file

@ -1,51 +0,0 @@
from __future__ import annotations
from contextlib import contextmanager
from pathlib import Path
import tomllib
import tomli_w
from vibe.core import config
from vibe.core.config import VibeConfig
def _restore_dump_config(config_file: Path):
original_dump_config = VibeConfig.dump_config
def real_dump_config(cls, config_dict: dict) -> None:
try:
with config_file.open("wb") as f:
tomli_w.dump(config_dict, f)
except OSError:
config_file.write_text(
"\n".join(
f"{k} = {v!r}" for k, v in config_dict.items() if v is not None
),
encoding="utf-8",
)
VibeConfig.dump_config = classmethod(real_dump_config) # type: ignore[assignment]
return original_dump_config
@contextmanager
def _migrate_config_file(tmp_path: Path, content: str):
config_file = tmp_path / "config.toml"
config_file.write_text(content, encoding="utf-8")
original_config_file = config.CONFIG_FILE
original_dump_config = _restore_dump_config(config_file)
try:
config.CONFIG_FILE = config_file
VibeConfig._migrate()
yield config_file
finally:
config.CONFIG_FILE = original_config_file
VibeConfig.dump_config = original_dump_config
def _load_migrated_config(config_file: Path) -> dict:
with config_file.open("rb") as f:
return tomllib.load(f)