From fdcb6c86c783cccee209de35a3fd5f2071ac43e0 Mon Sep 17 00:00:00 2001 From: Dave Corson-Knowles Date: Mon, 14 Oct 2024 15:46:15 -0700 Subject: [PATCH] Deprecate top_level_group? and test it in a Cop class --- CHANGELOG.md | 2 ++ .../cop/rspec/mixin/top_level_group.rb | 6 ++++++ spec/rubocop/cop/rspec/describe_class_spec.rb | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93499ceafb..eb84ae2d6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master (Unreleased) +- Deprecate `top_level_group?` private method on TopLevelGroup mixin. ([@corsonknowles]) + ## 3.1.0 (2024-10-01) - Add `RSpec/StringAsInstanceDoubleConstant` to check for and correct strings used as instance_doubles. ([@corsonknowles]) diff --git a/lib/rubocop/cop/rspec/mixin/top_level_group.rb b/lib/rubocop/cop/rspec/mixin/top_level_group.rb index c3b48fb1d9..9471d7ae2d 100644 --- a/lib/rubocop/cop/rspec/mixin/top_level_group.rb +++ b/lib/rubocop/cop/rspec/mixin/top_level_group.rb @@ -7,6 +7,11 @@ module RSpec module TopLevelGroup extend RuboCop::NodePattern::Macros + DEPRECATED_METHOD_WARNING = + 'top_level_group? is deprecated and will be ' \ + 'removed in the next major version of Rubocop/RSpec.' \ + 'Please copy the method if you need it for you mixin.' + def on_new_investigation super @@ -29,6 +34,7 @@ def on_top_level_example_group(_node); end def on_top_level_group(_node); end def top_level_group?(node) + warn DEPRECATED_METHOD_WARNING top_level_groups.include?(node) end diff --git a/spec/rubocop/cop/rspec/describe_class_spec.rb b/spec/rubocop/cop/rspec/describe_class_spec.rb index 8c4c514e9f..9cc73d6bff 100644 --- a/spec/rubocop/cop/rspec/describe_class_spec.rb +++ b/spec/rubocop/cop/rspec/describe_class_spec.rb @@ -215,4 +215,23 @@ RUBY end end + + context 'with mixin tests' do + describe '#top_level_group?' do + subject(:cop_with_top_level_group_mixin) do + described_class.new + end + + it 'warns because it is deprecated' do + # rubocop:disable RSpec/SubjectStub + allow(cop_with_top_level_group_mixin).to receive(:warn) + allow(cop_with_top_level_group_mixin).to receive(:top_level_groups) + .and_return([]) + + cop_with_top_level_group_mixin.send(:top_level_group?, nil) + expect(cop_with_top_level_group_mixin).to have_received(:warn) + # rubocop:enable RSpec/SubjectStub + end + end + end end