Skip to content

Commit bc8cd39

Browse files
authored
Print rspec progress to standard out when using 'run in terminal' (#73)
* Print progress to standard out when using 'run in terminal' * Fix a typo and linting issue
1 parent 9c5b1f1 commit bc8cd39

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

lib/ruby_lsp/ruby_lsp_rspec/rspec_formatter.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
# frozen_string_literal: true
33

44
require "rspec/core/formatters"
5+
require "rspec/core/formatters/progress_formatter"
56
require "ruby_lsp/test_reporters/lsp_reporter"
67

78
module RubyLsp
89
module RSpec
9-
class RSpecFormatter
10+
class RSpecFormatter < ::RSpec::Core::Formatters::ProgressFormatter
1011
::RSpec::Core::Formatters.register(
1112
self,
1213
:example_passed,
1314
:example_pending,
1415
:example_failed,
1516
:example_started,
17+
:start_dump,
1618
:stop,
1719
)
1820

1921
def initialize(output)
20-
@output = output
22+
super(output)
2123
end
2224

2325
def example_started(notification)
@@ -29,20 +31,26 @@ def example_started(notification)
2931
end
3032

3133
def example_passed(notification)
34+
super(notification)
35+
3236
example = notification.example
3337
uri = uri_for(example)
3438
id = generate_id(example)
3539
RubyLsp::LspReporter.instance.record_pass(id: id, uri: uri)
3640
end
3741

3842
def example_failed(notification)
43+
super(notification)
44+
3945
example = notification.example
4046
uri = uri_for(example)
4147
id = generate_id(example)
4248
RubyLsp::LspReporter.instance.record_fail(id: id, message: notification.exception.message, uri: uri)
4349
end
4450

4551
def example_pending(notification)
52+
super(notification)
53+
4654
example = notification.example
4755
uri = uri_for(example)
4856
id = generate_id(example)

spec/rspec_formatter_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
require "socket"
66
require "open3"
77
require "json"
8+
require "stringio"
9+
require "ruby_lsp/ruby_lsp_rspec/rspec_formatter"
810

911
RSpec.describe "RubyLsp::RSpec::RSpecFormatter" do
1012
it "sends correct LSP events during test execution" do
@@ -135,4 +137,63 @@
135137

136138
expect(events).to eq(expected)
137139
end
140+
141+
describe "RubyLsp::RSpec::RSpecFormatter notifications" do
142+
let(:output) { StringIO.new }
143+
let(:formatter) { RubyLsp::RSpec::RSpecFormatter.new(output) }
144+
let(:notification) { double("Notification") }
145+
let(:example) { double("Example") }
146+
147+
before do
148+
allow(notification).to receive(:example).and_return(example)
149+
allow(example).to receive(:file_path).and_return("spec/fixtures/rspec_example_spec.rb")
150+
allow(example).to receive(:location).and_return("./spec/fixtures/rspec_example_spec.rb:13")
151+
allow(example).to receive(:example_group).and_return(double("ExampleGroup", parent_groups: []))
152+
end
153+
154+
it "is a subclass of ProgressFormatter" do
155+
expect(RubyLsp::RSpec::RSpecFormatter.superclass).to eq(RSpec::Core::Formatters::ProgressFormatter)
156+
end
157+
158+
it "registers necessary notifications with RSpec" do
159+
registered_notifications = RSpec::Core::Formatters::Loader.formatters[RubyLsp::RSpec::RSpecFormatter]
160+
161+
expect(registered_notifications).to match_array([
162+
:example_passed,
163+
:example_pending,
164+
:example_failed,
165+
:example_started,
166+
:start_dump,
167+
:stop,
168+
])
169+
end
170+
171+
it "invokes ProgressFormatter's example_passed" do
172+
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_passed)
173+
174+
formatter.example_passed(notification)
175+
end
176+
177+
it "invokes ProgressFormatter's example_failed" do
178+
exception = double("Exception", message: "error message")
179+
allow(notification).to receive(:exception).and_return(exception)
180+
181+
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_failed)
182+
183+
formatter.example_failed(notification)
184+
end
185+
186+
it "invokes ProgressFormatter's example_pending" do
187+
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:example_pending)
188+
189+
formatter.example_pending(notification)
190+
end
191+
192+
it "invokes ProgressFormatter's start_dump" do
193+
dump_notification = double("DumpNotification")
194+
expect_any_instance_of(RSpec::Core::Formatters::ProgressFormatter).to receive(:start_dump)
195+
196+
formatter.start_dump(dump_notification)
197+
end
198+
end
138199
end

0 commit comments

Comments
 (0)