Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ cwm deactivate
| Command | Description |
|---------|-------------|
| `cwm ws build [--dry-run] [--no-rdeps]` | Build changed packages + their ABI reverse deps (`build`/`build_export`; `exec`-only excluded) in the active worktree |
| `cwm ws test [-w BRANCH] [--dry-run] [--no-rdeps]` | Run `colcon test` on changed packages + their ABI reverse deps (underlay+overlay sourced) |
| `cwm ws test-result [-w BRANCH]` | Show the test summary; exits non-zero if any test failed (`--return-code-on-test-failure`) |
| `cwm ws clean [--all]` | Clean build artifacts |
| `cwm ws status [--json]` | Show the state of the base workspace and all worktrees |

Expand Down
110 changes: 110 additions & 0 deletions src/cwm/cli/_workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""Shared helpers for diff-based workspace subcommands (``ws build`` / ``ws test``)."""

from __future__ import annotations

import os
from collections.abc import Callable
from pathlib import Path

import click

from cwm.core.cdc import ColconDiscoveryController
from cwm.core.changeset import Changeset, compute_changeset
from cwm.core.config import Config
from cwm.errors import CWMError, NotActivatedError
from cwm.util.colcon_runner import run_colcon, run_colcon_sourced
from cwm.util.fs import find_project_root


def resolve_worktree(worktree_branch: str | None, *, command: str) -> tuple[str, Path, Config]:
"""Resolve (branch, workspace_path, config) from the -w flag or active env vars.

With *worktree_branch* set, the workspace is resolved from config. Otherwise
the current subshell's ``CWM_WORKTREE`` / ``CWM_WORKSPACE`` (set by
``cwm activate``) are used. *command* (e.g. "ws build", "ws test") is
interpolated into the NotActivatedError hint so each subcommand prints an
accurate message.
"""
root = find_project_root()
config = Config.load(root)

if worktree_branch:
ws_path = config.worktree_ws_path(worktree_branch)
if not ws_path.exists():
raise CWMError(
f"Worktree workspace not found: {ws_path}\n"
f"Create it first with: cwm worktree add {worktree_branch}"
)
return worktree_branch, ws_path, config

branch = os.environ.get("CWM_WORKTREE")
ws_str = os.environ.get("CWM_WORKSPACE")
if not branch or not ws_str:
raise NotActivatedError(
f"cwm {command} requires an active CWM workspace or the -w/--worktree flag.\n"
" Activate: source <(cwm activate <branch>)\n"
f" Or: cwm {command} -w <branch>"
)
return branch, Path(ws_str), config


def run_workspace_colcon(
subcommand: str,
*,
worktree_branch: str | None,
no_rdeps: bool,
dry_run: bool,
colcon_args: tuple[str, ...],
generate_args: Callable[[ColconDiscoveryController, Changeset, Config], list[str]],
show_build_order: bool = False,
done_message: str,
) -> None:
"""Run a diff-based ``colcon <subcommand>`` over a worktree's change set.

Resolves the target worktree, scans for changed packages (plus ABI reverse
deps unless *no_rdeps*), delegates colcon argument generation to
*generate_args*, and either prints the command (*dry_run*) or runs it —
inheriting the active environment, or sourcing underlay+overlay for ``-w``.
"""
branch, ws_path, config = resolve_worktree(worktree_branch, command=f"ws {subcommand}")
src_path = config.worktree_src_path(branch)

click.echo("Scanning packages...")
cs = compute_changeset(config, branch, no_rdeps=no_rdeps)
click.echo(f" Found {cs.package_count} packages")

click.echo("Detecting changes...")
if not cs.changed:
click.echo(f"No changed packages detected. Nothing to {subcommand}.")
return

click.echo(f" Changed: {', '.join(sorted(cs.changed))}")
if cs.affected:
click.echo(f" Affected (reverse deps): {', '.join(sorted(cs.affected))}")
if show_build_order:
click.echo(f" Build order: {' -> '.join(cs.build_order)}")

cdc = ColconDiscoveryController(src_path)
colcon_extra = generate_args(cdc, cs, config)
colcon_extra.extend(colcon_args)

if dry_run:
click.echo()
click.echo("Dry run - would execute:")
click.echo(f" cd {ws_path}")
click.echo(f" colcon {subcommand} {' '.join(colcon_extra)}")
return

click.echo()
if not worktree_branch:
run_colcon(subcommand, ws_path, colcon_extra)
else:
run_colcon_sourced(
subcommand,
ws_path,
config.base_install_path,
config.worktree_install_path(branch),
colcon_extra,
)
click.echo()
click.echo(done_message)
94 changes: 13 additions & 81 deletions src/cwm/cli/build_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,12 @@

from __future__ import annotations

import os
from pathlib import Path

import click

from cwm.cli._workspace import run_workspace_colcon
from cwm.cli.completion import complete_worktree_branches
from cwm.cli.main import ws
from cwm.core.cdc import ColconDiscoveryController
from cwm.core.changeset import compute_changeset
from cwm.core.config import Config
from cwm.errors import CWMError, NotActivatedError
from cwm.util.colcon_runner import run_colcon_build, run_colcon_build_sourced
from cwm.util.fs import find_project_root


def _resolve_worktree(worktree_branch: str | None) -> tuple[str, Path, Config]:
"""Resolve branch and workspace path from -w flag or current subshell env vars.

Returns (branch, workspace_path, config).
"""
root = find_project_root()
config = Config.load(root)

if worktree_branch:
ws_path = config.worktree_ws_path(worktree_branch)
if not ws_path.exists():
raise CWMError(
f"Worktree workspace not found: {ws_path}\n"
f"Create it first with: cwm worktree add {worktree_branch}"
)
return worktree_branch, ws_path, config

branch = os.environ.get("CWM_WORKTREE")
ws_str = os.environ.get("CWM_WORKSPACE")
if not branch or not ws_str:
raise NotActivatedError(
"cwm ws build requires an active CWM workspace or the -w/--worktree flag.\n"
" Activate: source <(cwm activate <branch>)\n"
" Or: cwm ws build -w <branch>"
)
return branch, Path(ws_str), config
from cwm.errors import CWMError


@ws.command(context_settings={"ignore_unknown_options": True, "allow_extra_args": True})
Expand Down Expand Up @@ -70,50 +35,17 @@ def build(worktree_branch: str | None, dry_run: bool, no_rdeps: bool, colcon_arg
or with -w/--worktree. Any extra arguments after ``--`` are forwarded to colcon build.
"""
try:
branch, ws_path, config = _resolve_worktree(worktree_branch)
src_path = config.worktree_src_path(branch)

click.echo("Scanning packages...")
cs = compute_changeset(config, branch, no_rdeps=no_rdeps)
click.echo(f" Found {cs.package_count} packages")

click.echo("Detecting changes...")
if not cs.changed:
click.echo("No changed packages detected. Nothing to build.")
return

click.echo(f" Changed: {', '.join(sorted(cs.changed))}")
if cs.affected:
click.echo(f" Affected (reverse deps): {', '.join(sorted(cs.affected))}")
click.echo(f" Build order: {' -> '.join(cs.build_order)}")

cdc = ColconDiscoveryController(src_path)
colcon_extra = cdc.generate_build_args(
cs.changed,
cs.affected,
symlink_install=config.symlink_install,
run_workspace_colcon(
"build",
worktree_branch=worktree_branch,
no_rdeps=no_rdeps,
dry_run=dry_run,
colcon_args=colcon_args,
generate_args=lambda cdc, cs, config: cdc.generate_build_args(
cs.changed, cs.affected, symlink_install=config.symlink_install
),
show_build_order=True,
done_message="Build complete.",
)
colcon_extra.extend(colcon_args)

if dry_run:
click.echo()
click.echo("Dry run - would execute:")
click.echo(f" cd {ws_path}")
click.echo(f" colcon build {' '.join(colcon_extra)}")
return

click.echo()
if not worktree_branch:
run_colcon_build(ws_path, colcon_extra)
else:
run_colcon_build_sourced(
ws_path,
underlay_install=config.base_install_path,
overlay_install=config.worktree_install_path(branch),
extra_args=colcon_extra,
)
click.echo()
click.echo("Build complete.")

except CWMError as exc:
raise click.ClickException(str(exc)) from exc
3 changes: 2 additions & 1 deletion src/cwm/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def ws() -> None:
"""Manage workspace operations."""


ws.command_order = ["build", "clean", "status"]
ws.command_order = ["build", "test", "test-result", "clean", "status"]


@cli.group()
Expand Down Expand Up @@ -186,6 +186,7 @@ def base() -> None:
import cwm.cli.worktree_cmd # noqa: E402, F401
import cwm.cli.repo_cmd # noqa: E402, F401
import cwm.cli.build_cmd # noqa: E402, F401
import cwm.cli.test_cmd # noqa: E402, F401
import cwm.cli.clean_cmd # noqa: E402, F401
import cwm.cli.status_cmd # noqa: E402, F401
import cwm.cli.env_cmd # noqa: E402, F401
Expand Down
80 changes: 80 additions & 0 deletions src/cwm/cli/test_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""cwm ws test / cwm ws test-result - smart diff-based colcon test."""

from __future__ import annotations

import click

from cwm.cli._workspace import resolve_worktree, run_workspace_colcon
from cwm.cli.completion import complete_worktree_branches
from cwm.cli.main import ws
from cwm.errors import CWMError
from cwm.util.colcon_runner import run_colcon_test_result


@ws.command(context_settings={"ignore_unknown_options": True, "allow_extra_args": True})
@click.option(
"-w", "--worktree",
"worktree_branch",
default=None,
metavar="BRANCH",
shell_complete=complete_worktree_branches,
help="Test the given worktree without entering a subshell.",
)
@click.option("--dry-run", is_flag=True, help="Show the colcon command without executing.")
@click.option(
"--no-rdeps",
is_flag=True,
help="Skip reverse dependency analysis (test changed packages only).",
)
# shell_complete suppresses Click's fallback to filesystem completion (compopt -o default),
# which would otherwise surface the workspace's build/ directory as a tab-completion candidate.
@click.argument("colcon_args", nargs=-1, type=click.UNPROCESSED, shell_complete=lambda ctx, param, incomplete: [])
def test(worktree_branch: str | None, dry_run: bool, no_rdeps: bool, colcon_args: tuple[str, ...]) -> None:
"""Run tests for changed packages and their reverse dependencies.

Must be run with an active workspace (source <(cwm activate <branch>))
or with -w/--worktree. Any extra arguments are forwarded to colcon test.

Note: ``colcon test`` exits 0 even when tests fail; run ``cwm ws test-result``
afterwards to get the pass/fail summary and a meaningful exit code.
"""
try:
run_workspace_colcon(
"test",
worktree_branch=worktree_branch,
no_rdeps=no_rdeps,
dry_run=dry_run,
colcon_args=colcon_args,
generate_args=lambda cdc, cs, _config: cdc.generate_test_args(cs.changed, cs.affected),
done_message="Tests finished. Run 'cwm ws test-result' to view the summary and pass/fail status.",
)
except CWMError as exc:
raise click.ClickException(str(exc)) from exc


@ws.command(
name="test-result",
context_settings={"ignore_unknown_options": True, "allow_extra_args": True},
)
@click.option(
"-w", "--worktree",
"worktree_branch",
default=None,
metavar="BRANCH",
shell_complete=complete_worktree_branches,
help="Inspect the given worktree without entering a subshell.",
)
@click.argument("colcon_args", nargs=-1, type=click.UNPROCESSED, shell_complete=lambda ctx, param, incomplete: [])
def test_result(worktree_branch: str | None, colcon_args: tuple[str, ...]) -> None:
"""Show the test result summary and exit non-zero if any test failed.

Runs ``colcon test-result --all --return-code-on-test-failure`` so that
agents and CI can detect real test failures (which ``colcon test`` hides
behind a zero exit code). Reads the build artifacts only, so no environment
sourcing is required.
"""
try:
_branch, ws_path, _config = resolve_worktree(worktree_branch, command="ws test-result")
run_colcon_test_result(ws_path, list(colcon_args))
except CWMError as exc:
raise click.ClickException(str(exc)) from exc
16 changes: 16 additions & 0 deletions src/cwm/core/cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ def generate_build_args(
args.append("--symlink-install")
return args

def generate_test_args(
self,
changed_pkgs: set[str],
affected_pkgs: set[str],
) -> list[str]:
"""Generate colcon test arguments for the changed+affected package set.

Unlike :meth:`generate_build_args`, ``colcon test`` has no
``--allow-overriding`` / ``--symlink-install``; only ``--packages-select``
applies.
"""
all_pkgs = sorted(changed_pkgs | affected_pkgs)
if not all_pkgs:
return []
return ["--packages-select", *all_pkgs]

# -- COLCON_IGNORE fallback ------------------------------------------------

def place_ignore_markers(self, dga: DependencyGraphAnalyzer, keep: set[str]) -> list[Path]:
Expand Down
16 changes: 16 additions & 0 deletions src/cwm/util/colcon_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ def run_colcon_test(
) -> int:
"""Execute ``colcon test`` in *workspace* and stream output."""
return run_colcon("test", workspace, extra_args, env=env)


def run_colcon_test_result(
workspace: Path,
extra_args: list[str] | None = None,
*,
env: dict[str, str] | None = None,
) -> int:
"""Run ``colcon test-result --all --return-code-on-test-failure`` in *workspace*.

``colcon test`` exits 0 even when individual tests fail; driving the exit
code from ``test-result`` lets agents/CI detect the true pass/fail status
(non-zero exit raises ColconError).
"""
args = ["--all", "--return-code-on-test-failure", *(extra_args or [])]
return run_colcon("test-result", workspace, args, env=env)
14 changes: 14 additions & 0 deletions tests/unit/test_cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ def test_empty_returns_empty(self) -> None:
assert args == []


class TestGenerateTestArgs:
def test_packages_select_only(self) -> None:
cdc = ColconDiscoveryController(Path("/fake"))
args = cdc.generate_test_args({"core_lib"}, {"perception_node"})
assert args == ["--packages-select", "core_lib", "perception_node"]
# test has no build-only flags
assert "--allow-overriding" not in args
assert "--symlink-install" not in args

def test_empty_returns_empty(self) -> None:
cdc = ColconDiscoveryController(Path("/fake"))
assert cdc.generate_test_args(set(), set()) == []


class TestIgnoreMarkers:
def test_place_and_remove_markers(self, sample_ws: Path) -> None:
dga = DependencyGraphAnalyzer()
Expand Down
Loading
Loading