Simplified /workspace to map to a project scoped WORKSPACE_ROOT

This commit is contained in:
Fahad
2025-06-13 20:49:37 +04:00
parent ebf5cfaa9e
commit 26b22a1d53
7 changed files with 140 additions and 156 deletions

View File

@@ -31,11 +31,11 @@ import config # noqa: E402
importlib.reload(config)
# Set MCP_PROJECT_ROOT to a temporary directory for tests
# Set WORKSPACE_ROOT to a temporary directory for tests
# This provides a safe sandbox for file operations during testing
# Create a temporary directory that will be used as the project root for all tests
# Create a temporary directory that will be used as the workspace for all tests
test_root = tempfile.mkdtemp(prefix="zen_mcp_test_")
os.environ["MCP_PROJECT_ROOT"] = test_root
os.environ["WORKSPACE_ROOT"] = test_root
# Configure asyncio for Windows compatibility
if sys.platform == "win32":
@@ -55,11 +55,11 @@ ModelProviderRegistry.register_provider(ProviderType.OPENAI, OpenAIModelProvider
@pytest.fixture
def project_path(tmp_path):
"""
Provides a temporary directory within the PROJECT_ROOT sandbox for tests.
Provides a temporary directory within the WORKSPACE_ROOT sandbox for tests.
This ensures all file operations during tests are within the allowed directory.
"""
# Get the test project root
test_root = Path(os.environ.get("MCP_PROJECT_ROOT", "/tmp"))
# Get the test workspace root
test_root = Path(os.environ.get("WORKSPACE_ROOT", "/tmp"))
# Create a subdirectory for this specific test
test_dir = test_root / f"test_{tmp_path.name}"

View File

@@ -79,7 +79,6 @@ def test_docker_security_validation():
original_env = os.environ.copy()
try:
os.environ["WORKSPACE_ROOT"] = str(host_workspace)
os.environ["MCP_PROJECT_ROOT"] = str(host_workspace)
# Reload the module
importlib.reload(utils.file_utils)

View File

@@ -60,10 +60,10 @@ class TestPrecommitToolWithMockStore:
temp_dir, _ = temp_repo
tool = Precommit()
# Mock the Redis client getter and PROJECT_ROOT to allow access to temp files
# Mock the Redis client getter and SECURITY_ROOT to allow access to temp files
with (
patch("utils.conversation_memory.get_redis_client", return_value=mock_redis),
patch("utils.file_utils.PROJECT_ROOT", Path(temp_dir).resolve()),
patch("utils.file_utils.SECURITY_ROOT", Path(temp_dir).resolve()),
):
yield tool

View File

@@ -34,7 +34,7 @@ class TestFileUtils:
# Try to read a file outside the project root
content, tokens = read_file_content("/etc/passwd")
assert "--- ERROR ACCESSING FILE:" in content
assert "Path outside project root" in content
assert "Path outside workspace" in content
assert tokens > 0
def test_read_file_content_relative_path_rejected(self):