Module: RSpec::Signal::Backtrace::Parser

Defined in:
lib/rspec/signal/backtrace/parser.rb

Overview

Turns raw backtrace strings into Frames.

Handles the two shapes Ruby emits:

"/path/to/file.rb:12:in `method'"
"/path/to/file.rb:12:in 'Klass#method'"   (Ruby 3.4+ quoting)

and the shapes RSpec emits after its own filtering:

"./spec/models/user_spec.rb:12"

Constant Summary collapse

LINE =
/
  \A
  (?<path>.*?)
  (?::(?<line>\d+))?
  (?::in\s+[`'](?<label>.*)['`])?
  \s*\z
/x.freeze

Class Method Summary collapse

Class Method Details

.parse(backtrace, classifier) ⇒ Array<Frame>

Parameters:

  • backtrace (Array<String>, nil)
  • classifier (#call)

    receives a Frame with :kind unset, returns the kind

Returns:



29
30
31
32
33
34
35
36
37
# File 'lib/rspec/signal/backtrace/parser.rb', line 29

def parse(backtrace, classifier)
  Array(backtrace).filter_map do |raw|
    frame = parse_line(raw)
    next unless frame

    classifier.call(frame)
    frame
  end
end

.parse_line(raw) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/signal/backtrace/parser.rb', line 39

def parse_line(raw)
  text = raw.to_s.strip
  return nil if text.empty?

  match = LINE.match(text)
  return nil unless match

  path = match[:path].to_s.sub(%r{\A\./}, "")
  return nil if path.empty?

  Frame.new(
    raw: text,
    path: path,
    line: match[:line]&.to_i,
    label: match[:label],
    kind: :external,
    display_path: path
  )
end