kin: auto-commit after pipeline
This commit is contained in:
parent
61212b6605
commit
1e8253d6e8
2 changed files with 349 additions and 0 deletions
|
|
@ -5,14 +5,27 @@ Root cause: Makefile test target used bare `pytest tests/` which resolves to the
|
|||
system pytest (Python 3.14). Tests written for Python 3.11 could fail under 3.14.
|
||||
|
||||
Fix: Makefile test target changed to `python3.11 -m pytest tests/`.
|
||||
PR fix: _detect_test_command() now returns sys.executable -m pytest for
|
||||
pyproject.toml/setup.py projects (was bare 'pytest').
|
||||
|
||||
Coverage:
|
||||
(1) Makefile test target uses python3.11, not bare pytest
|
||||
(2) _detect_test_command still returns 'make test' for projects with Makefile
|
||||
(3) _detect_test_command returns sys.executable -m pytest for pyproject.toml/setup.py
|
||||
(4) Pipeline auto-test uses project.test_command when explicitly set
|
||||
(5) Pipeline auto-test uses _detect_test_command when project.test_command is NULL
|
||||
(6) PATCH /api/projects: test_command absent → DB field unchanged (decision #580)
|
||||
(7) Regression: pipeline auto-test early return (no test framework) does not crash
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
from core.db import init_db
|
||||
from core import models
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -73,3 +86,177 @@ class TestDetectTestCommandUnchanged:
|
|||
assert result == "make test", (
|
||||
f"kin project should detect 'make test', got {result!r}"
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# (3) _detect_test_command: pyproject.toml/setup.py → sys.executable -m pytest
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestDetectTestCommandSysExecutable:
|
||||
def test_detect_pyproject_returns_sys_executable_pytest(self, tmp_path):
|
||||
"""pyproject.toml project must get sys.executable -m pytest, not bare pytest."""
|
||||
from agents.runner import _detect_test_command
|
||||
|
||||
(tmp_path / "pyproject.toml").write_text("[build-system]\n")
|
||||
result = _detect_test_command(str(tmp_path))
|
||||
assert result == f"{sys.executable} -m pytest"
|
||||
|
||||
def test_detect_setup_py_returns_sys_executable_pytest(self, tmp_path):
|
||||
"""setup.py project must get sys.executable -m pytest, not bare pytest."""
|
||||
from agents.runner import _detect_test_command
|
||||
|
||||
(tmp_path / "setup.py").write_text("from setuptools import setup\nsetup()\n")
|
||||
result = _detect_test_command(str(tmp_path))
|
||||
assert result == f"{sys.executable} -m pytest"
|
||||
|
||||
def test_detect_pyproject_not_bare_pytest(self, tmp_path):
|
||||
"""Regression: _detect_test_command must not return bare 'pytest' for pyproject.toml."""
|
||||
from agents.runner import _detect_test_command
|
||||
|
||||
(tmp_path / "pyproject.toml").write_text("[build-system]\n")
|
||||
result = _detect_test_command(str(tmp_path))
|
||||
assert result != "pytest", (
|
||||
"Must not use bare 'pytest' — would resolve to wrong Python version"
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Shared fixture for pipeline auto-test tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@pytest.fixture
|
||||
def conn_kin109():
|
||||
c = init_db(":memory:")
|
||||
models.create_project(c, "proj", "Proj", "/tmp/proj", tech_stack=["python"])
|
||||
models.create_task(c, "PROJ-001", "proj", "Fix bug", brief={"route_type": "debug"})
|
||||
yield c
|
||||
c.close()
|
||||
|
||||
|
||||
def _claude_mock_success():
|
||||
m = MagicMock()
|
||||
m.stdout = '{"result": "done", "usage": {"total_tokens": 100}, "cost_usd": 0.001}'
|
||||
m.stderr = ""
|
||||
m.returncode = 0
|
||||
return m
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# (4/5) Pipeline auto-test: project.test_command priority vs auto-detect
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAutoTestCommandPriority:
|
||||
@patch("agents.runner._run_project_tests")
|
||||
@patch("agents.runner.subprocess.run")
|
||||
def test_uses_project_test_command_when_set(self, mock_run, mock_tests, conn_kin109, tmp_path):
|
||||
"""When project.test_command is set, pipeline auto-test must use that command."""
|
||||
mock_run.return_value = _claude_mock_success()
|
||||
mock_tests.return_value = {"success": True, "output": "ok", "returncode": 0}
|
||||
|
||||
(tmp_path / "pyproject.toml").write_text("[build-system]\n")
|
||||
models.update_project(
|
||||
conn_kin109, "proj",
|
||||
test_command="my_custom_runner",
|
||||
auto_test_enabled=True,
|
||||
path=str(tmp_path),
|
||||
)
|
||||
|
||||
from agents.runner import run_pipeline
|
||||
run_pipeline(conn_kin109, "PROJ-001", [{"role": "backend_dev", "brief": "fix it"}])
|
||||
|
||||
mock_tests.assert_called_once()
|
||||
assert mock_tests.call_args.args[1] == "my_custom_runner"
|
||||
|
||||
@patch("agents.runner._run_project_tests")
|
||||
@patch("agents.runner.subprocess.run")
|
||||
def test_uses_detect_when_test_command_null(self, mock_run, mock_tests, conn_kin109, tmp_path):
|
||||
"""When project.test_command is NULL, pipeline auto-test uses _detect_test_command result."""
|
||||
mock_run.return_value = _claude_mock_success()
|
||||
mock_tests.return_value = {"success": True, "output": "ok", "returncode": 0}
|
||||
|
||||
(tmp_path / "pyproject.toml").write_text("[build-system]\n")
|
||||
models.update_project(
|
||||
conn_kin109, "proj",
|
||||
test_command=None,
|
||||
auto_test_enabled=True,
|
||||
path=str(tmp_path),
|
||||
)
|
||||
|
||||
from agents.runner import run_pipeline
|
||||
run_pipeline(conn_kin109, "PROJ-001", [{"role": "backend_dev", "brief": "fix it"}])
|
||||
|
||||
mock_tests.assert_called_once()
|
||||
assert mock_tests.call_args.args[1] == f"{sys.executable} -m pytest"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# (6) PATCH /api/projects: test_command absent → DB field unchanged (decision #580)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestPatchProjectTestCommand:
|
||||
@pytest.fixture
|
||||
def client(self, tmp_path):
|
||||
import web.api as api_module
|
||||
api_module.DB_PATH = tmp_path / "test.db"
|
||||
from fastapi.testclient import TestClient
|
||||
from web.api import app
|
||||
c = TestClient(app)
|
||||
c.post("/api/projects", json={"id": "px", "name": "PX", "path": "/px"})
|
||||
# Pre-set a test_command value
|
||||
c.patch("/api/projects/px", json={"test_command": "existing_runner"})
|
||||
return c
|
||||
|
||||
def test_patch_without_test_command_does_not_change_db(self, client):
|
||||
"""PATCH without test_command field must NOT overwrite the existing value."""
|
||||
r = client.patch("/api/projects/px", json={"auto_test_enabled": False})
|
||||
assert r.status_code == 200
|
||||
|
||||
r2 = client.get("/api/projects/px")
|
||||
assert r2.status_code == 200
|
||||
assert r2.json()["test_command"] == "existing_runner"
|
||||
|
||||
def test_patch_with_test_command_updates_db(self, client):
|
||||
"""PATCH with test_command sets the new value in DB."""
|
||||
r = client.patch("/api/projects/px", json={"test_command": "new_runner"})
|
||||
assert r.status_code == 200
|
||||
|
||||
r2 = client.get("/api/projects/px")
|
||||
assert r2.json()["test_command"] == "new_runner"
|
||||
|
||||
def test_patch_with_empty_test_command_stores_null(self, client):
|
||||
"""PATCH with test_command='' clears it to NULL (enables auto-detect)."""
|
||||
r = client.patch("/api/projects/px", json={"test_command": ""})
|
||||
assert r.status_code == 200
|
||||
|
||||
r2 = client.get("/api/projects/px")
|
||||
assert r2.json()["test_command"] is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# (7) Regression: auto-test early return (no framework detected) does not crash
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
class TestAutoTestEarlyReturn:
|
||||
@patch("agents.runner.subprocess.run")
|
||||
def test_pipeline_skips_auto_test_when_no_framework_detected(self, mock_run, conn_kin109, tmp_path):
|
||||
"""When no test framework is detected and test_command is NULL, pipeline must
|
||||
skip auto-test and return success — not crash."""
|
||||
mock_run.return_value = _claude_mock_success()
|
||||
|
||||
# tmp_path has no Makefile, pyproject.toml, package.json, setup.py, tsconfig.json
|
||||
# → _detect_test_command returns None → auto-test skipped
|
||||
models.update_project(
|
||||
conn_kin109, "proj",
|
||||
test_command=None,
|
||||
auto_test_enabled=True,
|
||||
path=str(tmp_path),
|
||||
)
|
||||
|
||||
from agents.runner import run_pipeline
|
||||
result = run_pipeline(conn_kin109, "PROJ-001", [{"role": "backend_dev", "brief": "fix"}])
|
||||
|
||||
assert isinstance(result, dict)
|
||||
assert result["success"] is True
|
||||
# The skipped auto-test step must be in results
|
||||
skipped = [r for r in result["results"] if r.get("_skipped") and r.get("_project_test")]
|
||||
assert len(skipped) == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue