"""The .env loader, which is only interesting where it disagrees with itself. These tests exist because of one real incident: the Docker image declares ``ENV DASHSCOPE_API_KEY=`` so the variable shows up in ``docker inspect`` and can be overridden at ``docker run``. That left it set-but-empty inside every container, where it shadowed the mounted .env file. Every OCR page failed with "No API key", the pipeline fell back page by page as designed, and a 25-page run finished successfully having stored nothing. """ from __future__ import annotations from pathlib import Path from leanbench.config import load_dotenv def write_env(directory: Path, body: str) -> Path: path = directory / ".env" path.write_text(body, encoding="utf-8") return path def test_an_empty_variable_does_not_shadow_the_file(tmp_path: Path, monkeypatch) -> None: """The incident, in one assertion.""" monkeypatch.setenv("LEANBENCH_TEST_KEY", "") load_dotenv(write_env(tmp_path, "LEANBENCH_TEST_KEY=from-file\n")) import os assert os.environ["LEANBENCH_TEST_KEY"] == "from-file" def test_a_real_value_still_wins(tmp_path: Path, monkeypatch) -> None: """Overriding at the command line must keep working -- that is the whole point.""" monkeypatch.setenv("LEANBENCH_TEST_KEY", "from-environment") load_dotenv(write_env(tmp_path, "LEANBENCH_TEST_KEY=from-file\n")) import os assert os.environ["LEANBENCH_TEST_KEY"] == "from-environment" def test_an_unset_variable_is_taken_from_the_file(tmp_path: Path, monkeypatch) -> None: monkeypatch.delenv("LEANBENCH_TEST_KEY", raising=False) load_dotenv(write_env(tmp_path, "LEANBENCH_TEST_KEY=from-file\n")) import os assert os.environ["LEANBENCH_TEST_KEY"] == "from-file" def test_comments_blanks_and_quotes(tmp_path: Path, monkeypatch) -> None: monkeypatch.delenv("LEANBENCH_A", raising=False) monkeypatch.delenv("LEANBENCH_B", raising=False) monkeypatch.delenv("LEANBENCH_C", raising=False) load_dotenv(write_env(tmp_path, """ # a comment LEANBENCH_A = "quoted value" LEANBENCH_B='single' not a pair LEANBENCH_C=has=equals=inside """)) import os assert os.environ["LEANBENCH_A"] == "quoted value" assert os.environ["LEANBENCH_B"] == "single" assert os.environ["LEANBENCH_C"] == "has=equals=inside" def test_a_missing_file_is_not_an_error(tmp_path: Path) -> None: load_dotenv(tmp_path / "nope.env")