|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from codemcp.main import init_codemcp_project |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def project_dir(): |
| 15 | + """Create a temporary project directory with a simple codemcp.toml configuration.""" |
| 16 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 17 | + # Initialize the project |
| 18 | + init_codemcp_project(temp_dir) |
| 19 | + |
| 20 | + # Create a codemcp.toml file with test commands |
| 21 | + config_path = os.path.join(temp_dir, "codemcp.toml") |
| 22 | + with open(config_path, "w") as f: |
| 23 | + f.write(""" |
| 24 | +[commands] |
| 25 | +echo = ["echo", "Hello World"] |
| 26 | +pwd = ["pwd"] |
| 27 | + """) |
| 28 | + |
| 29 | + # Create a subdirectory |
| 30 | + subdir = os.path.join(temp_dir, "subdir") |
| 31 | + os.makedirs(subdir, exist_ok=True) |
| 32 | + |
| 33 | + yield temp_dir |
| 34 | + |
| 35 | + |
| 36 | +def test_run_command_from_subdir(project_dir): |
| 37 | + """Test running a command from a subdirectory of the project.""" |
| 38 | + subdir = os.path.join(project_dir, "subdir") |
| 39 | + |
| 40 | + # Run pwd command from subdirectory to verify cwd |
| 41 | + result = subprocess.run( |
| 42 | + [sys.executable, "-m", "codemcp", "run", "pwd", "--path", subdir], |
| 43 | + capture_output=True, |
| 44 | + text=True, |
| 45 | + check=True, |
| 46 | + ) |
| 47 | + |
| 48 | + # The pwd output should show the project root, not the subdirectory |
| 49 | + # Normalize paths for comparison (strip trailing slashes, etc.) |
| 50 | + normalized_output = os.path.normpath(result.stdout.strip()) |
| 51 | + normalized_project_dir = os.path.normpath(project_dir) |
| 52 | + assert normalized_output == normalized_project_dir |
| 53 | + |
| 54 | + |
| 55 | +def test_run_command_from_project_root(project_dir): |
| 56 | + """Test running a command from the project root.""" |
| 57 | + result = subprocess.run( |
| 58 | + [sys.executable, "-m", "codemcp", "run", "echo", "--path", project_dir], |
| 59 | + capture_output=True, |
| 60 | + text=True, |
| 61 | + check=True, |
| 62 | + ) |
| 63 | + |
| 64 | + assert "Hello World" in result.stdout |
0 commit comments