DEV: Add --profile=[COUNT] option for turbo_rspec

Why is this change required?

By default, `RSpec` comes with a `--profile=[COUNT]` option as well but
enabling that option means that the entire test suite needs to be
executed. This does not work so well for `turbo_rspec` which splits our
test files into various "buckets" for the tests to be executed in
multiple processes. Therefore, this commit adds a similar
`--profile=[COUNT]` option to `turbo_rspec` but will only profile the
tests being executed. Examples:

`LOAD_PLUGINS=1 bin/turbo_rspec --profile plugins/*/spec/system`

or

`LOAD_PLUGINS=1 bin/turbo_rspec --profile=20 plugins/*/spec/system`
This commit is contained in:
Alan Guo Xiang Tan
2023-05-30 09:52:46 +08:00
parent b580f04d34
commit b00edf3ea0
8 changed files with 96 additions and 15 deletions

View File

@ -1,41 +1,51 @@
# frozen_string_literal: true
RSpec::Support.require_rspec_core "formatters/base_text_formatter"
module TurboTests
# An RSpec formatter that prepends the process id to all messages
class DocumentationFormatter < RSpec::Core::Formatters::BaseTextFormatter
class DocumentationFormatter < ::TurboTests::BaseFormatter
RSpec::Core::Formatters.register(self, :example_failed, :example_passed, :example_pending)
def example_passed(notification)
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
"[#{notification.example.process_id}] #{notification.example.full_description}",
output_example(notification.example),
:success,
)
output.flush
end
def example_pending(notification)
message = notification.example.execution_result.pending_message
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
"[#{notification.example.process_id}] #{notification.example.full_description}" \
" (PENDING: #{message})",
"#{output_example(notification.example)} (PENDING: #{message})",
:pending,
)
output.flush
end
def example_failed(notification)
output.puts RSpec::Core::Formatters::ConsoleCodes.wrap(
"[#{notification.example.process_id}] #{notification.example.full_description}" \
" (FAILED - #{next_failure_index})",
"#{output_example(notification.example)} (FAILED - #{next_failure_index})",
:failure,
)
output.flush
end
private
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
def next_failure_index
@next_failure_index ||= 0
@next_failure_index += 1