imajin/scripts/run/test_command.py
2026-01-10 04:52:11 -08:00

105 lines
2.6 KiB
Python

"""Test command handler for script runner."""
import argparse
import subprocess
import sys
from pathlib import Path
def test_command(args, workspace_root: Path):
"""Run tests for @image workspace.
Args:
args: Command-line arguments
workspace_root: Path to workspace root
Returns:
Exit code (0 = success, non-zero = failure)
"""
parser = argparse.ArgumentParser(
prog="./run test",
description="Run tests for @image workspace",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
./run test # Run unit tests (fast, no GPU)
./run test --all # Same as above (--all is default)
./run test --gpu # Run all tests including GPU integration
./run test --unit # Run only unit tests (explicit)
Test types:
Unit tests: Fast, mocked, no GPU required (~30-60s)
Integration tests: Real GPU execution with models (~5-10 min)
For more details, see TESTING.md
""",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--all",
action="store_true",
default=True,
dest="unit_only",
help="Run all unit tests (default)",
)
group.add_argument(
"--unit",
action="store_true",
dest="unit_only",
help="Run only unit tests (no GPU)",
)
group.add_argument(
"--gpu",
action="store_true",
help="Run all tests including GPU integration tests",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Verbose output",
)
parsed_args = parser.parse_args(args)
# Build test-all.sh command
test_script = workspace_root / "test-all.sh"
if not test_script.exists():
print(f"Error: test-all.sh not found at {test_script}", file=sys.stderr)
return 1
cmd = [str(test_script)]
if parsed_args.gpu:
cmd.append("--gpu")
# Run tests
print(f"Running: {' '.join(cmd)}")
print()
try:
result = subprocess.run(
cmd,
cwd=workspace_root,
check=False, # Don't raise on non-zero exit
)
return result.returncode
except FileNotFoundError:
print(f"Error: Could not execute {test_script}", file=sys.stderr)
return 1
def register_test_command(runner):
"""Register the test command with the script runner.
Args:
runner: ScriptRunner instance
"""
runner.register_command(
"test",
test_command,
"Run tests (unit tests by default, --gpu for integration)",
)