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

Commit 56fad31

Browse files
authored
Merge pull request #2278 from devonestes/adding-error-message
Add helpful warning message for shared example groups
2 parents ac7afb0 + 3657b39 commit 56fad31

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

lib/rspec/core/shared_example_group.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,20 +213,35 @@ def valid_name?(candidate)
213213

214214
def warn_if_key_taken(context, key, new_block)
215215
existing_module = shared_example_groups[context][key]
216-
217216
return unless existing_module
218217

219-
RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil
220-
|WARNING: Shared example group '#{key}' has been previously defined at:
221-
| #{formatted_location existing_module.definition}
222-
|...and you are now defining it at:
223-
| #{formatted_location new_block}
224-
|The new definition will overwrite the original one.
225-
WARNING
218+
old_definition_location = formatted_location existing_module.definition
219+
new_definition_location = formatted_location new_block
220+
loaded_spec_files = RSpec.configuration.loaded_spec_files
221+
222+
if loaded_spec_files.include?(new_definition_location) && old_definition_location == new_definition_location
223+
RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil
224+
|WARNING: Your shared example group, '#{key}', defined at:
225+
| #{old_definition_location}
226+
|was automatically loaded by RSpec because the file name
227+
|matches the configured autoloading pattern (#{RSpec.configuration.pattern}),
228+
|and is also being required from somewhere else. To fix this
229+
|warning, either rename the file to not match the pattern, or
230+
|do not explicitly require the file.
231+
WARNING
232+
else
233+
RSpec.warn_with <<-WARNING.gsub(/^ +\|/, ''), :call_site => nil
234+
|WARNING: Shared example group '#{key}' has been previously defined at:
235+
| #{old_definition_location}
236+
|...and you are now defining it at:
237+
| #{new_definition_location}
238+
|The new definition will overwrite the original one.
239+
WARNING
240+
end
226241
end
227242

228243
def formatted_location(block)
229-
block.source_location.join ":"
244+
block.source_location.join(":").gsub(/:in.*$/, '')
230245
end
231246

232247
if Proc.method_defined?(:source_location)

spec/rspec/core/shared_example_group_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def find_implementation_block(registry, scope, name)
6969
expect(Kernel).to_not respond_to(shared_method_name)
7070
end
7171

72-
it "displays a warning when adding a second shared example group with the same name" do
72+
it 'displays a warning when adding a second shared example group with the same name' do
7373
group.send(shared_method_name, 'some shared group') {}
7474
original_declaration = [__FILE__, __LINE__ - 1].join(':')
7575

@@ -82,6 +82,22 @@ def find_implementation_block(registry, scope, name)
8282
expect(warning).to_not include 'Called from'
8383
end
8484

85+
it 'displays a helpful message when you define a shared example group in *_spec.rb file' do
86+
warning = nil
87+
allow(::Kernel).to receive(:warn) { |msg| warning = msg }
88+
declaration = nil
89+
90+
2.times do
91+
group.send(shared_method_name, 'some shared group') {}
92+
declaration = [__FILE__, __LINE__ - 1].join(':')
93+
RSpec.configuration.loaded_spec_files << declaration
94+
end
95+
96+
better_error = 'was automatically loaded by RSpec because the file name'
97+
expect(warning).to include('some shared group', declaration, better_error)
98+
expect(warning).to_not include 'Called from'
99+
end
100+
85101
it 'works with top level defined examples in modules' do
86102
expect(RSpec::configuration.reporter).to_not receive(:deprecation)
87103
RSpec.describe('example group') { include_context 'top level in module' }

0 commit comments

Comments
 (0)