diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d3b2a63c..7a62202ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,10 @@ when its branch is busy at the moment it is sent on. It stays waived with its reason and is resumed on a later pass once the branch is free. +- The red check runs a uv workspace member's new tests in the workspace environment on the + lock as it is, so a member whose dependencies are installed only there is no longer + rejected for pytest failing to start. + - A tick no longer builds its own resolution lane or waits for a run to end. The slot sample and the restack gauges count the runs that resolve workers have recorded, and the runs owed beyond a project's cap as waiting. diff --git a/docs/toolchain-profiles.md b/docs/toolchain-profiles.md index d5704bf84..4d6ff2f05 100644 --- a/docs/toolchain-profiles.md +++ b/docs/toolchain-profiles.md @@ -87,7 +87,7 @@ What it runs, exactly: | `lint_command_all_files()` | `uv run pre-commit run --all-files` — unit-test uses it only for a unit with no commits of its own | | `clean_command()` | `SKIP=pyrefly-check uv run pre-commit run --all-files` — type checking skipped: a test importing what does not exist yet is the expected state at the tests commit | | `fix_command(files)` | `uv run pre-commit run --files `, every file given | -| `red_command(files, ids=None)` | `uv run pytest -p no:cacheprovider --tb=line -q` (ids as `path::name`); on pytest's usage error (exit 4, `usage_error_exit`: an id it did not collect) the files run whole in its place, and that run's report and status stand; selection by id does not reach a test marked `integration` (a later change ships that) | +| `red_command(files, ids=None, project=None)` | `uv run pytest -p no:cacheprovider --tb=line -q` (ids as `path::name`); on pytest's usage error (exit 4, `usage_error_exit`: an id it did not collect) the files run whole in its place, and that run's report and status stand; selection by id does not reach a test marked `integration` (a later change ships that). For a project that a workspace root above it (within the checkout, with a `uv.lock`) lists under `tool.uv.workspace.members`, both runs use `uv run --all-packages --frozen` instead of `uv run`: the workspace environment on the lock as committed | | `test_commands`, single-package repo | `uv run pytest -q` if `tests/` exists, else nothing. A repo that declares `pytest-xdist` (project dependencies, extras, dependency groups) gets one parallel command instead: `uv run pytest -n auto --maxprocesses=8 -v` (`-v`, not `-q`, so xdist prints each worker's progress lines, the only record of the order a worker ran its tests in; `-n` and `--maxprocesses` are the repo's `tests.workers` and `tests.max_workers`; these are their defaults). It adds no `-m`, so the repo's own `addopts` selection stands, and a test that cannot run beside others is isolated, not marked. A repo without xdist keeps the plain command, since `-n` would be rejected there | | `test_commands`, workspace | per chosen member with a `tests/` dir: `uv run --package --isolated pytest -q` (the one parallel command above instead, when the member's own `pyproject.toml` declares `pytest-xdist`). Chosen: the members a changed path is under — or **every** member when a non-`.md` path outside all members changed (the root `pyproject.toml` is where a marker gets registered). Plus, when such an outside path or `tests/` changed and a repo-root `tests/` exists: `uv run --no-project --isolated --with pytest [--with ...] pytest tests -q` (the one parallel command above instead, when `root_extras` names `pytest-xdist`) | | `test_commands_all(repo, root_extras)` | every testable member plus the root `tests/`, whatever changed — unit-test uses it only for a unit with no commits of its own | diff --git a/src/agent_build_kit/pipeline/gate.py b/src/agent_build_kit/pipeline/gate.py index 953186046..63e97559f 100644 --- a/src/agent_build_kit/pipeline/gate.py +++ b/src/agent_build_kit/pipeline/gate.py @@ -96,7 +96,7 @@ def _red_command( return None ids = ids_for(touched, repo, repo / prefix, toolchain) if not ids: - return toolchain.red_command(owned) + return toolchain.red_command(owned, project=repo / prefix) chosen = {file: [i for i in ids if i.split("::")[0] == file] for file in owned} return toolchain.red_command( owned, [i for file in owned for i in chosen[file] or [file]], project=repo / prefix diff --git a/src/agent_build_kit/profiles/python_uv.py b/src/agent_build_kit/profiles/python_uv.py index 54e56651d..61024e203 100644 --- a/src/agent_build_kit/profiles/python_uv.py +++ b/src/agent_build_kit/profiles/python_uv.py @@ -184,6 +184,13 @@ def _lock_of(project: Path) -> Path | None: return workspace_lock(project, (LOCK,), _members_of) +def _is_workspace_member(project: Path) -> bool: + """Whether the lock the project uses is a workspace root's above it: the root's own project, + a folder the root does not list and a workspace with no lock are not members.""" + lock = _lock_of(project) + return lock is not None and lock.parent != project.resolve() + + def _path_sources(root: Path) -> list[Path]: """The checkouts the manifest names as path sources, that exist.""" sources = _pyproject(root).get("tool", {}).get("uv", {}).get("sources", {}) @@ -704,8 +711,11 @@ def red_command( options = "-p no:cacheprovider --tb=line -q" selection = by_id_selection(project) if ids else "" by_id = f" -m {shlex.quote(selection)}" if selection else "" - pytest = f"uv run pytest {shlex.join(ids or files)} {options}{by_id}" - whole = f"uv run pytest {' '.join(files)} {options}" if ids else None + # A workspace member runs in the workspace environment, on the lock as it is. + member = project is not None and _is_workspace_member(project) + uv = "uv run --all-packages --frozen" if member else "uv run" + pytest = f"{uv} pytest {shlex.join(ids or files)} {options}{by_id}" + whole = f"{uv} pytest {' '.join(files)} {options}" if ids else None return wrap_report_command( pytest, "--junitxml", retry=whole, retry_on=self.usage_error_exit ) diff --git a/tests/pipeline/test_red_check_runnable_files.py b/tests/pipeline/test_red_check_runnable_files.py index 91e834acf..6e9798223 100644 --- a/tests/pipeline/test_red_check_runnable_files.py +++ b/tests/pipeline/test_red_check_runnable_files.py @@ -11,6 +11,8 @@ import pytest +from agent_build_kit import profiles +from agent_build_kit.config import ProjectConfig from agent_build_kit.pipeline.gate import check_branch, red_problems from agent_build_kit.profiles.base import ToolchainProfile from agent_build_kit.profiles.node_npm import PROFILE as NODE @@ -248,3 +250,70 @@ def test_the_push_gate_prepares_the_worktree_once_before_its_clean_and_red_comma assert project == "." assert has_tests, "prepared after the tests commit was checked out" assert profile.outputs == ["yes\n"], "the marker existed when the red command ran" + + +class MemberAware(Recording): + """A profile whose run of a project exits 2 with `Failed to spawn: pytest` unless the real + red command it is given for that project differs from the project-less one (a member's + command), in which case the run fails for the missing behaviour.""" + + name = "member-aware" + + def red_command( + self, files: list[str], ids: list[str] | None = None, *, project: Path | None = None + ) -> str: + super().red_command(files, ids, project=project) + super().red_command(files, ids) + built, plain = self.commands[-2:] + if built != plain: + return "echo 'FAILED tests/test_new.py::test_new - assert False'; exit 1" + return "echo 'error: Failed to spawn: pytest' >&2; exit 2" + + def interpret_red( + self, output: str, exit_code: int, *, stubbed: bool = False + ) -> tuple[bool, list[str]]: + if exit_code == 1 and "Failed to spawn" not in output: + return True, [] + return False, ["no tests ran, so nothing was shown to fail"] + + +MEMBER_TEST = "pkg/core/tests/test_new.py" + + +@pytest.mark.parametrize( + ("already", "changed"), + [ + pytest.param({}, FAILING, id="a-new-test-is-selected-by-id"), + pytest.param( + {MEMBER_TEST: FAILING}, + "X = 1\n" + FAILING, + id="a-module-level-edit-runs-the-file-whole", + ), + ], +) +def test_the_red_check_over_a_workspace_members_new_test_is_judged_on_the_members_command( + repo: Path, already: dict[str, str], changed: str +) -> None: + commit( + repo, + "workspace", + { + "pyproject.toml": '[project]\nname = "r"\nversion = "0"\n' + '[tool.uv.workspace]\nmembers = ["pkg/core"]\n', + "uv.lock": "version = 1\n", + "pkg/core/pyproject.toml": '[project]\nname = "core"\nversion = "0"\n', + **already, + }, + ) + commit(repo, "test: changes one", {MEMBER_TEST: changed}) + profile = MemberAware(PYTHON) + + with profiles.registered(profile.toolchain): + problems = red_problems( + repo, + git(repo, "rev-parse", "HEAD"), + profile=profile.toolchain, + projects=[ProjectConfig(path="pkg/core", profile="member-aware")], + ) + + assert problems == [] diff --git a/tests/profiles/test_python_uv_red_command_ids.py b/tests/profiles/test_python_uv_red_command_ids.py index 19cfa5e88..51a24631b 100644 --- a/tests/profiles/test_python_uv_red_command_ids.py +++ b/tests/profiles/test_python_uv_red_command_ids.py @@ -9,6 +9,7 @@ import pytest from agent_build_kit.profiles.python_uv import PythonUvProfile +from tests.factories import init_repo FILES = ["tests/test_a.py", "tests/test_b.py"] IDS = ["tests/test_a.py::test_new", "tests/test_a.py::TestGroup::test_changed"] @@ -38,6 +39,85 @@ def test_without_ids_the_command_is_the_whole_file_run_and_with_them_it_is_not() assert profile.red_command(FILES, IDS) != whole +@pytest.fixture +def workspace(tmp_path: Path) -> Path: + """A uv workspace root listing `pkg/core` and the nested `apps/functions` as members, with + `pkg/other` a folder it does not list.""" + root = init_repo(tmp_path / "ws") + (root / "uv.lock").write_text("version = 1\n") + for folder in ("pkg/core", "apps/functions", "pkg/other"): + (root / folder).mkdir(parents=True) + (root / "pyproject.toml").write_text( + '[project]\nname = "root"\nversion = "0"\n' + '[tool.uv.workspace]\nmembers = ["pkg/core", "apps/functions"]\n' + ) + return root + + +@pytest.fixture +def plain(tmp_path: Path) -> Path: + """A project folder in a repository that declares no workspace.""" + root = init_repo(tmp_path / "plain") + (root / "pkg/core").mkdir(parents=True) + (root / "pyproject.toml").write_text('[project]\nname = "root"\nversion = "0"\n') + return root + + +@pytest.mark.parametrize("member", ["pkg/core", "apps/functions"]) +def test_a_workspace_members_red_command_runs_in_the_workspace_environment_on_the_lock_as_is( + workspace: Path, plain: Path, member: str +) -> None: + profile: Any = PythonUvProfile() + + command = profile.red_command(FILES, project=workspace / member) + + words = shlex.split(command) + assert "--all-packages" in words + assert "--frozen" in words + assert command != profile.red_command(FILES, project=plain / "pkg/core") + + +def test_a_member_of_a_workspace_with_no_lock_keeps_the_command_built_today( + workspace: Path, plain: Path +) -> None: + profile: Any = PythonUvProfile() + (workspace / "uv.lock").unlink() + + command = profile.red_command(FILES, project=workspace / "pkg/core") + + assert command == profile.red_command(FILES, project=plain / "pkg/core") + assert "--frozen" not in shlex.split(command) + + +@pytest.mark.parametrize("ids", [None, IDS]) +def test_a_project_that_is_not_a_member_keeps_the_command_built_today( + workspace: Path, plain: Path, ids: list[str] | None +) -> None: + profile: Any = PythonUvProfile() + today = profile.red_command(FILES, ids, project=plain / "pkg/core") + + assert profile.red_command(FILES, ids, project=workspace) == today, "the root's own project" + assert profile.red_command(FILES, ids, project=workspace / "pkg/other") == today + assert profile.red_command(FILES, ids, project=plain) == today + assert "--all-packages" not in shlex.split(today) + assert profile.red_command(FILES, ids, project=workspace / "pkg/core") != today, "a member" + + +def test_a_members_red_command_still_selects_by_id_retries_the_file_whole_and_prints_the_report( + workspace: Path, +) -> None: + profile: Any = PythonUvProfile() + + command = profile.red_command(FILES, IDS, project=workspace / "pkg/core") + + first, _, after = command.partition('if [ "$status" -eq 4 ]; then ') + assert "tests/test_a.py::test_new" in first + assert "tests/test_b.py" not in first + assert "tests/test_a.py tests/test_b.py" in after, "the retry runs the files whole" + assert "--all-packages" in after, "and in the workspace environment too" + assert "--junitxml" in command + + SPACED = "tests/a/test_x.py::test_y[no conversation found with session id sess-1]"