arcade-mcp/libs/tests/arcade_mcp_server/test_env_discovery.py
Eric Gustin 4a737b9710
Improve .env discovery (#737)
Resolves TOO-201

Documentation PR for this is here:
https://github.com/ArcadeAI/docs/pull/626


<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Medium Risk**
> Changes how environment variables/secrets are discovered and loaded,
which can subtly alter runtime behavior depending on directory structure
and existing env vars; bounded traversal and added tests reduce but
don’t eliminate this risk.
> 
> **Overview**
> **Improves `.env` discovery across the MCP server and CLI.** Adds
`find_env_file()` (bounded by the nearest `pyproject.toml` by default)
and switches settings loading, `arcade deploy`, `arcade configure` stdio
env injection, and provider API-key resolution to use it.
> 
> Updates dev reload to also watch the discovered `.env` even when it
lives outside the current working directory, adjusts `deploy --secrets
all` to only run when a `.env` was found, and moves the minimal
scaffold’s `.env.example` to the project root with updated
tests/integration checks. Version bumps align examples and top-level
deps with `arcade-mcp-server` `1.17.4` and `arcade-mcp` `1.11.2`.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
40cff1738c14674ce01f09fd325ece9c874cd072. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-25 23:20:28 -08:00

111 lines
3.8 KiB
Python

"""Tests for find_env_file() upward directory traversal."""
from pathlib import Path
import pytest
from arcade_mcp_server.settings import find_env_file
class TestFindEnvFile:
"""Test the find_env_file() utility function."""
def test_finds_env_in_current_directory(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should find .env file in cwd."""
env_file = tmp_path / ".env"
env_file.write_text("TEST_VAR=value")
monkeypatch.chdir(tmp_path)
assert find_env_file() == env_file
def test_finds_env_in_parent_directory(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should traverse upward to find .env in parent (no pyproject.toml boundary)."""
subdir = tmp_path / "a" / "b" / "c"
subdir.mkdir(parents=True)
env_file = tmp_path / ".env"
env_file.write_text("TEST_VAR=value")
monkeypatch.chdir(subdir)
assert find_env_file() == env_file
def test_prefers_closest_env_file(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should find closest .env when multiple exist."""
subdir = tmp_path / "subdir"
subdir.mkdir()
(tmp_path / ".env").write_text("ROOT=1")
closer_env = subdir / ".env"
closer_env.write_text("CLOSER=1")
monkeypatch.chdir(subdir)
assert find_env_file() == closer_env
def test_returns_none_when_not_found(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should return None when no .env exists."""
subdir = tmp_path / "subdir"
subdir.mkdir()
monkeypatch.chdir(subdir)
assert find_env_file() is None
def test_stop_at_limits_traversal(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Explicit stop_at should prevent traversing past specified directory."""
project = tmp_path / "project" / "src"
project.mkdir(parents=True)
(tmp_path / ".env").write_text("OUTSIDE=1")
monkeypatch.chdir(project)
assert find_env_file(stop_at=tmp_path / "project") is None
def test_stops_at_pyproject_toml_boundary(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should not traverse past directory containing pyproject.toml."""
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pyproject.toml").write_text("[project]\nname = 'test'")
src = project_root / "src"
src.mkdir()
# .env is above the project root — should NOT be found
(tmp_path / ".env").write_text("OUTSIDE=1")
monkeypatch.chdir(src)
assert find_env_file() is None
def test_finds_env_at_pyproject_toml_level(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should find .env at the same level as pyproject.toml."""
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pyproject.toml").write_text("[project]\nname = 'test'")
env_file = project_root / ".env"
env_file.write_text("SECRET=value")
src = project_root / "src"
src.mkdir()
monkeypatch.chdir(src)
assert find_env_file() == env_file
def test_finds_env_below_pyproject_toml(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Should find .env in a subdirectory within the project."""
project_root = tmp_path / "project"
project_root.mkdir()
(project_root / "pyproject.toml").write_text("[project]\nname = 'test'")
src = project_root / "src"
src.mkdir()
env_file = src / ".env"
env_file.write_text("SECRET=value")
monkeypatch.chdir(src)
assert find_env_file() == env_file