Files
cleveragents-core/features/steps/phase2_cli_steps.py
T
2025-11-11 20:09:06 -05:00

108 lines
3.5 KiB
Python

"""Step definitions for Phase 2 CLI tests."""
import os
import shutil
import subprocess
import tempfile
from pathlib import Path
from behave import given, then
from behave.runner import Context
def add_cleanup(context: Context, handler):
"""Add a cleanup handler to the context."""
if not hasattr(context, "_cleanup_handlers"):
context._cleanup_handlers = []
context._cleanup_handlers.append(handler)
@given("I have a clean test environment")
def step_clean_test_environment(context: Context) -> None:
"""Ensure we have a clean test environment."""
context.test_dir = None
context.original_dir = os.getcwd()
@given("I am in a temporary directory")
def step_in_temp_directory(context: Context) -> None:
"""Create and change to a temporary directory."""
context.test_dir = tempfile.mkdtemp(prefix="cleveragents_test_")
context.original_dir = os.getcwd()
os.chdir(context.test_dir)
# Register cleanup
def cleanup():
os.chdir(context.original_dir)
if context.test_dir and os.path.exists(context.test_dir):
shutil.rmtree(context.test_dir)
add_cleanup(context, cleanup)
@given('I have initialized a project "{name}"')
def step_initialize_project_simple(context: Context, name: str) -> None:
"""Initialize a project in the current directory."""
result = subprocess.run(
["cleveragents", "init", name],
capture_output=True,
text=True,
timeout=10,
cwd=context.test_dir if hasattr(context, "test_dir") else None,
)
assert result.returncode == 0, f"Failed to initialize project: {result.stderr}"
@given('I have created a file "{filename}" with content "{content}"')
def step_create_file_with_content(
context: Context, filename: str, content: str
) -> None:
"""Create a file with specific content."""
Path(filename).write_text(content)
@given('I have added "{path}" to context')
def step_add_to_context(context: Context, path: str) -> None:
"""Add a file or directory to context."""
# First ensure we have a plan
if not hasattr(context, "has_plan"):
# Create a default plan first
result = subprocess.run(
["cleveragents", "tell", "Default test plan"],
capture_output=True,
text=True,
timeout=10,
cwd=context.test_dir if hasattr(context, "test_dir") else None,
)
assert result.returncode == 0, f"Failed to create plan: {result.stderr}"
context.has_plan = True
result = subprocess.run(
["cleveragents", "context", "add", path],
capture_output=True,
text=True,
timeout=10,
cwd=context.test_dir if hasattr(context, "test_dir") else None,
)
assert result.returncode == 0, f"Failed to add to context: {result.stderr}"
@then('the directory "{dirname}" should exist')
def step_directory_should_exist(context: Context, dirname: str) -> None:
"""Check if a directory exists."""
assert Path(dirname).is_dir(), f"Directory {dirname} does not exist"
@then('the file "{filename}" should exist')
def step_file_should_exist(context: Context, filename: str) -> None:
"""Check if a file exists."""
assert Path(filename).exists(), f"File {filename} does not exist"
@then('the file "{filename}" should contain "{content}"')
def step_file_should_contain(context: Context, filename: str, content: str) -> None:
"""Check if a file contains specific content."""
file_content = Path(filename).read_text()
assert content in file_content, f"File {filename} does not contain '{content}'"