/usr/lib/ruby/vendor_ruby/rspec/core/formatters/exception_presenter.rb is in ruby-rspec-core 3.7.0c1e0m0s1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | # encoding: utf-8
RSpec::Support.require_rspec_core "formatters/console_codes"
RSpec::Support.require_rspec_core "formatters/snippet_extractor"
RSpec::Support.require_rspec_core 'formatters/syntax_highlighter'
RSpec::Support.require_rspec_support "encoded_string"
module RSpec
module Core
module Formatters
# @private
class ExceptionPresenter
attr_reader :exception, :example, :description, :message_color,
:detail_formatter, :extra_detail_formatter, :backtrace_formatter
private :message_color, :detail_formatter, :extra_detail_formatter, :backtrace_formatter
def initialize(exception, example, options={})
@exception = exception
@example = example
@message_color = options.fetch(:message_color) { RSpec.configuration.failure_color }
@description = options.fetch(:description) { example.full_description }
@detail_formatter = options.fetch(:detail_formatter) { Proc.new {} }
@extra_detail_formatter = options.fetch(:extra_detail_formatter) { Proc.new {} }
@backtrace_formatter = options.fetch(:backtrace_formatter) { RSpec.configuration.backtrace_formatter }
@indentation = options.fetch(:indentation, 2)
@skip_shared_group_trace = options.fetch(:skip_shared_group_trace, false)
@failure_lines = options[:failure_lines]
end
def message_lines
add_shared_group_lines(failure_lines, Notifications::NullColorizer)
end
def colorized_message_lines(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
add_shared_group_lines(failure_lines, colorizer).map do |line|
colorizer.wrap line, message_color
end
end
def formatted_backtrace(exception=@exception)
backtrace_formatter.format_backtrace(exception.backtrace, example.metadata) +
formatted_cause(exception)
end
if RSpec::Support::RubyFeatures.supports_exception_cause?
def formatted_cause(exception)
last_cause = final_exception(exception)
cause = []
if exception.cause
cause << '------------------'
cause << '--- Caused by: ---'
cause << "#{exception_class_name(last_cause)}:" unless exception_class_name(last_cause) =~ /RSpec/
encoded_string(last_cause.message.to_s).split("\n").each do |line|
cause << " #{line}"
end
cause << (" #{backtrace_formatter.format_backtrace(last_cause.backtrace, example.metadata).first}")
end
cause
end
else
# :nocov:
def formatted_cause(_)
[]
end
# :nocov:
end
def colorized_formatted_backtrace(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
formatted_backtrace.map do |backtrace_info|
colorizer.wrap "# #{backtrace_info}", RSpec.configuration.detail_color
end
end
def fully_formatted(failure_number, colorizer=::RSpec::Core::Formatters::ConsoleCodes)
lines = fully_formatted_lines(failure_number, colorizer)
lines.join("\n") << "\n"
end
def fully_formatted_lines(failure_number, colorizer)
lines = [
description,
detail_formatter.call(example, colorizer),
formatted_message_and_backtrace(colorizer),
extra_detail_formatter.call(failure_number, colorizer),
].compact.flatten
lines = indent_lines(lines, failure_number)
lines.unshift("")
lines
end
private
def final_exception(exception, previous=[])
cause = exception.cause
if cause && !previous.include?(cause)
previous << cause
final_exception(cause, previous)
else
exception
end
end
if String.method_defined?(:encoding)
def encoding_of(string)
string.encoding
end
def encoded_string(string)
RSpec::Support::EncodedString.new(string, Encoding.default_external)
end
else # for 1.8.7
# :nocov:
def encoding_of(_string)
end
def encoded_string(string)
RSpec::Support::EncodedString.new(string)
end
# :nocov:
end
def indent_lines(lines, failure_number)
alignment_basis = ' ' * @indentation
alignment_basis << "#{failure_number}) " if failure_number
indentation = ' ' * alignment_basis.length
lines.each_with_index.map do |line, index|
if index == 0
"#{alignment_basis}#{line}"
elsif line.empty?
line
else
"#{indentation}#{line}"
end
end
end
def exception_class_name(exception=@exception)
name = exception.class.name.to_s
name = "(anonymous error class)" if name == ''
name
end
def failure_lines
@failure_lines ||= [].tap do |lines|
lines.concat(failure_slash_error_lines)
sections = [failure_slash_error_lines, exception_lines]
if sections.any? { |section| section.size > 1 } && !exception_lines.first.empty?
lines << ''
end
lines.concat(exception_lines)
lines.concat(extra_failure_lines)
end
end
def failure_slash_error_lines
lines = read_failed_lines
if lines.count == 1
lines[0] = "Failure/Error: #{lines[0].strip}"
else
least_indentation = SnippetExtractor.least_indentation_from(lines)
lines = lines.map { |line| line.sub(/^#{least_indentation}/, ' ') }
lines.unshift('Failure/Error:')
end
lines
end
def exception_lines
lines = []
lines << "#{exception_class_name}:" unless exception_class_name =~ /RSpec/
encoded_string(exception.message.to_s).split("\n").each do |line|
lines << (line.empty? ? line : " #{line}")
end
lines
end
def extra_failure_lines
@extra_failure_lines ||= begin
lines = Array(example.metadata[:extra_failure_lines])
unless lines.empty?
lines.unshift('')
lines.push('')
end
lines
end
end
def add_shared_group_lines(lines, colorizer)
return lines if @skip_shared_group_trace
example.metadata[:shared_group_inclusion_backtrace].each do |frame|
lines << colorizer.wrap(frame.description, RSpec.configuration.default_color)
end
lines
end
def read_failed_lines
matching_line = find_failed_line
unless matching_line
return ["Unable to find matching line from backtrace"]
end
file_and_line_number = matching_line.match(/(.+?):(\d+)(|:\d+)/)
unless file_and_line_number
return ["Unable to infer file and line number from backtrace"]
end
file_path, line_number = file_and_line_number[1..2]
max_line_count = RSpec.configuration.max_displayed_failure_line_count
lines = SnippetExtractor.extract_expression_lines_at(file_path, line_number.to_i, max_line_count)
RSpec.world.syntax_highlighter.highlight(lines)
rescue SnippetExtractor::NoSuchFileError
["Unable to find #{file_path} to read failed line"]
rescue SnippetExtractor::NoSuchLineError
["Unable to find matching line in #{file_path}"]
rescue SecurityError
["Unable to read failed line"]
end
def find_failed_line
line_regex = RSpec.configuration.in_project_source_dir_regex
loaded_spec_files = RSpec.configuration.loaded_spec_files
exception_backtrace.find do |line|
next unless (line_path = line[/(.+?):(\d+)(|:\d+)/, 1])
path = File.expand_path(line_path)
loaded_spec_files.include?(path) || path =~ line_regex
end || exception_backtrace.first
end
def formatted_message_and_backtrace(colorizer)
lines = colorized_message_lines(colorizer) + colorized_formatted_backtrace(colorizer)
encoding = encoding_of("")
lines.map do |line|
RSpec::Support::EncodedString.new(line, encoding)
end
end
def exception_backtrace
exception.backtrace || []
end
# @private
# Configuring the `ExceptionPresenter` with the right set of options to handle
# pending vs failed vs skipped and aggregated (or not) failures is not simple.
# This class takes care of building an appropriate `ExceptionPresenter` for the
# provided example.
class Factory
def build
ExceptionPresenter.new(@exception, @example, options)
end
private
def initialize(example)
@example = example
@execution_result = example.execution_result
@exception = if @execution_result.status == :pending
@execution_result.pending_exception
else
@execution_result.exception
end
end
def options
with_multiple_error_options_as_needed(@exception, pending_options || {})
end
def pending_options
if @execution_result.pending_fixed?
{
:description => "#{@example.full_description} FIXED",
:message_color => RSpec.configuration.fixed_color,
:failure_lines => [
"Expected pending '#{@execution_result.pending_message}' to fail. No error was raised."
]
}
elsif @execution_result.status == :pending
{
:message_color => RSpec.configuration.pending_color,
:detail_formatter => PENDING_DETAIL_FORMATTER
}
end
end
def with_multiple_error_options_as_needed(exception, options)
return options unless multiple_exceptions_error?(exception)
options = options.merge(
:failure_lines => [],
:extra_detail_formatter => sub_failure_list_formatter(exception, options[:message_color]),
:detail_formatter => multiple_exception_summarizer(exception,
options[:detail_formatter],
options[:message_color])
)
return options unless exception.aggregation_metadata[:hide_backtrace]
options[:backtrace_formatter] = EmptyBacktraceFormatter
options
end
def multiple_exceptions_error?(exception)
MultipleExceptionError::InterfaceTag === exception
end
def multiple_exception_summarizer(exception, prior_detail_formatter, color)
lambda do |example, colorizer|
summary = if exception.aggregation_metadata[:hide_backtrace]
# Since the backtrace is hidden, the subfailures will come
# immediately after this, and using `:` will read well.
"Got #{exception.exception_count_description}:"
else
# The backtrace comes after this, so using a `:` doesn't make sense
# since the failures may be many lines below.
"#{exception.summary}."
end
summary = colorizer.wrap(summary, color || RSpec.configuration.failure_color)
return summary unless prior_detail_formatter
[
prior_detail_formatter.call(example, colorizer),
summary
]
end
end
def sub_failure_list_formatter(exception, message_color)
common_backtrace_truncater = CommonBacktraceTruncater.new(exception)
lambda do |failure_number, colorizer|
FlatMap.flat_map(exception.all_exceptions.each_with_index) do |failure, index|
options = with_multiple_error_options_as_needed(
failure,
:description => nil,
:indentation => 0,
:message_color => message_color || RSpec.configuration.failure_color,
:skip_shared_group_trace => true
)
failure = common_backtrace_truncater.with_truncated_backtrace(failure)
presenter = ExceptionPresenter.new(failure, @example, options)
presenter.fully_formatted_lines(
"#{failure_number ? "#{failure_number}." : ''}#{index + 1}",
colorizer
)
end
end
end
# @private
# Used to prevent a confusing backtrace from showing up from the `aggregate_failures`
# block declared for `:aggregate_failures` metadata.
module EmptyBacktraceFormatter
def self.format_backtrace(*)
[]
end
end
# @private
class CommonBacktraceTruncater
def initialize(parent)
@parent = parent
end
def with_truncated_backtrace(child)
child_bt = child.backtrace
parent_bt = @parent.backtrace
return child if child_bt.nil? || child_bt.empty? || parent_bt.nil?
index_before_first_common_frame = -1.downto(-child_bt.size).find do |index|
parent_bt[index] != child_bt[index]
end
return child if index_before_first_common_frame == -1
child = child.dup
child.set_backtrace(child_bt[0..index_before_first_common_frame])
child
end
end
end
# @private
PENDING_DETAIL_FORMATTER = Proc.new do |example, colorizer|
colorizer.wrap("# #{example.execution_result.pending_message}", :detail)
end
end
end
# Provides a single exception instance that provides access to
# multiple sub-exceptions. This is used in situations where a single
# individual spec has multiple exceptions, such as one in the `it` block
# and one in an `after` block.
class MultipleExceptionError < StandardError
# @private
# Used so there is a common module in the ancestor chain of this class
# and `RSpec::Expectations::MultipleExpectationsNotMetError`, which allows
# code to detect exceptions that are instances of either, without first
# checking to see if rspec-expectations is loaded.
module InterfaceTag
# Appends the provided exception to the list.
# @param exception [Exception] Exception to append to the list.
# @private
def add(exception)
# `PendingExampleFixedError` can be assigned to an example that initially has no
# failures, but when the `aggregate_failures` around hook completes, it notifies of
# a failure. If we do not ignore `PendingExampleFixedError` it would be surfaced to
# the user as part of a multiple exception error, which is undesirable. While it's
# pretty weird we handle this here, it's the best solution I've been able to come
# up with, and `PendingExampleFixedError` always represents the _lack_ of any exception
# so clearly when we are transitioning to a `MultipleExceptionError`, it makes sense to
# ignore it.
return if Pending::PendingExampleFixedError === exception
return if exception == self
all_exceptions << exception
if exception.class.name =~ /RSpec/
failures << exception
else
other_errors << exception
end
end
# Provides a way to force `ex` to be something that satisfies the multiple
# exception error interface. If it already satisfies it, it will be returned;
# otherwise it will wrap it in a `MultipleExceptionError`.
# @private
def self.for(ex)
return ex if self === ex
MultipleExceptionError.new(ex)
end
end
include InterfaceTag
# @return [Array<Exception>] The list of failures.
attr_reader :failures
# @return [Array<Exception>] The list of other errors.
attr_reader :other_errors
# @return [Array<Exception>] The list of failures and other exceptions, combined.
attr_reader :all_exceptions
# @return [Hash] Metadata used by RSpec for formatting purposes.
attr_reader :aggregation_metadata
# @return [nil] Provided only for interface compatibility with
# `RSpec::Expectations::MultipleExpectationsNotMetError`.
attr_reader :aggregation_block_label
# @param exceptions [Array<Exception>] The initial list of exceptions.
def initialize(*exceptions)
super()
@failures = []
@other_errors = []
@all_exceptions = []
@aggregation_metadata = { :hide_backtrace => true }
@aggregation_block_label = nil
exceptions.each { |e| add e }
end
# @return [String] Combines all the exception messages into a single string.
# @note RSpec does not actually use this -- instead it formats each exception
# individually.
def message
all_exceptions.map(&:message).join("\n\n")
end
# @return [String] A summary of the failure, including the block label and a count of failures.
def summary
"Got #{exception_count_description}"
end
# return [String] A description of the failure/error counts.
def exception_count_description
failure_count = Formatters::Helpers.pluralize(failures.size, "failure")
return failure_count if other_errors.empty?
error_count = Formatters::Helpers.pluralize(other_errors.size, "other error")
"#{failure_count} and #{error_count}"
end
end
end
end
|