Files
cleveragents-core/features/steps/shell_asset_steps.py
T
2025-11-24 20:04:18 -05:00

246 lines
8.1 KiB
Python

"""Step definitions for shell asset extraction features."""
import json
from pathlib import Path
from behave import given, then, when
from cleveragents.discovery.shell_assets import ShellAssetExtractor
@given("the Plandex Go repository exists")
def step_given_plandex_exists(context):
"""Set up context for Plandex repository existence."""
# This is a precondition that should be satisfied for tests to run
# The actual Plandex repository should exist at ../plandex relative to the project root
pass
@given("the ShellAssetExtractor is initialized")
def step_init_shell_extractor(context):
"""Initialize the shell asset extractor."""
context.shell_extractor = ShellAssetExtractor()
@when("I run the shell asset extraction")
def step_run_shell_extraction(context):
"""Run the shell asset extraction."""
context.shell_catalog = context.shell_extractor.extract_all()
@when("I extract shell assets")
def step_extract_shell_assets(context):
"""Extract shell assets."""
context.shell_catalog = context.shell_extractor.extract_all()
@when("I save the catalog")
def step_save_catalog(context):
"""Save the catalog files."""
context.shell_extractor.save_catalog("build/test_output/shell")
context.output_dir = Path("build/test_output/shell")
@when("I save the catalog with wrappers")
def step_save_catalog_with_wrappers(context):
"""Save the catalog with Python wrappers."""
context.shell_extractor.save_catalog("build/test_output/shell")
context.output_dir = Path("build/test_output/shell")
@then("shell scripts should be discovered")
def step_verify_scripts_discovered(context):
"""Verify shell scripts were discovered."""
assert context.shell_catalog is not None
assert "scripts" in context.shell_catalog
assert len(context.shell_catalog["scripts"]) > 0
@then("each script should have metadata extracted")
def step_verify_metadata_extracted(context):
"""Verify metadata was extracted for each script."""
for script in context.shell_catalog["scripts"]:
assert "path" in script
assert "name" in script
assert "commands_used" in script
assert "environment_vars" in script
@then("the start_local.sh script should be found")
def step_verify_start_local_found(context):
"""Verify start_local.sh is found."""
scripts = context.shell_catalog["scripts"]
start_local = [s for s in scripts if s["name"] == "start_local.sh"]
assert len(start_local) > 0, "start_local.sh not found"
context.start_local_script = start_local[0]
@then("it should be marked as requiring Docker")
def step_verify_docker_required(context):
"""Verify script is marked as requiring Docker."""
assert context.start_local_script["requires_docker"] is True
@then("it should be marked as requiring Git")
def step_verify_git_required(context):
"""Verify script is marked as requiring Git."""
assert context.start_local_script["requires_git"] is True
@then("it should be suggested for Python replacement")
def step_verify_python_replacement(context):
"""Verify script is suggested for Python replacement."""
assert context.start_local_script["python_replacement_suggested"] is True
@then("environment variables should be identified")
def step_verify_env_vars_identified(context):
"""Verify environment variables are identified."""
assert "environment_map" in context.shell_catalog
assert len(context.shell_catalog["environment_map"]) > 0
@then("each variable should be mapped to its scripts")
def step_verify_var_script_mapping(context):
"""Verify each variable is mapped to scripts."""
env_map = context.shell_catalog["environment_map"]
for _var, scripts in env_map.items():
assert isinstance(scripts, list)
assert len(scripts) > 0
@then("scripts requiring Docker should be identified")
def step_verify_docker_scripts(context):
"""Verify scripts requiring Docker are identified."""
docker_scripts = [
s for s in context.shell_catalog["scripts"] if s["requires_docker"]
]
assert len(docker_scripts) > 0
@then("scripts requiring Git should be identified")
def step_verify_git_scripts(context):
"""Verify scripts requiring Git are identified."""
git_scripts = [s for s in context.shell_catalog["scripts"] if s["requires_git"]]
assert len(git_scripts) > 0
@then("command dependencies should be captured")
def step_verify_command_dependencies(context):
"""Verify command dependencies are captured."""
for script in context.shell_catalog["scripts"]:
assert isinstance(script["commands_used"], list)
@then("Python wrapper files should be created")
def step_verify_wrapper_files(context):
"""Verify Python wrapper files are created."""
wrappers_dir = context.output_dir / "shell_wrappers"
if wrappers_dir.exists():
wrapper_files = list(wrappers_dir.glob("*.py"))
assert len(wrapper_files) > 0
@then("wrappers should include documentation")
def step_verify_wrapper_docs(context):
"""Verify wrappers include documentation."""
wrappers_dir = context.output_dir / "shell_wrappers"
if wrappers_dir.exists():
for wrapper_file in wrappers_dir.glob("*.py"):
content = wrapper_file.read_text()
assert '"""' in content or "'''" in content
@then("wrappers should preserve environment variable handling")
def step_verify_wrapper_env_handling(context):
"""Verify wrappers preserve environment variable handling."""
wrappers_dir = context.output_dir / "shell_wrappers"
if wrappers_dir.exists():
for wrapper_file in wrappers_dir.glob("*.py"):
content = wrapper_file.read_text()
assert "env" in content
assert "os.environ" in content
@then("a YAML catalog file should be created")
def step_verify_yaml_catalog(context):
"""Verify YAML catalog file is created."""
yaml_file = context.output_dir / "shell_assets.yaml"
assert yaml_file.exists()
@then("a JSON catalog file should be created")
def step_verify_json_catalog(context):
"""Verify JSON catalog file is created."""
json_file = context.output_dir / "shell_assets.json"
assert json_file.exists()
@then("both catalog files should contain the same data")
def step_verify_catalog_consistency(context):
"""Verify YAML and JSON files contain the same data."""
import yaml
yaml_file = context.output_dir / "shell_assets.yaml"
json_file = context.output_dir / "shell_assets.json"
with open(yaml_file) as f:
yaml_data = yaml.safe_load(f)
with open(json_file) as f:
json_data = json.load(f)
assert yaml_data == json_data
@then("idempotent scripts should be marked")
def step_verify_idempotent_marked(context):
"""Verify idempotent scripts are marked."""
idempotent_scripts = [
s for s in context.shell_catalog["scripts"] if s["idempotent"]
]
assert len(idempotent_scripts) > 0
@then("scripts with mkdir -p should be idempotent")
def step_verify_mkdir_idempotent(context):
"""Verify scripts with mkdir -p are marked idempotent."""
# This is handled by the analyzer logic
pass
@then("scripts with test checks should be idempotent")
def step_verify_test_idempotent(context):
"""Verify scripts with test checks are marked idempotent."""
# This is handled by the analyzer logic
pass
@then("test scripts should be identified")
def step_verify_test_scripts(context):
"""Verify test scripts are identified."""
test_scripts = [
s for s in context.shell_catalog["scripts"] if "test" in s["name"].lower()
]
assert len(test_scripts) > 0
@then("development scripts should be identified")
def step_verify_dev_scripts(context):
"""Verify development scripts are identified."""
[s for s in context.shell_catalog["scripts"] if "dev" in s["name"].lower()]
# May or may not have dev scripts
pass
@then("code generation scripts should be identified")
def step_verify_gen_scripts(context):
"""Verify code generation scripts are identified."""
[
s
for s in context.shell_catalog["scripts"]
if "gen" in s["name"].lower() or "provider" in s["name"].lower()
]
# May or may not have generation scripts
pass