Class: RubyLsp::RSpec::RSpecFormatter

Inherits:
RSpec::Core::Formatters::ProgressFormatter
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ RSpecFormatter

Returns a new instance of RSpecFormatter.



21
22
23
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 21

def initialize(output)
  super(output)
end

Instance Method Details

#adjust_backtrace(backtrace) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 80

def adjust_backtrace(backtrace)
  # Correct the backtrace entry so that vscode recognized it as a link to open
  parts = backtrace.split(":", 3)
  return backtrace if parts.length < 2

  parts[0].sub(/^\./, "file://" + File.expand_path(".")) + ":" + parts[1] + " : " + (parts[2] || "")
end

#example_failed(notification) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 42

def example_failed(notification)
  super(notification)

  example = notification.example
  uri = uri_for(example)
  id = generate_id(example)
  message = notification.message_lines.join("\n")
  message << "\n\n"
  message << notification.formatted_backtrace.map(&method(:adjust_backtrace)).map { |s| "# " + s }.join("\n")
  RubyLsp::LspReporter.instance.record_fail(id: id, message: message, uri: uri)
end

#example_passed(notification) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 33

def example_passed(notification)
  super(notification)

  example = notification.example
  uri = uri_for(example)
  id = generate_id(example)
  RubyLsp::LspReporter.instance.record_pass(id: id, uri: uri)
end

#example_pending(notification) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 54

def example_pending(notification)
  super(notification)

  example = notification.example
  uri = uri_for(example)
  id = generate_id(example)
  RubyLsp::LspReporter.instance.record_skip(id: id, uri: uri)
end

#example_started(notification) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 25

def example_started(notification)
  example = notification.example
  uri = uri_for(example)
  id = generate_id(example)
  line = example.location.split(":").last
  RubyLsp::LspReporter.instance.start_test(id: id, uri: uri, line: line)
end

#generate_id(example) ⇒ Object



76
77
78
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 76

def generate_id(example)
  [example, *example.example_group.parent_groups].reverse.map(&:location).join("::")
end

#stop(notification) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 63

def stop(notification)
  if RubyLsp::LspReporter.start_coverage?
    RubyLsp::LspReporter.instance.at_coverage_exit
  else
    RubyLsp::LspReporter.instance.shutdown
  end
end

#uri_for(example) ⇒ Object



71
72
73
74
# File 'lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb', line 71

def uri_for(example)
  absolute_path = File.expand_path(example.file_path)
  URI::Generic.from_path(path: absolute_path)
end