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

Remove relevance detection code #1063

Merged
merged 1 commit into from Oct 29, 2020

Conversation

pirj
Copy link
Member

@pirj pirj commented Oct 27, 2020

We have had problems with it:

  • it was slow and memory-hungry
  • it uses a non-standard extension of RuboCop's config (Patterns), while a comparable Include option exists
  • department-specific (e.g. FactoryBot) config was ignored
  • for the same department (i.e. FactoryBot) files to inspect are different for different cops

Close #967
Close #1062
Fixes #1044

It's all interconnected in this PR, here's the path I had to go through:

  1. Remove the code that checks file relevance
  2. Replace Patterns with Include
  3. Adjust config/default.yml
  4. Adjust specs, specifying the file for other departments
  5. Tweak config shared example to respect department settings
  6. Adjust config formatter
  7. Fix spec that failed to detect offences in an unrelated file

TODOs


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 modified an existing cop's configuration options:

  • Set VersionChanged in config/default.yml to the next major version.

@pirj pirj self-assigned this Oct 27, 2020
@pirj
Copy link
Member Author

pirj commented Oct 27, 2020

@fatkodima Please take a look, what do you think about memory consumption? Is this faster, or worse than previously?
For some reason, I can't invite you as a reviewer.

This was referenced Oct 27, 2020
@pirj pirj added this to the 2.0 milestone Oct 27, 2020
@pirj pirj marked this pull request as ready for review October 27, 2020 22:52
@fatkodima
Copy link

For some reason, I can't invite you as a reviewer.

I think that's because I'm not a member of a rubocop organization.

Just measured - memory is good, good job! 👏 👏


RSpec/ExampleLength:
Exclude:
- spec/rubocop/cop/rspec/factory_bot/attribute_defined_statically_spec.rb
Copy link
Member Author

Choose a reason for hiding this comment

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

It was a single it_behaves_like with good_code and bad_code defined on example group level. Now, when it's an example, it has overflown the allowed margin.

@@ -1,14 +1,9 @@
---
AllCops:
Copy link
Member Author

Choose a reason for hiding this comment

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

The reason I've dropped AllCops is that it's never mentioned in the docs it's a preferred way to configure cops in departments. It works quite fine on the root level. https://docs.rubocop.org/rubocop/1.0/configuration.html
Also, I think there was some complication where for_badge didn't work properly with configuration options defined inside AllCops.

@@ -241,6 +236,9 @@ RSpec/ExpectOutput:
RSpec/FilePath:
Description: Checks that spec file paths are consistent and well-formed.
Enabled: true
Include:
- "**/*_spec*rb*"
Copy link
Member Author

Choose a reason for hiding this comment

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

This is due to file name detection, e.g. some_spec.rb.rb and some_specorb.

- features/support/factories/**/*.rb

RSpec:
Include:
Copy link
Member Author

Choose a reason for hiding this comment

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

This behavior of Include (overriding default.yml) was introduced in 0.56.0 via rubocop/rubocop#5882. This change allows people to include/exclude precisely what they need to, without the defaults getting in the way.

Include:
- spec/factories.rb
- spec/factories/**/*.rb
- features/support/factories/**/*.rb
Copy link
Member Author

Choose a reason for hiding this comment

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

This cop should only inspect factory definitions.

@@ -1,22 +1,57 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically do
def inspected_source_filename
Copy link
Member Author

Choose a reason for hiding this comment

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

Our overloaded expect_offense/expect_no_offenses use this to pass the file name to super.

This tells the cop that it inspects a relevant source.

end
RUBY

include_examples 'autocorrect', bad, corrected
Copy link
Member Author

Choose a reason for hiding this comment

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

autocorrect shared example does not pass the correct file path to expect_offense, and thus the cop considers the file irrelevant and skips it, failing the example.

@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::RSpec::InstanceVariable do
RSpec.describe RuboCop::Cop::RSpec::InstanceVariable, :config do
Copy link
Member Author

Choose a reason for hiding this comment

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

Because it needs the default config, specifically Include, otherwise

expect_no_offenses(<<-RUBY, 'lib/source_code.rb')

fails.

Comment on lines +7 to +11
department_name = cop_class.badge.department.to_s
# By default, `RSpec/Include: ['**/*_spec.rb', '**/spec/**/*']`
department_configuration = RuboCop::ConfigLoader
.default_configuration
.for_department(department_name)
Copy link
Member Author

Choose a reason for hiding this comment

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

I hope to submit this upstream.

@pirj
Copy link
Member Author

pirj commented Oct 27, 2020

memory is good, good job!

@fatkodima Thanks! 🙌

not a member of a rubocop organization.

Just not yet. Keep up the good work!

@pirj pirj mentioned this pull request Oct 28, 2020
16 tasks
@pirj
Copy link
Member Author

pirj commented Oct 28, 2020

@sl4vr Appreciate it if you could take a look.
I have the impression that this change will make #956 simpler. WDYT?

@pirj pirj force-pushed the remove-relevance-detection-code branch from 561d04e to 676b2f4 Compare October 28, 2020 09:59
@pirj
Copy link
Member Author

pirj commented Oct 28, 2020

Rebased. It's down from +194 −162 to +113 −106 now.
@bquorning please take a look, but no rush.

super
end

def expect_no_offenses(source, filename = DEFAULT_FILENAME) # rubocop:disable Lint/UselessMethodDefinition
Copy link
Member Author

Choose a reason for hiding this comment

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

@tejasbubane Removed it 🤷

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.

This is looking real good. rubocop/rubocop#5882 is an amazing find, and I hope it works as well as it seems 👍

docs/modules/ROOT/pages/usage.adoc Show resolved Hide resolved
lib/rubocop/rspec/config_formatter.rb Outdated Show resolved Hide resolved
spec/rubocop/cop/rspec/base_spec.rb Outdated Show resolved Hide resolved
@pirj pirj force-pushed the remove-relevance-detection-code branch from c2105b0 to bcb058c Compare October 28, 2020 22:38
We have had problems with it:
 - it was slow
 - it is a non-standard extension of RuboCop's config
 - Include option exists
 - department-specific (e.g. FactoryBot) config was ignored
@pirj pirj force-pushed the remove-relevance-detection-code branch from 7fb58c3 to 4920879 Compare October 28, 2020 22:44
@pirj
Copy link
Member Author

pirj commented Oct 28, 2020

@bquorning @Darhazer Please merge at will. Addressed your concerns.

@bquorning bquorning merged commit 2cd7639 into release-2.0 Oct 29, 2020
@bquorning bquorning deleted the remove-relevance-detection-code branch October 29, 2020 11:58
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

4 participants