fix(events): add missing structlog import and use capture_logs in step file
CI / benchmark-regression (pull_request) Waiting to run
CI / benchmark-publish (pull_request) Waiting to run
CI / lint (pull_request) Successful in 1m0s
CI / build (pull_request) Successful in 41s
CI / quality (pull_request) Successful in 1m9s
CI / helm (pull_request) Successful in 31s
CI / typecheck (pull_request) Successful in 1m20s
CI / security (pull_request) Successful in 1m21s
CI / push-validation (pull_request) Successful in 31s
CI / integration_tests (pull_request) Successful in 3m47s
CI / e2e_tests (pull_request) Failing after 3m59s
CI / unit_tests (pull_request) Failing after 5m21s
CI / docker (pull_request) Has been skipped
CI / coverage (pull_request) Successful in 13m32s
CI / status-check (pull_request) Failing after 4s

Replace the custom logging.Handler approach in step_when_emit_event with
structlog.testing.capture_logs() for consistency with the stream error
step, and add the missing import structlog that caused the lint failure.

ISSUES CLOSED: #988
This commit is contained in:
2026-04-24 10:44:56 +00:00
parent c27133f433
commit 4aa38d163f
@@ -11,8 +11,7 @@ and all scenarios now run as normal regression guards.
from __future__ import annotations
import logging
import structlog
from behave import given, then, when # type: ignore[import-untyped]
from behave.runner import Context # type: ignore[import-untyped]
@@ -39,48 +38,9 @@ def step_given_bus_with_failing_handler(context: Context) -> None:
@when("I emit an event that triggers the failing handler")
def step_when_emit_event(context: Context) -> None:
"""Emit a PLAN_CREATED event and capture structlog output."""
# Use Python logging to capture structlog output
# Create a handler that captures log records
logger = logging.getLogger("cleveragents.infrastructure.events.reactive")
# Create a handler that captures logs
class StructlogCapturingHandler(logging.Handler):
def __init__(self):
super().__init__()
self.records = []
def emit(self, record):
self.records.append(record)
handler = StructlogCapturingHandler()
handler.setLevel(logging.DEBUG)
logger.addHandler(handler)
try:
# Emit the event
with structlog.testing.capture_logs() as captured:
context.bus.emit(DomainEvent(event_type=EventType.PLAN_CREATED))
# Convert logging records to structlog format
context.captured_logs = []
for record in handler.records:
# structlog with stdlib integration stores the dict in record.msg
log_entry = {}
# When structlog is configured with stdlib.ProcessorFormatter,
# the structured data is stored directly in record.msg as a dict
if isinstance(record.msg, dict):
log_entry = record.msg
else:
# Fallback: ensure we at least have log_level
log_entry = {"event": record.getMessage()}
# Ensure we have the log_level
if "log_level" not in log_entry:
log_entry["log_level"] = record.levelname.lower()
context.captured_logs.append(log_entry)
finally:
logger.removeHandler(handler)
context.captured_logs = captured
@then("the warning log should contain the exception message text")