2023-05-12 17:13:52 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module TurboTests
|
|
|
|
# An RSpec formatter that prepends the process id to all messages
|
2023-05-30 09:52:46 +08:00
|
|
|
class DocumentationFormatter < ::TurboTests::BaseFormatter
|
2023-05-12 17:13:52 +08:00
|
|
|
RSpec::Core::Formatters.register(self, :example_failed, :example_passed, :example_pending)
|
|
|
|
|
|
|
|
def example_passed(notification)
|
|
|
|
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
|
2023-05-30 09:52:46 +08:00
|
|
|
output_example(notification.example),
|
2023-05-12 17:13:52 +08:00
|
|
|
:success,
|
|
|
|
)
|
2023-05-30 09:52:46 +08:00
|
|
|
|
2023-05-12 17:13:52 +08:00
|
|
|
output.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
def example_pending(notification)
|
|
|
|
message = notification.example.execution_result.pending_message
|
2023-05-30 09:52:46 +08:00
|
|
|
|
2023-05-12 17:13:52 +08:00
|
|
|
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
|
2023-05-30 09:52:46 +08:00
|
|
|
"#{output_example(notification.example)} (PENDING: #{message})",
|
2023-05-12 17:13:52 +08:00
|
|
|
:pending,
|
|
|
|
)
|
2023-05-30 09:52:46 +08:00
|
|
|
|
2023-05-12 17:13:52 +08:00
|
|
|
output.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
def example_failed(notification)
|
|
|
|
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
|
2023-05-30 09:52:46 +08:00
|
|
|
"#{output_example(notification.example)} (FAILED - #{next_failure_index})",
|
2023-05-12 17:13:52 +08:00
|
|
|
:failure,
|
|
|
|
)
|
2023-05-30 09:52:46 +08:00
|
|
|
|
2023-05-12 17:13:52 +08:00
|
|
|
output.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-05-30 09:52:46 +08:00
|
|
|
def output_example(example)
|
|
|
|
output = +"[#{example.process_id}] #{example.full_description}"
|
|
|
|
|
|
|
|
if run_duration_ms = example.metadata[:run_duration_ms]
|
|
|
|
output << " (#{run_duration_ms}ms)"
|
|
|
|
end
|
|
|
|
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2023-05-12 17:13:52 +08:00
|
|
|
def next_failure_index
|
|
|
|
@next_failure_index ||= 0
|
|
|
|
@next_failure_index += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|