Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Regression tests for lazy creation of the Composio cache directory. |
| 2 | |
| 3 | ``composio/core/models/_files.py`` used to create ``~/.composio`` and assert |
| 4 | that it was writable at *module import time*. Because ``composio/__init__.py`` |
| 5 | imports ``tools.py``, which imports ``_files.py`` unconditionally, a bare |
| 6 | ``import composio`` raised ``RuntimeError`` on any read-only filesystem -- |
| 7 | AWS Lambda, distroless containers, ``ProtectHome=true`` systemd units -- even |
| 8 | for programs that never touched a file (#4150). |
| 9 | |
| 10 | These tests pin the fix: importing the package performs no filesystem work, |
| 11 | ``COMPOSIO_CACHE_DIR`` is honoured when set after import, and the writability |
| 12 | check still raises the same error when the directory is genuinely needed. |
| 13 | """ |
| 14 | |
| 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | from pathlib import Path |
| 19 | from unittest.mock import Mock |
| 20 | |
| 21 | import pytest |
| 22 | |
| 23 | from composio.core.models import _files |
| 24 | from composio.core.models._files import FileHelper |
| 25 | from composio.exceptions import BlockedInternalUrlError |
| 26 | |
| 27 | |
| 28 | @pytest.fixture() |
| 29 | def unusable_cache_dir(tmp_path: Path) -> Path: |
| 30 | """A cache path that cannot be created, without relying on permissions. |
| 31 | |
| 32 | ``chmod``-based read-only directories are a no-op when the test suite |
| 33 | runs as root (``os.access(..., W_OK)`` returns ``True`` for uid 0), which |
| 34 | is common in containerized CI. Nesting the cache directory *underneath a |
| 35 | regular file* makes ``mkdir`` fail with ``NotADirectoryError`` for every |
| 36 | user. |
| 37 | """ |
| 38 | blocker = tmp_path / "blocker" |
| 39 | blocker.write_text("not a directory") |
| 40 | return blocker / "cache" |
| 41 | |
| 42 | |
| 43 | def test_importing_composio_does_not_create_the_cache_directory( |
| 44 | unusable_cache_dir: Path, |
| 45 | ) -> None: |
| 46 | """The regression test: ``import composio`` must not touch the filesystem. |
| 47 | |
| 48 | Run in a subprocess so the import genuinely happens under the patched |
| 49 | environment rather than hitting the already-imported module in this |
| 50 | process. |
| 51 | """ |
| 52 | env = dict(os.environ) |
| 53 | env["COMPOSIO_CACHE_DIR"] = str(unusable_cache_dir) |
| 54 | |
| 55 | result = subprocess.run( |
| 56 | [sys.executable, "-c", "import composio"], |
| 57 | env=env, |
| 58 | capture_output=True, |
| 59 | text=True, |
| 60 | timeout=120, |
| 61 | ) |
| 62 | |
| 63 | assert result.returncode == 0, ( |
| 64 | "importing composio with an uncreatable cache directory must succeed, " |
| 65 | f"but it failed with:\n{result.stderr}" |
| 66 | ) |
| 67 | assert not unusable_cache_dir.exists() |
| 68 | |
| 69 | |
| 70 | def test_cache_directory_is_created_only_on_demand( |
| 71 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 72 | ) -> None: |
| 73 | cache_dir = tmp_path / "not-yet-there" |
| 74 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(cache_dir)) |
| 75 | |
| 76 | # Merely resolving the path must not create it. |
| 77 | assert _files.get_cache_directory() == cache_dir |
| 78 | assert not cache_dir.exists() |
| 79 | |
| 80 | assert _files.ensure_cache_directory() == cache_dir |
| 81 | assert cache_dir.is_dir() |
| 82 | |
| 83 | |
| 84 | def test_cache_dir_env_var_is_read_after_import( |
| 85 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 86 | ) -> None: |
| 87 | """``COMPOSIO_CACHE_DIR`` used to be frozen at import time.""" |
| 88 | first = tmp_path / "first" |
| 89 | second = tmp_path / "second" |
| 90 | |
| 91 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(first)) |
| 92 | assert _files.get_cache_directory() == first |
| 93 | |
| 94 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(second)) |
| 95 | assert _files.get_cache_directory() == second |
| 96 | |
| 97 | |
| 98 | def test_home_is_not_resolved_when_the_env_var_is_set( |
| 99 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 100 | ) -> None: |
| 101 | """The escape hatch has to work when ``Path.home()`` itself fails. |
| 102 | |
| 103 | The old code passed ``Path.home() / ...`` as the *default argument* to |
| 104 | ``os.environ.get``, which Python evaluates eagerly, so setting |
| 105 | ``COMPOSIO_CACHE_DIR`` did not save a user whose home directory could not |
| 106 | be resolved. |
| 107 | """ |
| 108 | |
| 109 | def _explode() -> Path: |
| 110 | raise RuntimeError("Could not determine home directory") |
| 111 | |
| 112 | monkeypatch.setattr(Path, "home", staticmethod(_explode)) |
| 113 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(tmp_path / "cache")) |
| 114 | |
| 115 | assert _files.get_cache_directory() == tmp_path / "cache" |
| 116 | |
| 117 | |
| 118 | def test_unresolvable_home_raises_a_helpful_error( |
| 119 | monkeypatch: pytest.MonkeyPatch, |
| 120 | ) -> None: |
| 121 | def _explode() -> Path: |
| 122 | raise RuntimeError("Could not determine home directory") |
| 123 | |
| 124 | monkeypatch.setattr(Path, "home", staticmethod(_explode)) |
| 125 | monkeypatch.delenv("COMPOSIO_CACHE_DIR", raising=False) |
| 126 | |
| 127 | with pytest.raises(RuntimeError, match="COMPOSIO_CACHE_DIR"): |
| 128 | _files.get_cache_directory() |
| 129 | |
| 130 | |
| 131 | def test_output_file_directory_hangs_off_the_cache_directory( |
| 132 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 133 | ) -> None: |
| 134 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(tmp_path / "cache")) |
| 135 | assert _files.get_output_file_directory() == tmp_path / "cache" / "files" |
| 136 | |
| 137 | |
| 138 | def test_ensure_cache_directory_still_reports_unwritable_paths( |
| 139 | monkeypatch: pytest.MonkeyPatch, unusable_cache_dir: Path |
| 140 | ) -> None: |
| 141 | """The writability check is deferred, not removed.""" |
| 142 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(unusable_cache_dir)) |
| 143 | |
| 144 | with pytest.raises(RuntimeError, match="COMPOSIO_CACHE_DIR"): |
| 145 | _files.ensure_cache_directory() |
| 146 | |
| 147 | |
| 148 | def test_legacy_module_constants_still_resolve( |
| 149 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 150 | ) -> None: |
| 151 | """``LOCAL_CACHE_DIRECTORY`` was public-ish; keep it importable (PEP 562).""" |
| 152 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(tmp_path / "cache")) |
| 153 | |
| 154 | assert _files.LOCAL_CACHE_DIRECTORY == tmp_path / "cache" |
| 155 | assert _files.LOCAL_OUTPUT_FILE_DIRECTORY == tmp_path / "cache" / "files" |
| 156 | |
| 157 | # And unlike the old module-level constants, they track the environment. |
| 158 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(tmp_path / "other")) |
| 159 | assert _files.LOCAL_CACHE_DIRECTORY == tmp_path / "other" |
| 160 | |
| 161 | |
| 162 | def test_unknown_module_attribute_still_raises_attribute_error() -> None: |
| 163 | """The module ``__getattr__`` must not swallow genuine typos.""" |
| 164 | with pytest.raises(AttributeError, match="no attribute"): |
| 165 | _files.LOCAL_CACHE_DIRECTROY # noqa: B018 (deliberate typo) |
| 166 | |
| 167 | |
| 168 | def test_file_helper_with_explicit_outdir_never_touches_the_default_cache( |
| 169 | monkeypatch: pytest.MonkeyPatch, unusable_cache_dir: Path, tmp_path: Path |
| 170 | ) -> None: |
| 171 | """A caller-supplied ``outdir`` must not force the default cache directory |
| 172 | into existence, and downloads into it must not trigger the writability |
| 173 | check for the (unused) default cache directory.""" |
| 174 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(unusable_cache_dir)) |
| 175 | |
| 176 | outdir = tmp_path / "custom-outdir" |
| 177 | helper = FileHelper(client=Mock(), outdir=str(outdir)) |
| 178 | assert helper._outdir_is_default is False |
| 179 | |
| 180 | tool = Mock() |
| 181 | tool.toolkit.slug = "gmail" |
| 182 | tool.slug = "GMAIL_FETCH" |
| 183 | |
| 184 | # The download itself fails fast on the blank s3url, but the point of |
| 185 | # this test is what happened *before* that: the default cache directory |
| 186 | # must never have been created. |
| 187 | with pytest.raises(BlockedInternalUrlError): |
| 188 | helper._download_file_value( |
| 189 | {"name": "irrelevant.txt", "mimetype": "text/plain", "s3url": ""}, |
| 190 | tool, |
| 191 | ) |
| 192 | assert not unusable_cache_dir.exists() |
| 193 | |
| 194 | |
| 195 | def test_file_helper_default_outdir_ensures_cache_directory_on_download( |
| 196 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 197 | ) -> None: |
| 198 | """The default-outdir path is exactly where the deferred writability |
| 199 | check must still fire, with the original error message.""" |
| 200 | unusable = tmp_path / "blocker-file" |
| 201 | unusable.write_text("not a directory") |
| 202 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(unusable / "cache")) |
| 203 | |
| 204 | helper = FileHelper(client=Mock()) |
| 205 | assert helper._outdir_is_default is True |
| 206 | |
| 207 | tool = Mock() |
| 208 | tool.toolkit.slug = "gmail" |
| 209 | tool.slug = "GMAIL_FETCH" |
| 210 | |
| 211 | with pytest.raises(RuntimeError, match="COMPOSIO_CACHE_DIR"): |
| 212 | helper._download_file_value( |
| 213 | { |
| 214 | "name": "irrelevant.txt", |
| 215 | "mimetype": "text/plain", |
| 216 | "s3url": "https://example.com/f", |
| 217 | }, |
| 218 | tool, |
| 219 | ) |
| 220 | |
| 221 | |
| 222 | def test_file_helper_empty_string_outdir_is_treated_as_default( |
| 223 | monkeypatch: pytest.MonkeyPatch, tmp_path: Path |
| 224 | ) -> None: |
| 225 | """``outdir=""`` falls through to the default directory (falsy, not just |
| 226 | ``None``), so the default-ness flag must agree or the writability check |
| 227 | silently stops firing for it.""" |
| 228 | unusable = tmp_path / "blocker-file" |
| 229 | unusable.write_text("not a directory") |
| 230 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(unusable / "cache")) |
| 231 | |
| 232 | helper = FileHelper(client=Mock(), outdir="") |
| 233 | assert helper._outdir_is_default is True |
| 234 | assert helper._outdir == _files.get_output_file_directory() |
| 235 | |
| 236 | tool = Mock() |
| 237 | tool.toolkit.slug = "gmail" |
| 238 | tool.slug = "GMAIL_FETCH" |
| 239 | |
| 240 | with pytest.raises(RuntimeError, match="COMPOSIO_CACHE_DIR"): |
| 241 | helper._download_file_value( |
| 242 | { |
| 243 | "name": "irrelevant.txt", |
| 244 | "mimetype": "text/plain", |
| 245 | "s3url": "https://example.com/f", |
| 246 | }, |
| 247 | tool, |
| 248 | ) |