From aa884c37e4a3f7ea2021839ee2eeaaed56ba8cd6 Mon Sep 17 00:00:00 2001 From: Phil Pirozhkov Date: Sat, 11 Jan 2020 14:52:14 +0300 Subject: [PATCH] Emit a warning for `around(:context)` fixes #2486 --- Changelog.md | 2 ++ lib/rspec/core/hooks.rb | 5 +++++ spec/rspec/core/hooks_spec.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/Changelog.md b/Changelog.md index 40c1237da2..2603682b5d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,8 @@ Bug Fixes: * Prevent bisect command from blocking when number of specs exceeds file descriptor limit on OSX or Linux. (Benoit Tigeot, #2669) * Prevent warnings being issued on Ruby 2.7.0. (Jon Rowe, #2680) +* Emit a warning when `around` hook is used with `:context` scope + (Phil Pirozhkov, #2687) ### 3.9.0 / 2019-10-07 [Full Changelog](http://github.com/rspec/rspec-core/compare/v3.8.2...v3.9.0) diff --git a/lib/rspec/core/hooks.rb b/lib/rspec/core/hooks.rb index 56c0be852d..6cde2ca1a5 100644 --- a/lib/rspec/core/hooks.rb +++ b/lib/rspec/core/hooks.rb @@ -448,6 +448,11 @@ def register(prepend_or_append, position, *args, &block) "`#{position}(:suite)` hook, registered on an example " \ "group, will be ignored." return + elsif scope == :context && position == :around + # TODO: consider making this an error in RSpec 4. For SemVer reasons, + # we are only warning in RSpec 3. + RSpec.warn_with "WARNING: `around(:context)` hooks are not supported and " \ + "behave like `around(:example)." end hook = HOOK_TYPES[position][scope].new(block, options) diff --git a/spec/rspec/core/hooks_spec.rb b/spec/rspec/core/hooks_spec.rb index d9fb62de9f..53a5a873aa 100644 --- a/spec/rspec/core/hooks_spec.rb +++ b/spec/rspec/core/hooks_spec.rb @@ -492,5 +492,34 @@ def yielder :hooks ]) end + + it 'emits a warning for `around(:context)`' do + expect(RSpec) + .to receive(:warn_with) + .with(a_string_including('`around(:context)` hooks are not supported')) + RSpec.describe do + around(:context) { } + end + end + + it 'emits a warning for `around(:context)` defined in `configure`' do + expect(RSpec).to receive(:warn_with).with(a_string_including( + '`around(:context)` hooks are not supported' + )) + RSpec.configure do |c| + c.around(:context) { } + end + end + + [:before, :around, :after].each do |type| + it "emits a warning for `#{type}(:suite)` hooks" do + expect(RSpec).to receive(:warn_with).with(a_string_including( + "`#{type}(:suite)` hooks are only supported on the RSpec configuration object." + )) + RSpec.describe do + send(type, :suite) { } + end + end + end end end