Skip to content

Commit

Permalink
Don't print duplicate deprecation messages.
Browse files Browse the repository at this point in the history
When a spec suite uses a helper method that is called
from a global `before` hook and calls a deprecated method,
we only need one deprecation message in our output,
not one per spec.

Note that this only limits *identical* deprecation messages.
The same deprecation from multiple call sites will still
each get printed.

Fixes #1151 for 2.99.
  • Loading branch information
myronmarston committed Nov 6, 2013
1 parent ac819ee commit 952fbbf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions lib/rspec/core/formatters/deprecation_formatter.rb
Expand Up @@ -9,6 +9,7 @@ class DeprecationFormatter
def initialize(deprecation_stream, summary_stream)
@deprecation_stream = deprecation_stream
@summary_stream = summary_stream
@seen_deprecations = Set.new
@count = 0
end

Expand All @@ -19,8 +20,11 @@ def printer
end

def deprecation(data)
return if @seen_deprecations.include?(data)

@count += 1
printer.print_deprecation_message data
@seen_deprecations << data
end

def deprecation_summary
Expand Down
34 changes: 25 additions & 9 deletions spec/rspec/core/formatters/deprecation_formatter_spec.rb
Expand Up @@ -76,15 +76,23 @@ module RSpec::Core::Formatters
formatter.deprecation(:deprecated => 'foo')
}.to change { deprecation_stream.sync }.from(false).to(true)
end

it 'does not print duplicate messages' do
3.times { formatter.deprecation(:deprecated => 'foo') }
formatter.deprecation_summary

expect(summary_stream.string).to match(/1 deprecation/)
expect(File.read(deprecation_stream.path)).to eq("foo is deprecated.\n")
end
end

context "with an IO deprecation_stream" do
let(:deprecation_stream) { StringIO.new }

it "groups similar deprecations together" do
formatter.deprecation(:deprecated => 'i_am_deprecated')
formatter.deprecation(:deprecated => 'i_am_deprecated', :call_site => "foo.rb:1")
formatter.deprecation(:deprecated => 'i_am_a_different_deprecation')
formatter.deprecation(:deprecated => 'i_am_deprecated')
formatter.deprecation(:deprecated => 'i_am_deprecated', :call_site => "foo.rb:2")
formatter.deprecation_summary

expected = <<-EOS.gsub(/^\s+\|/, '')
Expand All @@ -93,23 +101,23 @@ module RSpec::Core::Formatters
|
|i_am_a_different_deprecation is deprecated.
|
|i_am_deprecated is deprecated.
|i_am_deprecated is deprecated.
|i_am_deprecated is deprecated. Called from foo.rb:1.
|i_am_deprecated is deprecated. Called from foo.rb:2.
|
EOS
expect(deprecation_stream.string).to eq expected
end

it "limits the deprecation warnings after 3 calls" do
5.times { formatter.deprecation(:deprecated => 'i_am_deprecated') }
5.times { |i| formatter.deprecation(:deprecated => 'i_am_deprecated', :call_site => "foo.rb:#{i + 1}") }
formatter.deprecation_summary
expected = <<-EOS.gsub(/^\s+\|/, '')
|
|Deprecation Warnings:
|
|i_am_deprecated is deprecated.
|i_am_deprecated is deprecated.
|i_am_deprecated is deprecated.
|i_am_deprecated is deprecated. Called from foo.rb:1.
|i_am_deprecated is deprecated. Called from foo.rb:2.
|i_am_deprecated is deprecated. Called from foo.rb:3.
|Too many uses of deprecated 'i_am_deprecated'. Set config.deprecation_stream to a File for full output.
|
EOS
Expand All @@ -136,13 +144,21 @@ module RSpec::Core::Formatters
end

it "prints the true deprecation count to the summary_stream" do
5.times { formatter.deprecation(:deprecated => 'i_am_deprecated') }
5.times { |i| formatter.deprecation(:deprecated => 'i_am_deprecated', :call_site => "foo.rb:#{i + 1}") }
5.times do |n|
formatter.deprecation(:message => "callsite info: /path/#{n}/to/some/file.rb:2#{n}3. And some more stuff")
end
formatter.deprecation_summary
expect(summary_stream.string).to match(/10 deprecation warnings total/)
end

it 'does not print duplicate messages' do
3.times { formatter.deprecation(:deprecated => 'foo') }
formatter.deprecation_summary

expect(summary_stream.string).to match(/1 deprecation/)
expect(deprecation_stream.string).to eq("\nDeprecation Warnings:\n\nfoo is deprecated.\n\n")
end
end
end
end
Expand Down

0 comments on commit 952fbbf

Please sign in to comment.