-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
--- | ||
AllCops: | ||
RSpec: | ||
Patterns: | ||
- _spec.rb | ||
- "(?:^|/)spec/" | ||
RSpec/FactoryBot: | ||
Patterns: | ||
- spec/factories.rb | ||
- spec/factories/**/*.rb | ||
- features/support/factories/**/*.rb | ||
RSpec: | ||
Include: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
- "**/*_spec.rb" | ||
- "**/spec/**/*" | ||
|
||
RSpec/AlignLeftLetBrace: | ||
Description: Checks that left braces for adjacent single line lets are aligned. | ||
|
@@ -241,6 +235,9 @@ RSpec/ExpectOutput: | |
RSpec/FilePath: | ||
Description: Checks that spec file paths are consistent and well-formed. | ||
Enabled: true | ||
Include: | ||
- "**/*_spec*rb*" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is due to file name detection, e.g. |
||
- "**/spec/**/*" | ||
CustomTransform: | ||
RuboCop: rubocop | ||
RSpec: rspec | ||
|
@@ -639,13 +636,23 @@ RSpec/Capybara/VisibilityMatcher: | |
RSpec/FactoryBot/AttributeDefinedStatically: | ||
Description: Always declare attribute values as blocks. | ||
Enabled: true | ||
Include: | ||
- spec/factories.rb | ||
- spec/factories/**/*.rb | ||
- features/support/factories/**/*.rb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cop should only inspect factory definitions. |
||
VersionAdded: '1.28' | ||
VersionChanged: '2.0' | ||
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/AttributeDefinedStatically | ||
|
||
RSpec/FactoryBot/CreateList: | ||
Description: Checks for create_list usage. | ||
Enabled: true | ||
Include: | ||
- "**/*_spec.rb" | ||
- "**/spec/**/*" | ||
- spec/factories.rb | ||
- spec/factories/**/*.rb | ||
- features/support/factories/**/*.rb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
EnforcedStyle: create_list | ||
SupportedStyles: | ||
- create_list | ||
|
@@ -657,6 +664,10 @@ RSpec/FactoryBot/CreateList: | |
RSpec/FactoryBot/FactoryClassName: | ||
Description: Use string value when setting the class attribute explicitly. | ||
Enabled: true | ||
Include: | ||
- spec/factories.rb | ||
- spec/factories/**/*.rb | ||
- features/support/factories/**/*.rb | ||
VersionAdded: '1.37' | ||
VersionChanged: '2.0' | ||
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/FactoryBot/FactoryClassName | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,72 +4,16 @@ module RuboCop | |
module Cop | ||
module RSpec | ||
# @abstract parent class to RSpec cops | ||
# | ||
# The criteria for whether rubocop-rspec analyzes a certain ruby file | ||
# is configured via `AllCops/RSpec`. For example, if you want to | ||
# customize your project to scan all files within a `test/` directory | ||
# then you could add this to your configuration: | ||
# | ||
# @example configuring analyzed paths | ||
# # .rubocop.yml | ||
# # AllCops: | ||
# # RSpec: | ||
# # Patterns: | ||
# # - '_test.rb$' | ||
# # - '(?:^|/)test/' | ||
class Base < ::RuboCop::Cop::Base | ||
include RuboCop::RSpec::Language | ||
include RuboCop::RSpec::Language::NodePattern | ||
|
||
DEFAULT_CONFIGURATION = | ||
RuboCop::RSpec::CONFIG.fetch('AllCops').fetch('RSpec') | ||
|
||
DEFAULT_PATTERN_RE = Regexp.union( | ||
DEFAULT_CONFIGURATION.fetch('Patterns') | ||
.map(&Regexp.public_method(:new)) | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect this to dramatically simplify #956 |
||
|
||
exclude_from_registry | ||
|
||
# Invoke the original inherited hook so our cops are recognized | ||
def self.inherited(subclass) # rubocop:disable Lint/MissingSuper | ||
RuboCop::Cop::Base.inherited(subclass) | ||
end | ||
|
||
def relevant_file?(file) | ||
relevant_rubocop_rspec_file?(file) && super | ||
end | ||
|
||
private | ||
|
||
def relevant_rubocop_rspec_file?(file) | ||
rspec_pattern.match?(file) | ||
end | ||
|
||
def rspec_pattern | ||
if rspec_pattern_config? | ||
Regexp.union(rspec_pattern_config.map(&Regexp.public_method(:new))) | ||
else | ||
DEFAULT_PATTERN_RE | ||
end | ||
end | ||
|
||
def all_cops_config | ||
config | ||
.for_all_cops | ||
end | ||
|
||
def rspec_pattern_config? | ||
return unless all_cops_config.key?('RSpec') | ||
|
||
all_cops_config.fetch('RSpec').key?('Patterns') | ||
end | ||
|
||
def rspec_pattern_config | ||
all_cops_config | ||
.fetch('RSpec', DEFAULT_CONFIGURATION) | ||
.fetch('Patterns') | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Base do | ||
subject(:cop) { RuboCop::RSpec::FakeCop.new(config) } | ||
|
||
let(:config) do | ||
rubocop_config = | ||
{ | ||
'AllCops' => { | ||
'RSpec' => { | ||
'Patterns' => rspec_patterns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
}, | ||
'RSpec/FakeCop' => { | ||
'Exclude' => %w[bar_spec.rb] | ||
} | ||
} | ||
|
||
RuboCop::Config.new(rubocop_config, 'fake_cop_config.yml') | ||
end | ||
|
||
let(:rspec_patterns) { ['_spec.rb$', '(?:^|/)spec/'] } | ||
let(:cop_class) { RuboCop::RSpec::FakeCop } | ||
let(:cop_config) { { 'Exclude' => %w[bar_spec.rb] } } | ||
|
||
before do | ||
stub_const('RuboCop::RSpec::FakeCop', | ||
|
@@ -78,8 +61,12 @@ def on_send(node) | |
end | ||
|
||
context 'when custom patterns are specified' do | ||
let(:rspec_patterns) do | ||
['_test\.rb$'] | ||
let(:other_cops) do | ||
{ | ||
'RSpec' => { | ||
'Include' => ['*_test\.rb'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This overrides the setting of |
||
} | ||
} | ||
end | ||
|
||
it 'registers offenses when the path matches a custom specified pattern' do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::FactoryBot::AttributeDefinedStatically do | ||
def inspected_source_filename | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our overloaded This tells the cop that it inspects a relevant source. |
||
'spec/factories.rb' | ||
end | ||
|
||
it 'registers an offense for offending code' do | ||
pirj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect_offense(<<-RUBY) | ||
FactoryBot.define do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.shared_context 'with RuboCop-RSpec config', :config do | ||
# This overrides the config defined in the default shared context since | ||
# RuboCop ignores department-level cop configuration in specs. | ||
let(:config) do | ||
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) | ||
Comment on lines
+7
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope to submit this upstream. |
||
|
||
hash = { 'AllCops' => all_cops_config, | ||
cop_class.cop_name => cur_cop_config, | ||
department_name => department_configuration } | ||
.merge!(other_cops) | ||
|
||
RuboCop::Config.new(hash, "#{Dir.pwd}/.rubocop.yml") | ||
end | ||
end |
There was a problem hiding this comment.
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.htmlAlso, I think there was some complication where
for_badge
didn't work properly with configuration options defined insideAllCops
.