From 2ff8b66ee13a0036b23bc8f31c24a5be9e280095 Mon Sep 17 00:00:00 2001 From: Dave Corson-Knowles Date: Sat, 26 Oct 2024 11:24:12 -0700 Subject: [PATCH] Complete branch coverage for 4 more files. --- .rubocop_todo.yml | 7 +++--- .simplecov | 2 +- .../rspec/empty_line_after_subject_spec.rb | 11 +++++++- spec/rubocop/cop/rspec/instance_spy_spec.rb | 10 ++++++++ spec/rubocop/cop/rspec/stubbed_mock_spec.rb | 19 ++++++++++++++ spec/rubocop/rspec/config_formatter_spec.rb | 25 +++++++++++++++++++ 6 files changed, 69 insertions(+), 5 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index eef920c776..7614c5e187 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,14 +1,15 @@ # This configuration was generated by # `rubocop --auto-gen-config --no-offense-counts --no-auto-gen-timestamp` -# using RuboCop version 1.63.4. +# using RuboCop version 1.66.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -Lint/ToEnumArguments: +# This cop supports safe autocorrection (--autocorrect). +InternalAffairs/RedundantMessageArgument: Exclude: - - 'lib/rubocop/cop/rspec/multiple_expectations.rb' + - 'lib/rubocop/cop/rspec/context_wording.rb' Rake/MethodDefinitionInTask: Exclude: diff --git a/.simplecov b/.simplecov index 91dedc2805..4e4c02e2fe 100644 --- a/.simplecov +++ b/.simplecov @@ -2,7 +2,7 @@ SimpleCov.start do enable_coverage :branch - minimum_coverage line: 100, branch: 96.79 + minimum_coverage line: 100, branch: 97.3 add_filter '/spec/' add_filter '/vendor/bundle/' end diff --git a/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb b/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb index e3bbccc13f..b9f0aef1ed 100644 --- a/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb +++ b/spec/rubocop/cop/rspec/empty_line_after_subject_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe RuboCop::Cop::RSpec::EmptyLineAfterSubject do +RSpec.describe RuboCop::Cop::RSpec::EmptyLineAfterSubject, :config do it 'registers an offense for empty line after subject' do expect_offense(<<~RUBY) RSpec.describe User do @@ -211,4 +211,13 @@ end RUBY end + + it 'ignores subject outside of example group' do + expect_no_offenses(<<~RUBY) + subject(:foo) { bar } + describe 'example group' do + # ... + end + RUBY + end end diff --git a/spec/rubocop/cop/rspec/instance_spy_spec.rb b/spec/rubocop/cop/rspec/instance_spy_spec.rb index 9807003b24..94f0d360f5 100644 --- a/spec/rubocop/cop/rspec/instance_spy_spec.rb +++ b/spec/rubocop/cop/rspec/instance_spy_spec.rb @@ -44,6 +44,16 @@ end RUBY end + + it 'ignores instance_double when expect is called on another variable' do + expect_no_offenses(<<~RUBY) + it do + foo = instance_double(Foo).as_null_object + bar = instance_spy(Bar).as_null_object + expect(bar).to have_received(:baz) + end + RUBY + end end context 'when not used with `have_received`' do diff --git a/spec/rubocop/cop/rspec/stubbed_mock_spec.rb b/spec/rubocop/cop/rspec/stubbed_mock_spec.rb index 37ee2e6863..879480ce7c 100644 --- a/spec/rubocop/cop/rspec/stubbed_mock_spec.rb +++ b/spec/rubocop/cop/rspec/stubbed_mock_spec.rb @@ -134,4 +134,23 @@ .with(bar).and_return baz RUBY end + + describe '#replacement', :config do + it 'returns "allow" for :expect' do + expect(cop.send(:replacement, :expect)).to eq(:allow) + end + + it 'returns "allow(subject)" for :is_expected' do + expect(cop.send(:replacement, :is_expected)).to eq('allow(subject)') + end + + it 'returns "allow_any_instance_of" for :expect_any_instance_of' do + expect(cop.send(:replacement, + :expect_any_instance_of)).to eq(:allow_any_instance_of) + end + + it 'falls through silently and returns nil for unknown methods' do + expect(cop.send(:replacement, :unknown_method)).to be_nil + end + end end diff --git a/spec/rubocop/rspec/config_formatter_spec.rb b/spec/rubocop/rspec/config_formatter_spec.rb index 2eef012947..7bfc6b69e1 100644 --- a/spec/rubocop/rspec/config_formatter_spec.rb +++ b/spec/rubocop/rspec/config_formatter_spec.rb @@ -64,4 +64,29 @@ Reference: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/Baz YAML end + + describe '#unified_config' do + context 'when cop is in SUBDEPARTMENTS or AMENDMENTS' do + let(:config) do + { 'RSpec/SubDepartment' => {}, 'Metrics/BlockLength' => {} } + end + let(:descriptions) do + { 'RSpec/SubDepartment' => {}, 'Metrics/BlockLength' => {} } + end + + before do + stub_const('RuboCop::RSpec::ConfigFormatter::SUBDEPARTMENTS', + ['RSpec/SubDepartment']) + stub_const('RuboCop::RSpec::ConfigFormatter::AMENDMENTS', + ['Metrics/BlockLength']) + end + + it 'skips processing for those cops' do + formatter = described_class.new(config, descriptions) + unified_config = formatter.send(:unified_config) + expect(unified_config['RSpec/SubDepartment']).to eq({}) + expect(unified_config['Metrics/BlockLength']).to eq({}) + end + end + end end