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 some false negatives for Minitest/GlobalExpectations #72

Merged
merged 1 commit into from Mar 29, 2020

Conversation

andrykonchin
Copy link
Contributor

@andrykonchin andrykonchin commented Mar 28, 2020

Fixed some use cases for the Minitest/GlobalExpectations cop.

I have noticed there are cases when the cop doesn't produce offenses:

  • expectation for constant e.g. class/module
  • method call with arguments
  • expectation for chained hash references

Let's look at some examples which the cope ignores:

response[1]['X-Runtime'].must_match /[\d\.]+/

::File.read(::File.join(@def_disk_cache, 'path', 'to', 'blah.html')).must_equal @def_value.first

Rack::Contrib.must_respond_to(:release)

Related links


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

@andrykonchin andrykonchin force-pushed the fix-global-expectation-cop branch 3 times, most recently from bbbe4e3 to 8cf3bb9 Compare March 28, 2020 16:39
@@ -162,6 +162,51 @@ class GlobalExpectationsTest < Minitest::Test
end
RUBY
end

define_method(:"registers_offense_when_using_global_#{matcher}_for_chained_hash_reference") do
Copy link
Member

Choose a reason for hiding this comment

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

To run a test, the prefix must start with test_.

Suggested change
define_method(:"registers_offense_when_using_global_#{matcher}_for_chained_hash_reference") do
define_method(:"test_registers_offense_when_using_global_#{matcher}_for_chained_hash_reference") do

And, if an appropriate method name exceeds the maximum length of Layout/LineLength, this repository allows the maximum length of Layout/LineLength of .rubocop_todo.yml to be increased. In that case, I think it will be 120 or less.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@koic Good catch. It was just a typo when I polish the final formatting of the new specs. Fixed 👍.

Also I increased Layout/LineLength in the .rubocop.yml file and keep .rubocop_todo.yml unchanged as far as it's an autogenerated file. Please let me know it there is a more convenient way to increase a line max length and to solve the issue.

RUBY
end

define_method(:"registers_offense_when_using_global_#{matcher}_for_method_call_with_params") do
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
define_method(:"registers_offense_when_using_global_#{matcher}_for_method_call_with_params") do
define_method(:"test_registers_offense_when_using_global_#{matcher}_for_method_call_with_params") do

RUBY
end

define_method(:"registers_offense_when_using_global_#{matcher}_for_constant") do
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
define_method(:"registers_offense_when_using_global_#{matcher}_for_constant") do
define_method(:"test_registers_offense_when_using_global_#{matcher}_for_constant") do

.rubocop.yml Outdated
@@ -51,6 +51,9 @@ Layout/ClassStructure:
Layout/TrailingWhitespace:
AllowInHeredoc: true

Layout/LineLength:
Max: 120

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 update .rubocop_todo.yml, not .rubocop.yml in this PR?

--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -20,4 +20,4 @@ Metrics/MethodLength:
 # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
 # URISchemes: http, https
 Layout/LineLength:
-  Max: 100
+  Max: 120

And other changes looks good to me. Can you squash your commits into one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@koic Done.

I fixed my mistake I've introduced in the pattern. The new pattern didn't support _'s aliases - value and expect. Added proper tests.

@andrykonchin andrykonchin force-pushed the fix-global-expectation-cop branch 2 times, most recently from f215c92 to 29c8418 Compare March 29, 2020 11:55
@koic koic merged commit 69fac3b into rubocop:master Mar 29, 2020
@koic
Copy link
Member

koic commented Mar 29, 2020

Thanks!

@andrykonchin andrykonchin deleted the fix-global-expectation-cop branch March 29, 2020 12:44
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