Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete branch coverage for more files #1990

Merged

Conversation

corsonknowles
Copy link
Contributor

@corsonknowles corsonknowles commented Oct 26, 2024

Increase branch coverage minimum threshold.

Modernize .rubocop_todo.yml with Rubocop 1.6.8

In the further pursuit of full coverage for Rubocop/RSpec, this solves for additional files.

Note

We remove a safe operator from a method used in a node matcher here, since it should always get a node.

Reconciled with:


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Updated documentation.
  • Added an entry to the CHANGELOG.md if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

If you have created a new cop:

  • Added the new cop to config/default.yml.
  • The cop is configured as Enabled: pending in config/default.yml.
  • The cop is configured as Enabled: true in .rubocop.yml.
  • The cop documents examples of good and bad code.
  • The tests assert both that bad code is reported and that good code is not reported.
  • Set VersionAdded: "<<next>>" in default/config.yml.

If you have modified an existing cop's configuration options:

  • Set VersionChanged: "<<next>>" in config/default.yml.

@corsonknowles corsonknowles requested a review from a team as a code owner October 26, 2024 18:25
@corsonknowles corsonknowles force-pushed the continue_branch_coverage_quest branch 2 times, most recently from 2ff8b66 to 1dedfd0 Compare October 26, 2024 18:40
@corsonknowles corsonknowles changed the title Complete branch coverage for 4 more files. Complete branch coverage for 5 more files Oct 26, 2024
@bquorning
Copy link
Collaborator

Surprisingly little duplication between this and #1989 :yay:

@corsonknowles corsonknowles changed the title Complete branch coverage for 5 more files Complete branch coverage for more files Oct 27, 2024
@corsonknowles
Copy link
Contributor Author

@bquorning @ydah I reconciled this with #1989 and it should be ready to go!

:expect_any_instance_of)).to eq(:allow_any_instance_of)
end

it 'falls through silently and returns nil for unknown methods' do
Copy link
Contributor Author

@corsonknowles corsonknowles Oct 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically this is all that's needed in here to solve for the case statement branch coverage

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t particularly like this kind of spec. Testing a private method with very edge case input doesn’t provide a whole lot of value. Actually we could change StubbedMock#replacement to do a hash lookup, and SimpleCov would claim 100% coverage:

        def replacement(method_name)
          {
            expect: 'allow',
            is_expected: 'allow(subject)',
            expect_any_instance_of: 'allow_any_instance_of'
          }.fetch(method_name)
        end

I think it would be better look at whether there might actually be a problem with the method_name value which is passed into #replacement. It seems to be found in #on_send by the expectation node matcher, which is looking for #Expectations.all… That method is define in language.rb as returning the language configuration for Expectations, which in default.yml is set to this array of values:

  • are_expected
  • expect
  • expect_any_instance_of
  • is_expected
  • should
  • should_not
  • should_not_receive
  • should_receive

I’m thinking, could we make test case (using expect_offense or expect_no_offenses) using one of those other values, and thereby hit the code that’s currently not covered by tests?

@corsonknowles corsonknowles force-pushed the continue_branch_coverage_quest branch 2 times, most recently from aadca81 to 679c0f8 Compare October 27, 2024 13:13
@corsonknowles
Copy link
Contributor Author

The Ruby head CI failure comes down to how rocket hashes are spaced in the parser.

@bquorning
Copy link
Collaborator

The Ruby head CI failure comes down to how rocket hashes are spaced in the parser.

See

if RUBY_VERSION >= '3.4'
let(:expected_special) { 's(:sym, :special) => true' }
let(:expected_symbol) { 's(:sym, :symbol) => true' }
else
let(:expected_special) { 's(:sym, :special)=>true' }
let(:expected_symbol) { 's(:sym, :symbol)=>true' }
end
for inspiration on how to work around that.

@corsonknowles corsonknowles force-pushed the continue_branch_coverage_quest branch 3 times, most recently from 664b1f5 to d252842 Compare November 2, 2024 15:48
spec/rubocop/rspec/inject_spec.rb Outdated Show resolved Hide resolved
expect(bar).to have_received(:baz)
end
RUBY
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is good 👍🏼

I actually found again that the lib/ code can be optimized a bit changes a bit, moving code into the node pattern:

diff --git a/lib/rubocop/cop/rspec/instance_spy.rb b/lib/rubocop/cop/rspec/instance_spy.rb
index cca5a1e6..43298d35 100644
--- a/lib/rubocop/cop/rspec/instance_spy.rb
+++ b/lib/rubocop/cop/rspec/instance_spy.rb
@@ -36,7 +36,7 @@ module RuboCop
         def_node_search :have_received_usage, <<~PATTERN
           (send
             (send nil? :expect
-            (lvar $_)) :to
+            (lvar %1)) :to
             (send nil? :have_received
             ...)
           ...)
@@ -46,9 +46,7 @@ module RuboCop
           return unless example?(node)
 
           null_double(node) do |var, receiver|
-            have_received_usage(node) do |expected|
-              next if expected != var
-
+            have_received_usage(node, var) do
               add_offense(receiver) do |corrector|
                 autocorrect(corrector, receiver)
               end

It would be nice to find a way to calculate test coverage of the node patterns…

:expect_any_instance_of)).to eq(:allow_any_instance_of)
end

it 'falls through silently and returns nil for unknown methods' do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t particularly like this kind of spec. Testing a private method with very edge case input doesn’t provide a whole lot of value. Actually we could change StubbedMock#replacement to do a hash lookup, and SimpleCov would claim 100% coverage:

        def replacement(method_name)
          {
            expect: 'allow',
            is_expected: 'allow(subject)',
            expect_any_instance_of: 'allow_any_instance_of'
          }.fetch(method_name)
        end

I think it would be better look at whether there might actually be a problem with the method_name value which is passed into #replacement. It seems to be found in #on_send by the expectation node matcher, which is looking for #Expectations.all… That method is define in language.rb as returning the language configuration for Expectations, which in default.yml is set to this array of values:

  • are_expected
  • expect
  • expect_any_instance_of
  • is_expected
  • should
  • should_not
  • should_not_receive
  • should_receive

I’m thinking, could we make test case (using expect_offense or expect_no_offenses) using one of those other values, and thereby hit the code that’s currently not covered by tests?

@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First up, I think that since 1fa9ce3 is long merged, we can just remove SUBDEPARTMENTS now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And second, it looks like AMENDMENTS is currently a String and not an Array. Which is… confusing, to put it mildly. Aren’t we essentially doing this:

"abcde".gsub(*%(bc), 'x')

🙈

@@ -45,7 +45,7 @@ def metadata
private

def valid_scope?(node)
node&.sym_type? && Language::HookScopes.all(node.value)
node.sym_type? && Language::HookScopes.all(node.value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

Copy link
Collaborator

@bquorning bquorning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@bquorning bquorning merged commit 5d96600 into rubocop:master Nov 2, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants