0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/lib/turbo_tests/json_example.rb
Alan Guo Xiang Tan 71a6dd9674
DEV: Add per-test SQL/Redis/network capture RSpec formatter (#40632)
Adds `RspecPerformanceFormatter`, an RSpec formatter that captures the
SQL queries, Redis commands, and outbound network calls each example
performs and emits them as NDJSON with one object per test. It gives an
AI model (or a developer) concrete per-test context for spotting N+1s,
redundant queries, or unexpected outbound calls. Off unless the
formatter is selected, so normal runs and production are unaffected.
Works with both `bin/rspec` and `bin/turbo_rspec`.
2026-06-16 09:47:54 +08:00

58 lines
1.9 KiB
Ruby
Vendored

# frozen_string_literal: true
module TurboTests
class JsonExample
def initialize(rspec_example)
@rspec_example = rspec_example
end
def to_json
{
execution_result: execution_result_to_json(@rspec_example.execution_result),
location: @rspec_example.location,
full_description: @rspec_example.full_description,
metadata: {
shared_group_inclusion_backtrace:
@rspec_example.metadata[:shared_group_inclusion_backtrace].map(
&method(:stack_frame_to_json)
),
extra_failure_lines: @rspec_example.metadata[:extra_failure_lines],
run_duration_ms: @rspec_example.metadata[:run_duration_ms],
js_deprecations: @rspec_example.metadata[:js_deprecations],
rerun_file_path: @rspec_example.metadata[:rerun_file_path],
active_record_debug_logs: @rspec_example.metadata[:active_record_debug_logs],
perf: @rspec_example.metadata[:perf],
},
location_rerun_argument: @rspec_example.location_rerun_argument,
}
end
private
def stack_frame_to_json(frame)
{ shared_group_name: frame.shared_group_name, inclusion_location: frame.inclusion_location }
end
def exception_to_json(exception)
if exception
{
class_name: exception.class.name.to_s,
backtrace: exception.backtrace,
message: exception.message,
cause: exception_to_json(exception.cause),
}
end
end
def execution_result_to_json(result)
{
example_skipped?: result.example_skipped?,
pending_message: result.pending_message,
status: result.status,
pending_fixed?: result.pending_fixed?,
exception: exception_to_json(result.exception),
pending_exception: exception_to_json(result.pending_exception),
}
end
end
end