|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import contextlib |
| 4 | +import importlib.util |
4 | 5 | import io |
5 | 6 | import json |
6 | 7 | import os |
| 8 | +from pathlib import Path |
7 | 9 | import subprocess |
8 | 10 | import tempfile |
9 | 11 | import unittest |
10 | | -from pathlib import Path |
11 | 12 | from unittest import mock |
12 | 13 |
|
13 | | -import install_tool_shims |
| 14 | +INSTALL_TOOL_SHIMS_PATH = Path(__file__).resolve().parent / "install_tool_shims.py" |
| 15 | +INSTALL_TOOL_SHIMS_SPEC = importlib.util.spec_from_file_location( |
| 16 | + "install_tool_shims", INSTALL_TOOL_SHIMS_PATH |
| 17 | +) |
| 18 | +if INSTALL_TOOL_SHIMS_SPEC is None or INSTALL_TOOL_SHIMS_SPEC.loader is None: |
| 19 | + raise RuntimeError(f"failed to load {INSTALL_TOOL_SHIMS_PATH}") |
| 20 | +install_tool_shims = importlib.util.module_from_spec(INSTALL_TOOL_SHIMS_SPEC) |
| 21 | +INSTALL_TOOL_SHIMS_SPEC.loader.exec_module(install_tool_shims) |
14 | 22 |
|
15 | 23 |
|
16 | 24 | class CopyPublishedToolsTest(unittest.TestCase): |
@@ -66,6 +74,66 @@ def test_tool_allowlist_restricts_installed_tools(self) -> None: |
66 | 74 | self.assertTrue((target / "research" / "websearch" / "pyproject.toml").exists()) |
67 | 75 | self.assertFalse((target / "productivity" / "linear").exists()) |
68 | 76 |
|
| 77 | + def test_tool_allowlist_matches_project_name_when_copying_published_tools(self) -> None: |
| 78 | + with tempfile.TemporaryDirectory() as tmp: |
| 79 | + root = Path(tmp) |
| 80 | + published = root / "published" |
| 81 | + target = root / "target" |
| 82 | + |
| 83 | + composio = published / "productivity" / "composio" |
| 84 | + composio.mkdir(parents=True) |
| 85 | + (composio / "pyproject.toml").write_text( |
| 86 | + '[project]\nname = "centaur-composio-tool"\n' |
| 87 | + ) |
| 88 | + linear = published / "productivity" / "linear" |
| 89 | + linear.mkdir(parents=True) |
| 90 | + (linear / "pyproject.toml").write_text('[project]\nname = "linear"\n') |
| 91 | + |
| 92 | + with mock.patch.dict("os.environ", {"TOOL_ALLOWLIST": "centaur-composio-tool"}): |
| 93 | + install_tool_shims._copy_published_tools(target, published) |
| 94 | + |
| 95 | + self.assertTrue((target / "productivity" / "composio" / "pyproject.toml").exists()) |
| 96 | + self.assertFalse((target / "productivity" / "linear").exists()) |
| 97 | + |
| 98 | + def test_tool_blocklist_matches_project_name_when_copying_published_tools(self) -> None: |
| 99 | + with tempfile.TemporaryDirectory() as tmp: |
| 100 | + root = Path(tmp) |
| 101 | + published = root / "published" |
| 102 | + target = root / "target" |
| 103 | + |
| 104 | + composio = published / "productivity" / "composio" |
| 105 | + composio.mkdir(parents=True) |
| 106 | + (composio / "pyproject.toml").write_text( |
| 107 | + '[project]\nname = "centaur-composio-tool"\n' |
| 108 | + ) |
| 109 | + linear = published / "productivity" / "linear" |
| 110 | + linear.mkdir(parents=True) |
| 111 | + (linear / "pyproject.toml").write_text('[project]\nname = "linear"\n') |
| 112 | + |
| 113 | + with mock.patch.dict("os.environ", {"TOOL_BLOCKLIST": "centaur-composio-tool"}): |
| 114 | + install_tool_shims._copy_published_tools(target, published) |
| 115 | + |
| 116 | + self.assertFalse((target / "productivity" / "composio").exists()) |
| 117 | + self.assertTrue((target / "productivity" / "linear" / "pyproject.toml").exists()) |
| 118 | + |
| 119 | + def test_malformed_project_table_falls_back_to_directory_matching(self) -> None: |
| 120 | + with tempfile.TemporaryDirectory() as tmp: |
| 121 | + root = Path(tmp) |
| 122 | + published = root / "published" |
| 123 | + target = root / "target" |
| 124 | + |
| 125 | + broken = published / "research" / "broken" |
| 126 | + broken.mkdir(parents=True) |
| 127 | + (broken / "pyproject.toml").write_text('project = "not-a-table"\n') |
| 128 | + |
| 129 | + stderr = io.StringIO() |
| 130 | + with mock.patch.dict("os.environ", {"TOOL_ALLOWLIST": "broken"}): |
| 131 | + with contextlib.redirect_stderr(stderr): |
| 132 | + install_tool_shims._copy_published_tools(target, published) |
| 133 | + |
| 134 | + self.assertTrue((target / "research" / "broken" / "pyproject.toml").exists()) |
| 135 | + self.assertIn("warning: invalid [project] table", stderr.getvalue()) |
| 136 | + |
69 | 137 | def test_unset_allowlist_installs_all_tools(self) -> None: |
70 | 138 | with tempfile.TemporaryDirectory() as tmp: |
71 | 139 | root = Path(tmp) |
|
0 commit comments