discourse/lib/turbo_tests/json_example.rb
Alan Guo Xiang Tan 655c106101
DEV: Capture and log AR debug logs on GitHub actions for flaky tests (#25048)
Why this change?

We have been running into flaky tests which seems to be related to
AR transaction problems. However, we are not able to reproduce this
locally and do not have sufficient information on our builds now to
debug the problem.

What does this change do?

Noe the following changes only applies when `ENV["GITHUB_ACTIONS"]` is
present.

This change introduces an RSpec around hook when `capture_log: true` has
been set for a test. The responsibility of the hook is to capture the
ActiveRecord debug logs and print them out.
2023-12-27 14:40:00 +08:00

58 lines
1.8 KiB
Ruby

# 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],
process_pid: Process.pid,
js_deprecations: @rspec_example.metadata[:js_deprecations],
active_record_debug_logs: @rspec_example.metadata[:active_record_debug_logs],
},
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