Files
temp/tests/features/steps/main_module_coverage_steps.py
T

147 lines
5.0 KiB
Python

"""Step definitions for main module coverage tests."""
import subprocess
import sys
from unittest.mock import patch
from behave import given, then, when
import cleveragents.__main__
@given("I have the cleveragents package installed")
def step_package_installed(context):
"""Verify the package is installed."""
context.package_installed = True
# Store original argv for restoration
context.original_argv = sys.argv.copy()
@when("I execute the module directly with python -m")
def step_execute_module_directly(context):
"""Execute the module as __main__."""
context.execution_result = None
context.main_called = False
# Mock the main function to track if it's called
with patch("cleveragents.__main__.main") as mock_main:
mock_main.return_value = 0
try:
# Import the module and check if main is called
import cleveragents.__main__
# The main() call happens only when __name__ == '__main__'
# Since we're importing, we need to simulate that condition
if hasattr(cleveragents.__main__, "__name__"):
# Manually call main to simulate direct execution
cleveragents.__main__.main()
context.main_called = mock_main.called
context.execution_result = "success"
except SystemExit as e:
context.execution_result = e.code
except Exception as e:
context.execution_result = str(e)
@when("I import the __main__ module")
def step_import_main_module(context):
"""Import the __main__ module without executing it."""
context.import_result = None
context.main_called = False
with patch("cleveragents.__main__.main") as mock_main:
try:
# Import the module (should not trigger main)
import importlib
importlib.reload(cleveragents.__main__)
context.main_called = mock_main.called
context.import_result = "success"
except Exception as e:
context.import_result = str(e)
@when('I execute the module with "{argument}" argument')
def step_execute_with_argument(context, argument):
"""Execute the module with specific arguments."""
context.execution_result = None
try:
# Use subprocess to run the module with arguments
result = subprocess.run(
[sys.executable, "-m", "cleveragents", argument],
capture_output=True,
text=True,
timeout=5,
)
context.execution_result = result
context.stdout = result.stdout
context.stderr = result.stderr
context.return_code = result.returncode
except subprocess.TimeoutExpired:
context.execution_result = "timeout"
except Exception as e:
context.execution_result = str(e)
@then("the main entry point function should be invoked")
def step_main_function_called(context):
"""Verify the main function was called."""
assert context.main_called, "Main function was not called"
@then("the CLI should handle the execution")
def step_cli_handles_execution(context):
"""Verify CLI handled the execution."""
assert context.execution_result in [
"success",
0,
], f"CLI execution failed: {context.execution_result}"
@then("the module should load successfully")
def step_module_loads_successfully(context):
"""Verify module imported successfully."""
assert context.import_result == "success", f"Module import failed: {context.import_result}"
@then("the main function should not be invoked on import")
def step_main_not_called_automatically(context):
"""Verify main is not called on import."""
assert not context.main_called, "Main function was called on import"
@then("the version information should be displayed")
def step_version_displayed(context):
"""Verify version info is displayed."""
assert hasattr(context, "stdout"), "No stdout captured"
# Check for version pattern in output
assert any(
word in context.stdout.lower() for word in ["version", "v", "0."]
), f"Version not found in output: {context.stdout}"
@then("the program should exit successfully")
def step_exit_successfully(context):
"""Verify successful exit."""
assert context.return_code == 0, f"Non-zero exit code: {context.return_code}"
@then("the help information should be displayed")
def step_help_displayed(context):
"""Verify help is displayed."""
assert hasattr(context, "stdout"), "No stdout captured"
assert (
"usage" in context.stdout.lower() or "help" in context.stdout.lower()
), f"Help not found in output: {context.stdout}"
@then("all available commands should be listed")
def step_commands_listed(context):
"""Verify commands are listed."""
assert hasattr(context, "stdout"), "No stdout captured"
# Check for common command indicators
assert any(
word in context.stdout for word in ["Commands:", "Options:", "run", "create"]
), f"Commands not found in output: {context.stdout}"