Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit ff77be9

Browse files
committed
switch from specific coderay reporter message to hook
1 parent b1b5bf4 commit ff77be9

File tree

5 files changed

+58
-63
lines changed

5 files changed

+58
-63
lines changed

lib/rspec/core/notifications.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,8 @@ def fully_formatted
281281
# @attr pending_examples [Array<RSpec::Core::Example>] the pending examples
282282
# @attr load_time [Float] the number of seconds taken to boot RSpec
283283
# and load the spec files
284-
# @attr syntax_highlighting_unavailable [Boolean] indicates if syntax highlighting
285-
# was attempted to be used but was unavailable.
286-
SummaryNotification = Struct.new(:duration, :examples, :failed_examples, :pending_examples,
287-
:load_time, :syntax_highlighting_unavailable)
284+
SummaryNotification = Struct.new(:duration, :examples, :failed_examples,
285+
:pending_examples, :load_time)
288286
class SummaryNotification
289287
# @api
290288
# @return [Fixnum] the number of examples run
@@ -362,11 +360,7 @@ def formatted_load_time
362360
# @return [String] The summary information fully formatted in the way that
363361
# RSpec's built-in formatters emit.
364362
def fully_formatted(colorizer=::RSpec::Core::Formatters::ConsoleCodes)
365-
formatted = ""
366-
if syntax_highlighting_unavailable
367-
formatted << "\nSyntax highlighting of failure snippets unavailable -- install the coderay gem to enable it.\n"
368-
end
369-
formatted << "\nFinished in #{formatted_duration} " \
363+
formatted = "\nFinished in #{formatted_duration} " \
370364
"(files took #{formatted_load_time} to load)\n" \
371365
"#{colorized_totals_line(colorizer)}\n"
372366

lib/rspec/core/reporter.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ def initialize(configuration)
1717
@examples = []
1818
@failed_examples = []
1919
@pending_examples = []
20+
@before_summary_messages = {}
2021
@duration = @start = @load_time = nil
2122
end
2223

2324
# @private
24-
attr_reader :examples, :failed_examples, :pending_examples
25-
# @private
26-
attr_accessor :syntax_highlighting_unavailable
25+
attr_reader :examples, :failed_examples, :pending_examples, :before_summary_messages
2726

2827
# @private
2928
def reset
3029
@examples = []
3130
@failed_examples = []
3231
@pending_examples = []
32+
@before_summary_messages = {}
3333
@profiler = Profiler.new if defined?(@profiler)
3434
end
3535

@@ -97,6 +97,14 @@ def message(message)
9797
notify :message, Notifications::MessageNotification.new(message)
9898
end
9999

100+
# @param type A key to limit duplicate messages
101+
# @param message [#to_s] A message object to output on exit to formatters
102+
#
103+
# Send a custom message to supporting formatters once on exit.
104+
def add_message_before_summary(type, message)
105+
@before_summary_messages[type] ||= message
106+
end
107+
100108
# @param event [Symbol] Name of the custom event to trigger on formatters
101109
# @param options [Hash] Hash of arguments to provide via `CustomNotification`
102110
#
@@ -166,9 +174,11 @@ def finish
166174
@configuration.profile_examples,
167175
@profiler.example_groups)
168176
end
177+
before_summary_messages.each_value do |text|
178+
notify :message, Notifications::MessageNotification.new(text)
179+
end
169180
notify :dump_summary, Notifications::SummaryNotification.new(@duration, @examples, @failed_examples,
170-
@pending_examples, @load_time,
171-
syntax_highlighting_unavailable)
181+
@pending_examples, @load_time)
172182
notify :seed, Notifications::SeedNotification.new(@configuration.seed, seed_used?)
173183
end
174184
end

lib/rspec/core/source/syntax_highlighter.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ def implementation
3131
def color_enabled_implementation
3232
@color_enabled_implementation ||= begin
3333
::Kernel.require 'coderay'
34-
@configuration.reporter.syntax_highlighting_unavailable = false
3534
CodeRayImplementation
3635
rescue LoadError
37-
@configuration.reporter.syntax_highlighting_unavailable = true
36+
@configuration.reporter.add_message_before_summary(
37+
:coderay,
38+
"\nSyntax highlighting of failure snippets unavailable " \
39+
"-- install the coderay gem to enable it.\n"
40+
)
3841
NoSyntaxHighlightingImplementation
3942
end
4043
end

spec/rspec/core/reporter_spec.rb

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,33 @@ module RSpec::Core
3535
reporter.finish
3636
end
3737

38-
let(:install_coderay_snippet) { "install the coderay gem" }
39-
40-
def formatter_notified_of_dump_summary(syntax_highlighting_unavailable)
41-
formatter = spy("formatter")
42-
reporter.syntax_highlighting_unavailable = syntax_highlighting_unavailable
43-
reporter.register_listener formatter, :dump_summary
38+
it "dumps any add_message_before_summary messages once before the summary" do
39+
formatter = instance_double("RSpec::Core::Formatter::ProgressFormatter")
40+
reporter.register_listener(formatter, :message, :dump_summary)
4441

45-
reporter.start(0)
46-
reporter.finish
47-
formatter
48-
end
42+
reporter.add_message_before_summary :install_this_gem, "install my gem"
43+
reporter.add_message_before_summary :install_this_gem, "install my gem please?"
44+
reporter.add_message_before_summary :install_that_gem, "install a different gem"
4945

50-
it "includes a note about install coderay if syntax highlighting is unavailable" do
51-
formatter = formatter_notified_of_dump_summary(true)
46+
expect(formatter).to be_notified_with(
47+
an_object_having_attributes(:message => "install my gem")
48+
)
49+
expect(formatter).to be_notified_with(
50+
an_object_having_attributes(:message => "install a different gem")
51+
)
52+
expect(formatter).to receive(:dump_summary).ordered
5253

53-
expect(formatter).to have_received(:dump_summary).with(an_object_having_attributes(
54-
:fully_formatted => a_string_including(install_coderay_snippet)
55-
))
54+
reporter.finish
5655
end
5756

58-
it "does not include a note about installing coderay if syntax highlighting is available" do
59-
formatter = formatter_notified_of_dump_summary(false)
60-
61-
expect(formatter).to have_received(:dump_summary).with(an_object_having_attributes(
62-
:fully_formatted => a_string_excluding(install_coderay_snippet)
63-
))
57+
if RUBY_VERSION == '1.8.7'
58+
def be_notified_with(message)
59+
receive(:message).with(message).once
60+
end
61+
else
62+
def be_notified_with(message)
63+
receive(:message).with(message).once.ordered
64+
end
6465
end
6566
end
6667

spec/rspec/core/source/syntax_highlighter_spec.rb

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ class RSpec::Core::Source
55
let(:config) { RSpec::Core::Configuration.new.tap { |c| c.color = true } }
66
let(:highlighter) { SyntaxHighlighter.new(config) }
77

8-
def be_highlighted
9-
include("\e[32m")
10-
end
11-
128
context "when CodeRay is available", :unless => RSpec::Support::OS.windows? do
139
before { expect { require 'coderay' }.not_to raise_error }
1410

@@ -42,22 +38,6 @@ def be_highlighted
4238
expect(highlighter.highlight(['[:ok, "ok"]']).first).not_to be_highlighted
4339
end
4440

45-
it 'notifies the reporter' do
46-
config.reporter.syntax_highlighting_unavailable = true
47-
48-
expect {
49-
highlighter.highlight([""])
50-
}.to change { config.reporter.syntax_highlighting_unavailable }.to(false)
51-
end
52-
53-
it 'does not notify the reporter if highlighting is never attempted' do
54-
config.reporter.syntax_highlighting_unavailable = true
55-
56-
expect {
57-
SyntaxHighlighter.new(config)
58-
}.not_to change { config.reporter.syntax_highlighting_unavailable }
59-
end
60-
6141
it "rescues coderay failures since we do not want a coderay error to be displayed instead of the user's error" do
6242
allow(CodeRay).to receive(:encode).and_raise(Exception.new "boom")
6343
lines = [":ok"]
@@ -96,20 +76,27 @@ def be_highlighted
9676
end
9777

9878
it 'notifies the reporter', :unless => RSpec::Support::OS.windows? do
99-
config.reporter.syntax_highlighting_unavailable = false
100-
10179
expect {
10280
highlighter.highlight([""])
103-
}.to change { config.reporter.syntax_highlighting_unavailable }.to(true)
81+
}.to change {
82+
config.reporter.before_summary_messages
83+
}.to include_coderay_snippet
10484
end
10585

10686
it 'does not notify the reporter if highlighting is never attempted' do
107-
config.reporter.syntax_highlighting_unavailable = false
108-
10987
expect {
11088
SyntaxHighlighter.new(config)
111-
}.not_to change { config.reporter.syntax_highlighting_unavailable }
89+
}.not_to change { config.reporter.before_summary_messages }
11290
end
11391
end
92+
93+
def be_highlighted
94+
include("\e[32m")
95+
end
96+
97+
def include_coderay_snippet
98+
include(:coderay => a_string_including("install the coderay gem"))
99+
end
100+
114101
end
115102
end

0 commit comments

Comments
 (0)