discourse/lib/turbo_tests/json_rows_formatter.rb

97 lines
2.1 KiB
Ruby
Raw Normal View History

2019-06-20 21:33:41 -04:00
# frozen_string_literal: true
module TurboTests
# An RSpec formatter used for each subprocess during parallel test execution
class JsonRowsFormatter
RSpec::Core::Formatters.register(
self,
:close,
:example_failed,
:example_passed,
:example_pending,
:seed
)
attr_reader :output
def initialize(output)
@output = output
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)
}
end
def example_to_json(example)
{
execution_result: execution_result_to_json(example.execution_result),
location: example.location,
full_description: example.full_description,
metadata: {
shared_group_inclusion_backtrace:
example.metadata[:shared_group_inclusion_backtrace]
},
location_rerun_argument: example.location_rerun_argument
}
end
def example_passed(notification)
2019-06-20 21:33:41 -04:00
output_row(
type: :example_passed,
example: example_to_json(notification.example)
2019-06-20 21:33:41 -04:00
)
end
def example_pending(notification)
2019-06-20 21:33:41 -04:00
output_row(
type: :example_pending,
example: example_to_json(notification.example)
2019-06-20 21:33:41 -04:00
)
end
def example_failed(notification)
2019-06-20 21:33:41 -04:00
output_row(
type: :example_failed,
example: example_to_json(notification.example)
2019-06-20 21:33:41 -04:00
)
end
def seed(notification)
2019-06-20 21:33:41 -04:00
output_row(
type: :seed,
seed: notification.seed,
2019-06-20 21:33:41 -04:00
)
end
def close(notification)
2019-06-20 21:33:41 -04:00
output_row(
type: :close,
2019-06-20 21:33:41 -04:00
)
end
private
def output_row(obj)
output.puts(obj.to_json)
output.flush
end
end
end