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

Fix raise error when usgin assert with block #175

Merged
merged 1 commit into from Jun 13, 2022

Conversation

ippachi
Copy link
Contributor

@ippachi ippachi commented Jun 13, 2022

rubocop-minitest was raising error if the style was to pass a block to an assert, such as power-assert.
So, I made it so that such cases are ignored.

This is stacktrace.

  1) Error:
AssertPredicateTest#test_does_not_raise_error_using_assert_with_block:
NoMethodError: undefined method `block_type?' for nil:NilClass

          return if first_argument.block_type? || first_argument.numblock_type?
                                  ^^^^^^^^^^^^
    /Users/ippachi/ghq/github.com/ippachi/rubocop-minitest/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb:16:in `on_send'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:136:in `public_send'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:160:in `with_cop_error_handling'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:134:in `each'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:70:in `on_send'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.18.0/lib/rubocop/ast/traversal.rb:153:in `on_block'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:71:in `on_block'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.18.0/lib/rubocop/ast/traversal.rb:153:in `on_def'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:71:in `on_def'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.18.0/lib/rubocop/ast/traversal.rb:153:in `on_class'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:71:in `on_class'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/gems/rubocop-ast-1.18.0/lib/rubocop/ast/traversal.rb:20:in `walk'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/commissioner.rb:86:in `investigate'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/team.rb:155:in `investigate_partial'
    /Users/ippachi/.asdf/installs/ruby/3.1.2/lib/ruby/gems/3.1.0/bundler/gems/rubocop-f133b38b5893/lib/rubocop/cop/team.rb:83:in `investigate'
    /Users/ippachi/ghq/github.com/ippachi/rubocop-minitest/lib/rubocop/minitest/assert_offense.rb:114:in `_investigate'
    /Users/ippachi/ghq/github.com/ippachi/rubocop-minitest/lib/rubocop/minitest/assert_offense.rb:153:in `inspect_source'
    /Users/ippachi/ghq/github.com/ippachi/rubocop-minitest/lib/rubocop/minitest/assert_offense.rb:85:in `assert_no_offenses'
    /Users/ippachi/ghq/github.com/ippachi/rubocop-minitest/test/rubocop/cop/minitest/assert_predicate_test.rb:157:in `test_does_not_raise_error_using_assert_with_block'

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic
Copy link
Member

koic commented Jun 13, 2022

Can you refactor to the following?

diff --git a/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb b/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
index 4a0f204..5269553 100644
--- a/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
+++ b/lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
@@ -13,7 +13,10 @@ module RuboCop

           first_argument = arguments.first

-          return if no_offense_first_argument?(first_argument)
+          return unless first_argument
+          return if first_argument.block_type? || first_argument.numblock_type?
+          return unless predicate_method?(first_argument)
+          return unless first_argument.arguments.count.zero?

           add_offense(node, message: offense_message(arguments)) do |corrector|
             autocorrect(corrector, node, arguments)
@@ -36,6 +39,10 @@ module RuboCop
           peel_redundant_parentheses_from(arguments.first.children)
         end

+        def predicate_method?(first_argument)
+          first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method?
+        end
+
         def offense_message(arguments)
           message_argument = arguments.last if arguments.first != arguments.last

@@ -57,15 +64,6 @@ module RuboCop
         def correct_receiver(receiver)
           receiver ? receiver.source : 'self'
         end
-
-        def no_offense_first_argument?(first_argument)
-          return true unless first_argument
-          return true if first_argument.block_type? || first_argument.numblock_type?
-          return true unless first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method?
-          return true unless first_argument.arguments.count.zero?
-
-          false
-        end
       end
     end
   end

So, first_argument.respond_to?(:predicate_method?) && first_argument.predicate_method? will be extracted to private predicate_method?(first_argument) method.

@@ -0,0 +1 @@
* Fix raise error when using assert with block. ([@ippachi][])
Copy link
Member

Choose a reason for hiding this comment

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

Can you add the pull request address?

Suggested change
* Fix raise error when using assert with block. ([@ippachi][])
* [#175](https://github.com/rubocop/rubocop-minitest/pull/175): Fix raise error when using assert with block. ([@ippachi][])

@ippachi
Copy link
Contributor Author

ippachi commented Jun 13, 2022

@koic

Thanks for the fast response!
I Fixed.

@koic koic merged commit 7015950 into rubocop:master Jun 13, 2022
@koic
Copy link
Member

koic commented Jun 13, 2022

Good catch and great work! RuboCop Minitest 0.20.1 has been released. Thank you!
https://github.com/rubocop/rubocop-minitest/releases/tag/v0.20.1

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.

None yet

2 participants