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

Show the example a warning occurred in if nil call site is given. #28

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/rspec/support/warnings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ def self.warning(text, options={})
def self.warn_with(message, options = {})
call_site = options.fetch(:call_site) { CallerFilter.first_non_rspec_line }
message << " Use #{options[:replacement]} instead." if options[:replacement]
message << " Called from #{call_site}." if call_site
if call_site
message << " Called from #{call_site}."
elsif RSpec.respond_to?(:current_example) && RSpec.current_example
message << " Warning occured in `#{RSpec.current_example.source_location.join(":")}`."
end
::Kernel.warn message
end

end
8 changes: 8 additions & 0 deletions spec/rspec/support/warnings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ def run_without_rspec_core
RSpec.send(helper, 'Message', :replacement => 'Replacement')
end
end

it "includes the file and line of the spec if call site is nil" do
top_of_spec = __LINE__ - 1
run_without_rspec_core do
expect(::Kernel).to receive(:warn).with(/Warning occured.*warnings_spec.rb:#{top_of_spec}/)
RSpec.send(helper, "bees", :call_site => nil)
end
end
end

describe "#warning" do
Expand Down