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

Commit e5a6b5c

Browse files
committed
Raise on unsupported hook scope usage
1 parent 19be4b2 commit e5a6b5c

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking Changes:
99
* Change the default `shared_context_metadata_behavior` to `apply_to_host_groups`
1010
and remove the configuration option. (Phil Pirozhkov, #2834)
1111
* Remove `run_all_when_everything_filtered` configuration option. (Phil Pirozhkov, #2845)
12+
* Raise on unsupported hook scope usage. (Phil Pirozhkov, #2849)
1213

1314
Enhancements:
1415

lib/rspec/core/hooks.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,13 @@ def register(prepend_or_append, position, *args, &block)
442442
scope, options = scope_and_options_from(*args)
443443

444444
if scope == :suite
445-
# TODO: consider making this an error in RSpec 4. For SemVer reasons,
446-
# we are only warning in RSpec 3.
447-
RSpec.warn_with "WARNING: `#{position}(:suite)` hooks are only supported on " \
448-
"the RSpec configuration object. This " \
449-
"`#{position}(:suite)` hook, registered on an example " \
450-
"group, will be ignored."
451-
return
445+
raise ArgumentError, "`#{position}(:suite)` hooks are only " \
446+
"supported on the RSpec configuration object. This " \
447+
"`#{position}(:suite)` hook, registered on an example " \
448+
"group, will be ignored."
452449
elsif scope == :context && position == :around
453-
# TODO: consider making this an error in RSpec 4. For SemVer reasons,
454-
# we are only warning in RSpec 3.
455-
RSpec.warn_with "WARNING: `around(:context)` hooks are not supported and " \
456-
"behave like `around(:example)."
450+
raise ArgumentError, "`around(:context)` hooks are not supported " \
451+
"and behave like `around(:example)`."
457452
end
458453

459454
hook = HOOK_TYPES[position][scope].new(block, options)

spec/rspec/core/hooks_spec.rb

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -484,32 +484,30 @@ def yielder
484484
])
485485
end
486486

487-
it 'emits a warning for `around(:context)`' do
488-
expect(RSpec).to receive(:warn_with).with(a_string_including(
489-
'`around(:context)` hooks are not supported'
490-
))
491-
RSpec.describe do
492-
around(:context) { }
493-
end
487+
it 'raises an error for `around(:context)`' do
488+
expect {
489+
RSpec.describe do
490+
around(:context) { }
491+
end
492+
}.to raise_error(ArgumentError, a_string_including("`around(:context)` hooks are not supported"))
494493
end
495494

496-
it 'emits a warning for `around(:context)` defined in `configure`' do
497-
expect(RSpec).to receive(:warn_with).with(a_string_including(
498-
'`around(:context)` hooks are not supported'
499-
))
500-
RSpec.configure do |c|
501-
c.around(:context) { }
502-
end
495+
it 'raises an error for `around(:context)` defined in `configure`' do
496+
expect {
497+
RSpec.configure do |c|
498+
c.around(:context) { }
499+
end
500+
}.to raise_error(ArgumentError, a_string_including("`around(:context)` hooks are not supported"))
503501
end
504502

505503
[:before, :around, :after].each do |type|
506504
it "emits a warning for `#{type}(:suite)` hooks" do
507-
expect(RSpec).to receive(:warn_with).with(a_string_including(
508-
"`#{type}(:suite)` hooks are only supported on the RSpec configuration object."
509-
))
510-
RSpec.describe do
511-
send(type, :suite) { }
512-
end
505+
expect {
506+
RSpec.describe do
507+
send(type, :suite) { }
508+
end
509+
}.to raise_error(ArgumentError, a_string_including(
510+
"`#{type}(:suite)` hooks are only supported on the RSpec configuration object"))
513511
end
514512
end
515513
end

spec/rspec/core/suite_hooks_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,12 @@ module RSpec::Core
6666
end
6767

6868
context "registered on an example group" do
69-
it "is ignored with a clear warning" do
70-
sequence = []
71-
69+
it "raises an error with a clear message" do
7270
expect {
7371
RSpec.describe "Group" do
74-
__send__(registration_method, :suite) { sequence << :suite_hook }
75-
example { sequence << :example }
76-
end.run
77-
}.to change { sequence }.to([:example]).
78-
and output(a_string_including("#{type}(:suite)")).to_stderr
72+
__send__(registration_method, :suite) { }
73+
end
74+
}.to raise_error(a_string_including("#{type}(:suite)"))
7975
end
8076
end
8177

0 commit comments

Comments
 (0)