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

Commit 05759fd

Browse files
author
Sam Phippen
committed
Merge pull request #1253 from rspec/spec-location-if-no-call-site
Include the spec location in warnings if we can't generate a call site.
2 parents d38f6a5 + 360dbae commit 05759fd

File tree

3 files changed

+73
-18
lines changed

3 files changed

+73
-18
lines changed

lib/rspec/core.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
require 'rspec/support/caller_filter'
1818
require 'rspec/core/warnings'
19-
require 'rspec/support/warnings'
2019

2120
require_rspec['core/flat_map']
2221
require_rspec['core/filter_manager']
@@ -45,6 +44,8 @@
4544
module RSpec
4645
autoload :SharedContext, 'rspec/core/shared_context'
4746

47+
extend RSpec::Core::Warnings
48+
4849
# @private
4950
def self.wants_to_quit
5051
# Used internally to determine what to do when a SIGINT is received

lib/rspec/core/warnings.rb

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,38 @@
1+
require "rspec/support/warnings"
2+
13
module RSpec
4+
module Core
5+
module Warnings
6+
# @private
7+
#
8+
# Used internally to print deprecation warnings
9+
def deprecate(deprecated, data = {})
10+
RSpec.configuration.reporter.deprecation(
11+
{
12+
:deprecated => deprecated,
13+
:call_site => CallerFilter.first_non_rspec_line
14+
}.merge(data)
15+
)
16+
end
217

3-
# @private
4-
#
5-
# Used internally to print deprecation warnings
6-
def self.deprecate(deprecated, data = {})
7-
RSpec.configuration.reporter.deprecation(
8-
{
9-
:deprecated => deprecated,
10-
:call_site => CallerFilter.first_non_rspec_line
11-
}.merge(data)
12-
)
13-
end
18+
# @private
19+
#
20+
# Used internally to print deprecation warnings
21+
def warn_deprecation(message)
22+
RSpec.configuration.reporter.deprecation :message => message
23+
end
1424

15-
# @private
16-
#
17-
# Used internally to print deprecation warnings
18-
def self.warn_deprecation(message)
19-
RSpec.configuration.reporter.deprecation :message => message
20-
end
25+
def warn_with(message, options = {})
26+
if options[:use_spec_location_as_call_site]
27+
message += "." unless message.end_with?(".")
2128

29+
if RSpec.current_example
30+
message += " Warning generated from spec at `#{RSpec.current_example.location}`."
31+
end
32+
end
33+
34+
super(message, options)
35+
end
36+
end
37+
end
2238
end

spec/rspec/core/warnings_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,42 @@
2626
end
2727
end
2828

29+
describe "#warn_with" do
30+
context "when :use_spec_location_as_call_site => true is passed" do
31+
let(:options) {
32+
{
33+
:use_spec_location_as_call_site => true,
34+
:call_site => nil,
35+
}
36+
}
37+
38+
it "adds the source location of spec" do
39+
line = __LINE__ - 1
40+
file_path = RSpec::Core::Metadata.relative_path(__FILE__)
41+
expect(Kernel).to receive(:warn).with(/The warning. Warning generated from spec at `#{file_path}:#{line}`./)
42+
43+
RSpec.warn_with("The warning.", options)
44+
end
45+
46+
it "appends a period to the supplied message if one is not present" do
47+
line = __LINE__ - 1
48+
file_path = RSpec::Core::Metadata.relative_path(__FILE__)
49+
expect(Kernel).to receive(:warn).with(/The warning. Warning generated from spec at `#{file_path}:#{line}`./)
50+
51+
RSpec.warn_with("The warning", options)
52+
end
53+
54+
context "when there is no current example" do
55+
before do
56+
allow(RSpec).to receive(:current_example).and_return(nil)
57+
end
58+
59+
it "adds no message about the spec location" do
60+
expect(Kernel).to receive(:warn).with(/The warning\.$/)
61+
62+
RSpec.warn_with("The warning.", options)
63+
end
64+
end
65+
end
66+
end
2967
end

0 commit comments

Comments
 (0)